ES 基本使用

1. 索引初始化

  创建索引之前可以对索引做初始化操作,比如指定shards数据,replicas数量。

curl -s -XGET 'http://master:9200/cmz/' | json
curl -s -XPUT 'http://master:9200/cmz/' -d '{
    "settings":{
        "index":{
            "number_of_shards":5,
            "number_of_replicas":1
        }
    }
}' | json
curl -s -XGET 'http://master:9200/cmz/' | json
或者 curl -s -XGET 'http://master:9200/cmz?pretty' 

curl -s -XGET 'http://master:9200/_cat/indices?v'  # 查看所有索引
curl -s -XGET 'http://master:9200/_cat/nodes?v'   # 集群的节点列表

-s 表示静默输出,|json 表示打印的输出已json方式显示 npm install -g json 才能使用|json -d 后面是body内容

详细操作
root@master:~# curl -s -XPUT 'http://master:9200/cmz/' -d '{
>     "settings":{
>         "index":{
>             "number_of_shards":5,
>             "number_of_replicas":1
>         }
>     }
> }' | json
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "cmz"
}

查看索引
root@master:~# curl -s -XGET 'http://master:9200/cmz/' | json
{
  "cmz": {
    "aliases": {},
    "mappings": {},
    "settings": {
      "index": {
        "creation_date": "1570685776669",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "Su_uUwATQ_utbTGrI_Wplg",
        "version": {
          "created": "5061699"
        },
        "provided_name": "cmz"
      }
    }
  }
}

root@master:~# curl -s -XGET 'http://master:9200/_cat/indices?v'   # 查看所有索引
health status index               uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   filebeat-2019.10.10 -Zs0dsJKRQG7WJABl1EDbQ   5   1      35342            0     14.4mb          7.2mb
green  open   .kibana             tPbKD15vSjmVmqv237npuQ   1   1          2            0     35.1kb         17.6kb
green  open   cmz                 Su_uUwATQ_utbTGrI_Wplg   5   1          0            0      1.5kb           810b
green  open   accounts            TSMxoo8DTvyr8OlpGGb_QA   5   1          2            0     18.7kb          9.3kb 

health:
    无论何时我们请求集群健康时,我们会得到green, yellow, 或者 red 这三种状态。
    Green : everything is good(一切都很好)(所有功能正常)
    Yellow : 所有数据都是可用的,但有些副本还没有分配(所有功能正常)
    Red : 有些数据不可用(部分功能正常)
index:  索引
    filebeat-2019.10.10  索引名
    cmz                  索引名
    accounts             索引名
uuid: 唯一标识uuid
rep: 副本数

root@master:~# curl -s -XGET 'http://master:9200/_cat/nodes?v'
ip           heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.2.22           10          86   0    0.00    0.00     0.00 mdi       -      slave2
192.168.2.20           26          87   0    0.00    0.04     0.00 mdi       *      master
192.168.2.21           18          86   0    0.01    0.05     0.01 mdi       -      slave1

2. 索引查找

2.1 初始化数据

curl -XDELETE 'master:9200/cmz/'

curl -s -XPOST 'master:9200/cmz/person/1' -d '
{
  "name":"夏天",
  "sex":"男",
  "job": "销售"
}'|json

curl -s  -XPOST 'master:9200/cmz/person/2' -d '
{
  "name":"琳琳",
  "sex":"男",
  "job": "研发"
}'|json

curl -s  -XPOST 'master:9200/cmz/person/3' -d '
{
  "name":"刘清",
  "sex":"女",
  "job": "财务"
}'|json

curl -s  -XPOST 'master:9200/cmz/person/4' -d '
{
  "name":"琳娜",
  "sex":"女",
  "job": "HR"
}'|json
详细操作
root@master:~# curl -XDELETE 'master:9200/cmz/'
url -s  -XPOST 'master:9200/cmz/person/3' -d '
{
  "name":"刘清",
  "sex":"女",
  "job": "财务"
}'|json

