ElasticSearch的常用API

1、在服务器上怎么查ES的信息

# 通过使用_cat可以查看支持的命令
### curl localhost:9200/_cat
eg:

/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates

# verbose
# 每个命令都支持使用?v参数,来显示详细的信息:
# $ curl localhost:9200/_cat/master?v
id                     host      ip        node
QG6QrX32QSi8C3-xQmrSoA 127.0.0.1 127.0.0.1 Manslaughter


# help
# 每个命令都支持使用help参数,来输出可以显示的列:
# $ curl localhost:9200/_cat/master?help
id   |   | node id
host | h | host name
ip   |   | ip address
node | n | node name


# headers
# 通过h参数,可以指定输出的字段:**
# $ curl localhost:9200/_cat/master?v
id                     host      ip        node
QG6QrX32QSi8C3-xQmrSoA 127.0.0.1 127.0.0.1 Manslaughter

# $ curl localhost:9200/_cat/master?h=host,ip,node
127.0.0.1 127.0.0.1 Manslaughter

# 数字类型的格式化
# 很多的命令都支持返回可读性的大小数字,比如使用mb或者kb来表示。
# [es@localhost config]$ curl localhost:9200/_cat/indices
green open customer2 9im2_3hIT9-chDznVXwOtg 1 1 0 0 566b  283b
green open customer1 uaA_cmxYSU-76jT93-hs0w 1 1 1 0  7kb 3.5kb

# bytes: 数值列还原为原始值. 如diskSize, 默认转为以kb/mb/gb表示, 打开后还原为原始值
# [es@localhost config]$ curl localhost:9200/_cat/indices?bytes=b
green open customer2 9im2_3hIT9-chDznVXwOtg 1 1 0 0  566  283
green open customer1 uaA_cmxYSU-76jT93-hs0w 1 1 1 0 7260 3630


## shell
# 创建customer索引
curl -X PUT 'localhost:9200/customer?pretty'
eg:
[es@localhost config]$ curl -X PUT 'localhost:9200/customer?pretty'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "customer"
}


# 删除索引
curl -X DELETE localhost:9200/customer?pretty
eg:
[es@localhost config]$ curl -X DELETE localhost:9200/customer?pretty
{
  "acknowledged" : true
}


#获取全部索引【列出所有索引, 并展示索引基本信息】
curl localhost:9200/_cat/indices?v
eg:
[es@localhost config]$ curl localhost:9200/_cat/indices?v
health status index     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   customer1 uaA_cmxYSU-76jT93-hs0w   1   1          1            0        7kb          3.5kb
green  open   customer  FTTKAgKKQAWpBI78aTUO9Q   1   1          1            0     15.7kb          7.8kb
--health 					索引的健康状态
--status					索引状态信息open/close
--index 					索引名
--uuid						索引的UUID
--pri 						索引主分片数量
--rep 						索引复制分片数量
--store.size 			索引主分片 复制分片 总占用存储空间
--pri.store.size 	索引总占用空间, 不计算复制分片 占用空间


#查看集群健康状态简短显示
curl localhost:9200/_cat/health?v
eg:
[es@localhost config]$ curl localhost:9200/_cat/health?v
epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1592809420 07:03:40  elasticsearch green           2         2      4   2    0    0        0             0                  -                100.0%


#查看集群健康状态详细显示
curl localhost:9200/_cluster/health?pretty
eg:
[es@localhost config]$ curl localhost:9200/_cluster/health?pretty
{
  "cluster_name" : "elasticsearch",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "active_primary_shards" : 2,
  "active_shards" : 4,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}


# 查看节点列表【列出所有node, 并展示node所在机器的运行状态信息.】
curl localhost:9200/_cat/nodes?v
eg:
[es@localhost config]$ curl localhost:9200/_cat/nodes?v
ip             heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.21.131           17          94  13    0.40    0.32     0.17 dilm      *      localhost.localdomain
192.168.21.131           12          93  15    0.40    0.32     0.17 dilm      -      localhost.localdomain
--ip						ip
--heap.percent 	堆内存占用百分比
--ram.percent 	内存占用百分比
--cpu 					CPU占用百分比
--master 				*表示节点是集群中的主节点、其他表示从节点
--name 					节点名


