elasticsearch入门教程

2018/07/23 elasticsearch

官网

https://www.elastic.co/cn/

版本

6.3.0

ES和关系型数据库的数据对比

DB ES
database 数据库 index 索引
table type 类型
row 行 document 文档
column 列 field 字段

安装

安装elasticsearch服务

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.0.tar.gz
tar -zxvf elasticsearch-6.3.0.tar.gz
cd elasticsearch-6.3.0
# 启动(后台启动加参数 -D )
./bin/elasticsearch
# 测试是否正常
curl http://localhost:9200
{
  "name" : "node-1",
  "cluster_name" : "my-es",
  "cluster_uuid" : "esvG05v5RRuP0kbyhHxYYA",
  "version" : {
    "number" : "6.3.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "424e937",
    "build_date" : "2018-06-11T23:38:03.357887Z",
    "build_snapshot" : false,
    "lucene_version" : "7.3.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

# 配置允许跨域访问
vim config/elasticsearch.yml 
# 设置允许跨域
http.cors.allow-origin: "*"
# 设置允许远程连接
network.host: 0.0.0.0

安装elasticsearch-head可视化插件

git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
npm run start
open http://localhost:9100/

CRUD API

index 索引

文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html

  • 创建索引
curl -X PUT 'http://localhost:9200/index_hello_world'
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "index_hello_world"
}
  • 查询索引
curl -X GET 'http://localhost:9200/index_hello_world'
{
    "index_hello_world": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1532362513103",
                "number_of_shards": "5",
                "number_of_replicas": "1",
                "uuid": "Qgf_brzgT2i0go9T8GbNCQ",
                "version": {
                    "created": "6030099"
                },
                "provided_name": "index_hello_world"
            }
        }
    }
}
  • 删除索引
curl -X DELETE 'http://localhost:9200/index_hello_world'
{
    "acknowledged": true
}
  • 判断索引是否存在
# 返回200表示存在
curl -i -X HEAD 'http://localhost:9200/index_hello_world'
Warning: Setting custom HTTP method to HEAD with -X/--request may not work the 
Warning: way you want. Consider using -I/--head instead.
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 253

Search

    Table of Contents