curl -s  -XPOST 'master:9200/cmz/person/4' -d '
{
  "name":"琳娜",
  "sex":"女",
  "job": "HR"
}'|json{"acknowledged":true}root@master:~# 
root@master:~# curl -s -XPOST 'master:9200/cmz/person/1' -d '
> {
>   "name":"夏天",
>   "sex":"男",
>   "job": "销售"
> }'|json

{
  "_index": "cmz",
  "_type": "person",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "created": true
}
root@master:~# 
root@master:~# curl -s  -XPOST 'master:9200/cmz/person/2' -d '
> {
>   "name":"琳琳",
>   "sex":"男",
>   "job": "研发"
> }'|json
{
  "_index": "cmz",
  "_type": "person",
  "_id": "2",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "created": true
}
root@master:~# 
root@master:~# curl -s  -XPOST 'master:9200/cmz/person/3' -d '
> {
>   "name":"刘清",
>   "sex":"女",
>   "job": "财务"
> }'|json
{
  "_index": "cmz",
  "_type": "person",
  "_id": "3",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "created": true
}
root@master:~# 
root@master:~# curl -s  -XPOST 'master:9200/cmz/person/4' -d '
> {
>   "name":"琳娜",
>   "sex":"女",
>   "job": "HR"
> }'|json
{
  "_index": "cmz",
  "_type": "person",
  "_id": "4",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "created": true
}

索引操作

# 查看单个索引详情【查询cmz索引的详情】
curl -s -XGET 'http://master:9200/cmz/_settings?pretty'

# 查看多个索引详情,索引之间用英文逗号隔开【查询cmz,accounts索引的详情】
curl -s -XGET 'http://master:9200/cmz,accounts/_settings?pretty'

# 查看所有索引
curl -s -XGET 'http://master:9200/_all/_settings?pretty'

通过GET带上参数_settings可以获取该索引的详细信息

详细操作
root@master:~# curl -s -XGET 'http://master:9200/_all/_settings?pretty'
{
  "filebeat-2019.10.10" : {
    "settings" : {
      "index" : {
        "mapping" : {
          "total_fields" : {
            "limit" : "10000"
          }
        },
        "refresh_interval" : "5s",
        "number_of_shards" : "5",
        "provided_name" : "filebeat-2019.10.10",
        "creation_date" : "1570675693796",
        "number_of_replicas" : "1",
        "uuid" : "-Zs0dsJKRQG7WJABl1EDbQ",
        "version" : {
          "created" : "5061699"
        }
      }
    }
  },
  ".kibana" : {
    "settings" : {
      "index" : {
        "number_of_shards" : "1",
        "provided_name" : ".kibana",
        "mapper" : {
          "dynamic" : "false"
        },
        "creation_date" : "1570601461473",
        "number_of_replicas" : "1",
        "uuid" : "tPbKD15vSjmVmqv237npuQ",
        "version" : {
          "created" : "5061699"
        }
      }
    }
  },
  "accounts" : {
    "settings" : {
      "index" : {
        "creation_date" : "1570602387056",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "TSMxoo8DTvyr8OlpGGb_QA",
        "version" : {
          "created" : "5061699"
        },
        "provided_name" : "accounts"
      }
    }
  },
  "cmz" : {
    "settings" : {
      "index" : {
        "creation_date" : "1570687081844",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "OBeF5_J9TYyUQiCG_QUkrw",
        "version" : {
          "created" : "5061699"
        },
        "provided_name" : "cmz"
      }
    }
  }
}