#查看分片
curl localhost:9200/_cat/shards
eg:
[es@localhost config]$ curl localhost:9200/_cat/shards
customer1 0 p STARTED 1 3.5kb 192.168.21.131 localhost.localdomain
customer1 0 r STARTED 1 3.5kb 192.168.21.131 localhost.localdomain
customer  0 p STARTED 1 3.5kb 192.168.21.131 localhost.localdomain
customer  0 r STARTED 1 3.5kb 192.168.21.131 localhost.localdomain
--prirep p表示该分片是主分片, r 表示该分片是复制分片

#各节点机器存储信息【列出所有node, 并展示所属机器的配置信息】
curl localhost:9200/_cat/allocation?v
[es@localhost config]$ curl localhost:9200/_cat/allocation?v
shards disk.indices disk.used disk.avail disk.total disk.percent host           ip             node
     2        3.8kb     6.5gb     40.4gb     46.9gb           13 192.168.21.131 192.168.21.131 localhost.localdoma
     2        3.8kb     6.5gb     40.4gb     46.9gb           13 192.168.21.131 192.168.21.131 localhost.localdoma
--shards 				节点说承载的分片数
--disk.indices 	索引占用的空间大小
--disk.used 		节点所在机器已使用磁盘空间
--disk.avail 		节点所在机器可用磁盘空间
--disk.total 		节点所在机器总磁盘空间
--disk.percent 	节点所在机器磁盘空间占用百分比
--host					host
--ip 						节点所属机器IP地址
--node 					节点名


#获取所有的type
curl -X GET http://localhost:9200/_mapping?pretty
eg:
[es@localhost config]$ curl -X GET http://localhost:9200/_mapping?pretty
{
  "customer" : {
    "mappings" : {
      "properties" : {
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  },
  "customer1" : {
    "mappings" : {
      "properties" : {
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}
# ```
### 集群
# 查看集群状态
curl localhost:9200/_cat/health?v

# shell
#插入一条数据id为1的文档,如果已经存在该ID则替换该文档
curl -X PUT "localhost:9200/customer/_doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'{"name": "John Doe"}'
eg:
[es@localhost config]$ curl -X PUT "localhost:9200/customer6/_doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'{"name": "John Doe"}'
{
  "_index" : "customer6",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
[es@localhost config]$ curl -X PUT "localhost:9200/customer6/_doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'{"name": "John Doe"}'
{
  "_index" : "customer6",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}


# 插入一个文档,不指定ID,ID自动生成
curl -X POST "localhost:9200/customer/_doc?pretty&pretty" -H 'Content-Type: application/json' -d'{"name": "Jane Doe"}'
eg:
[es@localhost config]$ curl -X POST "localhost:9200/customer8/_doc?pretty&pretty" -H 'Content-Type: application/json' -d'{"name": "Jane Doe"}'
{
  "_index" : "customer8",
  "_type" : "_doc",
  "_id" : "7Wph23IB-KZb9yvuVPEj",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
[es@localhost config]$ curl -X POST "localhost:9200/customer8/_doc?pretty&pretty" -H 'Content-Type: application/json' -d'{"name": "Jane Doe"}'
{
  "_index" : "customer8",
  "_type" : "_doc",
  "_id" : "7mph23IB-KZb9yvuv_Gx",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}



# 查询id为1的文档
curl -X GET localhost:9200/customer/_doc/1?pretty
eg:
[es@localhost config]$ curl -X GET localhost:9200/customer8/_doc/7mph23IB-KZb9yvuv_Gx?pretty
{
  "_index" : "customer8",
  "_type" : "_doc",
  "_id" : "7mph23IB-KZb9yvuv_Gx",
  "_version" : 1,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Jane Doe"
  }
}
[es@localhost config]$ curl -X GET localhost:9200/customer8/_doc/7Wph23IB-KZb9yvuVPEj?pretty
{
  "_index" : "customer8",
  "_type" : "_doc",
  "_id" : "7Wph23IB-KZb9yvuVPEj",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Jane Doe"
  }
}


# 查询一个索引的所有文档
curl localhost:9200/customer/_search?pretty
eg:
[es@localhost config]$ curl localhost:9200/customer8/_search?pretty
{
  "took" : 30,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "customer8",
        "_type" : "_doc",
        "_id" : "7Wph23IB-KZb9yvuVPEj",
        "_score" : 1.0,
        "_source" : {
          "name" : "Jane Doe"
        }
      },
      {
        "_index" : "customer8",
        "_type" : "_doc",
        "_id" : "7mph23IB-KZb9yvuv_Gx",
        "_score" : 1.0,
        "_source" : {
          "name" : "Jane Doe"
        }
      }
    ]
  }
}


# 条件查询
curl "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty"
eg:
[es@localhost config]$ curl "localhost:9200/customer8/_search?q=*&pretty"
{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "customer8",
        "_type" : "_doc",
        "_id" : "7Wph23IB-KZb9yvuVPEj",
        "_score" : 1.0,
        "_source" : {
          "name" : "Jane Doe"
        }
      },
      {
        "_index" : "customer8",
        "_type" : "_doc",
        "_id" : "7mph23IB-KZb9yvuv_Gx",
        "_score" : 1.0,
        "_source" : {
          "name" : "Jane Doe"
        }
      }
    ]
  }
}