root@master:~# curl -s -XGET 'http://master:9200/cmz/_settings?pretty'
{
  "cmz" : {
    "settings" : {
      "index" : {
        "creation_date" : "1570687081844",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "OBeF5_J9TYyUQiCG_QUkrw",
        "version" : {
          "created" : "5061699"
        },
        "provided_name" : "cmz"
      }
    }
  }
}
root@master:~# curl -s -XGET 'http://master:9200/cmz,accounts/_settings?pretty'
{
  "accounts" : {
    "settings" : {
      "index" : {
        "creation_date" : "1570602387056",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "TSMxoo8DTvyr8OlpGGb_QA",
        "version" : {
          "created" : "5061699"
        },
        "provided_name" : "accounts"
      }
    }
  },
  "cmz" : {
    "settings" : {
      "index" : {
        "creation_date" : "1570687081844",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "OBeF5_J9TYyUQiCG_QUkrw",
        "version" : {
          "created" : "5061699"
        },
        "provided_name" : "cmz"
      }
    }
  }
}

3. 基本操作-创建

3.1 创建索引

|-linux 命令
|     |- 静默输出  
|     |    |- post请求
|     |    |                 |-索引名
|     |    |                 |    |- type名
|     |    |                 |    |  |- 文档id [不配置就自动生成]
curl -s -XPOST 'master:9200/cmz/book/2' -d '
{
  "title":"Python自动化",
  "author":{
      "name":"蔡猛芝",
      "age":"30"
  },
  "publisher":"南京出版社",
  "publish_date": "2017-05-08",
  "price": "50.88"
}'|json
详细操作
root@master:~# curl -s -XPOST 'master:9200/cmz/book/2' -d '
> {
>   "title":"Python自动化",
>   "author":{
>       "name":"蔡猛芝",
>       "age":"30"
>   },
>   "publisher":"南京出版社",
>   "publish_date": "2017-05-08",
>   "price": "50.88"
> }'|json
{
  "_index": "cmz",
  "_type": "book",
  "_id": "2",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "created": true
}

root@master:~# curl -s -XPOST 'master:9200/cmz/book/' -d '
> {
>   "title":"K8S",
>   "author":{
>       "name":"蔡猛芝",
>       "age":"20"
>   },
>   "publisher":"南京出版社",
>   "publish_date": "2017-05-08",
>   "price": "66.88"
> }'|json
{
  "_index": "cmz",
  "_type": "book",
  "_id": "AW20V2rzDaJz3wZRR9wI",  # 不指定id,会随机生成一个
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "created": true
}

4. 基本操作-查找

4.1 通过id 查询

  查询某条或者某几条等完整信息

curl -s -XGET 'master:9200/cmz/book/1?pretty'
curl -s -XGET 'master:9200/cmz/book/2?pretty'
curl -s -XGET 'master:9200/cmz/book/AW20V2rzDaJz3wZRR9wI?pretty'

详细操作
root@master:~# curl -s -XGET 'master:9200/cmz/book/1?pretty'
{
  "_index" : "cmz",
  "_type" : "book",
  "_id" : "1",
  "_version" : 2,
  "found" : true,
  "_source" : {
    "title" : "Python自动化",
    "author" : {
      "name" : "蔡猛芝",
      "age" : "30"
    },
    "publisher" : "南京出版社",
    "publish_date" : "2017-05-08",
    "price" : "50.88"
  }
}
root@master:~# curl -s -XGET 'master:9200/cmz/book/2?pretty'
{
  "_index" : "cmz",
  "_type" : "book",
  "_id" : "2",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "title" : "Python自动化",
    "author" : {
      "name" : "蔡猛芝",
      "age" : "30"
    },
    "publisher" : "南京出版社",
    "publish_date" : "2017-05-08",
    "price" : "50.88"
  }
}
root@master:~# curl -s -XGET 'master:9200/cmz/book/AW20V2rzDaJz3wZRR9wI?pretty'
{
  "_index" : "cmz",
  "_type" : "book",
  "_id" : "AW20V2rzDaJz3wZRR9wI",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "title" : "K8S",
    "author" : {
      "name" : "蔡猛芝",
      "age" : "20"
    },
    "publisher" : "南京出版社",
    "publish_date" : "2017-05-08",
    "price" : "66.88"
  }
}
详细操作

``` root@master:~# curl -s -XGET 'master:9200/cmz/book/1?_source=title'|json { "_index": "cmz", "_type": "book", "_id": "1", "_version": 2, "found": true, "_source": { "title": "Python自动化" } } root@master:~# curl -s -XGET 'master:9200/cmz/book/1?_source=title,price'|json { "_index": "cmz", "_type": "book", "_id": "1", "_version": 2, "found": true, "_source": { "price": "50.88", "title": "Python自动化" } } root@master:~# curl -s -XGET 'master:9200/cmz/book/2?_source=author'|json { "_index": "cmz", "_type": "book", "_id": "2", "_version": 1, "found": true, "_source": { "author": { "name": "蔡猛芝", "age": "30" } } } root@master:~# curl -s -XGET 'master:9200/cmz/book/2?_source=author.age'|json { "_index": "cmz", "_type": "book", "_id": "2", "_version": 1, "found": true, "_source": { "author": { "age": "30" } } }

root@master:~# curl -s -XGET 'master:9200/cmz/book/AW20V2rzDaJz3wZRR9wI?_source=price'|json { "_index": "cmz", "_type": "book", "_id": "AW20V2rzDaJz3wZRR9wI", "_version": 1, "found": true, "_source": { "price": "66.88" } }

```

5. 基本操作-更新

  更新有update和put两种方式更新数据,推荐update。update能针对你要的字段进行单独设置,而put必须输入所有。

5.1 put更新

curl -s -XGET 'master:9200/cmz/book/2' 
curl -s -XPUT 'master:9200/cmz/book/2' -d '
{
  "title":"Python自动化",
  "author":{
      "name":"蔡猛芝",
      "age":"30"
  },
  "publisher":"南京出版社",
  "publish_date": "2017-05-08",
  "price": "100"
}'|json
curl -s -XGET 'master:9200/cmz/book/2'

我只修改了价格

详细操作
root@master:~# curl -s -XGET 'master:9200/cmz/book/2' 
"
}'|json
curl -s -XGET 'master:9200/cmz/book/2'{"_index":"cmz","_type":"book","_id":"2","_version":1,"found":true,"_source":
{
  "title":"Python自动化",
  "author":{
      "name":"蔡猛芝",
      "age":"30"
  },
  "publisher":"南京出版社",
  "publish_date": "2017-05-08",
  "price": "50.88"
}}root@master:~# curl -s -XPUT 'master:9200/cmz/book/2' -d '
> {
>   "title":"Python自动化",
>   "author":{
>       "name":"蔡猛芝",
>       "age":"30"
>   },
>   "publisher":"南京出版社",
>   "publish_date": "2017-05-08",
>   "price": "100"
> }'|json
{
  "_index": "cmz",
  "_type": "book",
  "_id": "2",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "created": false
}
root@master:~# curl -s -XGET 'master:9200/cmz/book/2'
{"_index":"cmz","_type":"book","_id":"2","_version":2,"found":true,"_source":
{
  "title":"Python自动化",
  "author":{
      "name":"蔡猛芝",
      "age":"30"
  },
  "publisher":"南京出版社",
  "publish_date": "2017-05-08",
  "price": "100"
}}
详细操作
root@master:~# curl -s -XGET 'master:9200/cmz/book/2?pretty'
{
  "_index" : "cmz",
  "_type" : "book",
  "_id" : "2",
  "_version" : 4,
  "found" : true,
  "_source" : {
    "title" : "Python自动化",
    "author" : {
      "name" : "蔡猛芝",
      "age" : "30"
    },
    "publisher" : "南京出版社",
    "publish_date" : "2017-05-08",
    "price" : 100
  }
}
root@master:~# curl -s -XPOST 'master:9200/cmz/book/2/_update' -d '{
>     "doc":{
>         "price":200
>     }
> }'|json
{
  "_index": "cmz",
  "_type": "book",
  "_id": "2",
  "_version": 5,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  }
}
root@master:~# curl -s -XGET 'master:9200/cmz/book/2?pretty'
{
  "_index" : "cmz",
  "_type" : "book",
  "_id" : "2",
  "_version" : 5,
  "found" : true,
  "_source" : {
    "title" : "Python自动化",
    "author" : {
      "name" : "蔡猛芝",
      "age" : "30"
    },
    "publisher" : "南京出版社",
    "publish_date" : "2017-05-08",
    "price" : 200
  }
}