# 条件查询,_source查询的字段,size查询条数,sort排序,query查询条件
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} },
  "from":5,
  "size":5,
  "sort":{
  	"balance":{"order":"desc"}
    },
  "_source":["_id","name"]
}'

# 模糊查询,match中跟要模糊查询的字段,模糊的值在value中用空格隔开,用match_phrase它返回地址中包含短语"mill lane"的所有帐户
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": { "match": {"address":"mill lane"} },
  "from":5,
  "size":5,
  "sort":{
  	"balance":{"order":"desc"}
    },
  "_source":["_id","name"]
}'

# 该bool must子句指定了所有必须为true的查询才能将文档视为匹配项。相反,此示例组成两个match查询,并返回地址中包含“ mill”或“ lane”的所有帐户:
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}'

# 该bool should子句指定了一个查询列表,对于将文档视为匹配项,其中任一查询都必须为true。本示例组成两个match查询,并返回地址中既不包含“ mill”也不包含“ lane”的所有帐户
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "should": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}'

# 该bool must_not子句指定了一个查询列表,对于被视为匹配的文档,没有一个查询列表必须为真。我们可以在查询中同时组合must,should和must_not子句bool。此外,我们可以bool在任何这些bool子句中组成查询,以模仿任何复杂的多级布尔逻辑
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must_not": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}'

# 我们可以在查询中同时组合must,should和must_not子句bool。此外,我们可以bool在任何这些bool子句中组成查询,以模仿任何复杂的多级布尔逻辑。
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "age": "40" } }
      ],
      "must_not": [
        { "match": { "state": "ID" } }
      ]
    }
  }
}'

# filter子句,这些子句允许我们使用查询来限制将要与其他子句匹配的文档,而无需更改分数的计算方式。作为示例,让我们介绍一下rangequery,它允许我们按一定范围的值过滤文档。通常用于数字或日期过滤
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}'

# 执行聚合,按状态对所有帐户进行分组,然后返回按计数递减排序的前10个(默认)状态(也是默认)
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword"
      }
    }
  }
}'



# 更新ID为1的文档
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty&pretty" -H 'Content-Type: application/json' -d'{"doc":{"name": "Jane Doe","age":"18"}}'

# 批量更新文档
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }'

# 删除ID为2的文档
curl -X DELETE localhsot:9200/customer/_doc/2?pretty

# 批量删除和更新
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}'

# 批量将accounts.json文件中的数据导入到bank索引中
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json"




curl -X POST "localhost:9200/twitter/_update_by_query?conflicts=proceed&pretty" -H 'Content-Type: application/json' -d
{
  "query": { 
    "term": {
      "user": "kimchy"
    }
  }
}