6. 基本操作-删除

6.1 删除type下数据

可以删除type下一条记录,也就是删除book下一本书。

curl -s -XGET 'master:9200/cmz/book/1?pretty'
curl -s -XDELETE 'master:9200/cmz/book/1'|json
curl -s -XGET 'master:9200/cmz/book/1?pretty'

详细操作
root@master:~# curl -s -XGET 'master:9200/cmz/book/1?pretty'
{
  "_index" : "cmz",
  "_type" : "book",
  "_id" : "1",
  "_version" : 2,
  "found" : true,
  "_source" : {
    "title" : "Python自动化",
    "author" : {
      "name" : "蔡猛芝",
      "age" : "30"
    },
    "publisher" : "南京出版社",
    "publish_date" : "2017-05-08",
    "price" : "50.88"
  }
}
root@master:~# curl -s -XDELETE 'master:9200/cmz/book/1'
{"found":true,"_index":"cmz","_type":"book","_id":"1","_version":3,"result":"deleted","_shards":{"total":2,"successful":2,"failed":0}}root@master:~# 
root@master:~# curl -s -XGET 'master:9200/cmz/book/1?pretty'
{
  "_index" : "cmz",
  "_type" : "book",
  "_id" : "1",
  "found" : false
}

6.2 删除type

curl -s -XDELETE 'master:9200/cmz/book/_delete_by_query'|json

有问题,查询后补充

常见错误
root@master:~# curl -s -X DELETE 'master:9200/cmz/book?pretty'
No handler found for uri [/cmz/book?pretty] and method [DELETE]

首先要说明的是现在的Elasticsearch已经不支持删除一个type
所以现在如果想要删除type有两种选择: 
1.重新设置index。 
2.删除type下的所有数据

6.3 删除索引

curl -s -XGET 'master:9200/cmz?pretty'
curl -s -XDELETE 'master:9200/cmz'|json
curl -s -XGET 'master:9200/cmz?pretty'
详细操作
root@master:~# curl -s -XGET 'master:9200/cmz?pretty'
{
  "cmz" : {
    "aliases" : { },
    "mappings" : {
      "book" : {
        "properties" : {
          "author" : {
            "properties" : {
              "age" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              },
              "name" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              }
            }
          },
          "price" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "publish_date" : {
            "type" : "date"
          },
          "publisher" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "title" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1570692045472",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "wdUw4fRuQL6VPdX6BPefvA",
        "version" : {
          "created" : "5061699"
        },
        "provided_name" : "cmz"
      }
    }
  }
}
root@master:~# curl -s -XDELETE 'master:9200/cmz'|json
{
  "acknowledged": true
}
root@master:~# curl -s -XGET 'master:9200/cmz?pretty'
{
  "error" : {
    "root_cause" : [
      {
        "type" : "index_not_found_exception",
        "reason" : "no such index",
        "resource.type" : "index_or_alias",
        "resource.id" : "cmz",
        "index_uuid" : "_na_",
        "index" : "cmz"
      }
    ],
    "type" : "index_not_found_exception",
    "reason" : "no such index",
    "resource.type" : "index_or_alias",
    "resource.id" : "cmz",
    "index_uuid" : "_na_",
    "index" : "cmz"
  },
  "status" : 404
}