elasticsearch常用命令

2023-11-07

curl -X :///

:REST风格的语法谓词
  :节点ip
  :节点端口号,默认9200
  :索引名
  :索引类型
  :操作对象的ID号

curl localhost:9200/_cat

=.=
/_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

(1)elasticsearch 查看集群统计信息

 curl -XGET 'http://localhost:9200/_cluster/stats?pretty'   

(2)elasticsearch 查看所有索引

 curl 'localhost:9200/_cat/indices?v'       

(3)elasticsearch 查看集群的节点列表

 curl 'localhost:9200/_cat/nodes?v'  

(4)elasticsearch 检测集群是否健康

 curl 'localhost:9200/_cat/health?v' 

(5)elasticsearch 创建索引

 curl -XPUT 'localhost:9200/customer?pretty'

(6)elasticsearch 插入数据

 curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '

{
   “name”: “John Doe”
  }’

(7)elasticsearch 获取数据

 curl -XGET 'localhost:9200/customer/external/1?pretty'

 获取customer索引下类型为external,id为1的数据,pretty参数表示返回结果格式美观。

(8)elasticsearch 删除索引

curl -XDELETE 'localhost:9200/customer?pretty' 

(9)elasticsearch 修改数据

curl -XPUT ‘localhost:9200/customer/external/1?pretty’ -d ’
  {
   “name”: “John Doe”
  }’
  curl -XPUT ‘localhost:9200/customer/external/1?pretty’ -d ’
  {
   “name”: “Jane Doe”
  }’
先新增id为1,name为John Doe的数据,然后将id为1的name修改为Jane Doe。

(10)elasticsearch 更新数据

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '

{
   “doc”: { “name”: “Jane Doe” }
  }’

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '

{
   “doc”: { “name”: “Jane Doe”, “age”: 20 }
  }’

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '

{
   “script” : “ctx._source.age += 5”
  }’

(11)elasticsearch 删除数据

  curl -XDELETE 'localhost:9200/customer/external/2?pretty'
  将执行删除Customer中ID为2的数据

  curl -XPUT 'localhost:9200/customer'             //创建索引


  curl -XPUT 'localhost:9200/customer/external/1'-d '    //插入数据

{
“name”: “John Doe”
}’

curl ‘localhost:9200/customer/external/1’//查询数据

curl -XDELETE ‘localhost:9200/customer’//删除索引

(12)elasticsearch 批处理

批量操作中执行创建索引:

curl -XPOST ‘localhost:9200/customer/external/_bulk?pretty’ -d ’
  {“index”:{"_id":“1”}}
  {“name”: “John Doe” }
  {“index”:{"_id":“2”}}
  {“name”: “Jane Doe” }
  ’
  下面语句批处理执行更新id为1的数据然后执行删除id为2的数据

curl -XPOST ‘localhost:9200/customer/external/_bulk?pretty’ -d ’
  {“update”:{"_id":“1”}}
  {“doc”: { “name”: “John Doe becomes Jane Doe” } }
  {“delete”:{"_id":“2”}}

(13)elasticsearch 导入数据集

 curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"

curl ‘localhost:9200/_cat/indices?v’ 查看

(14)elasticsearch 查询数据

 curl 'localhost:9200/bank/_search?q=*&pretty'

 返回所有bank中的索引数据。其中 q=*  表示匹配索引中所有的数据。

 等价于:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: { “match_all”: {} }
  }’

 匹配所有数据,但只返回1个:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’

{
  “query”: { “match_all”: {} },
   “size”: 1
  }’
  注意:如果siez不指定,则默认返回10条数据。

  curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’

{
  “query”: { “match_all”: {} },
   “from”: 10,
   “size”: 10
  }’

返回从11到20的数据。(索引下标从0开始)
  curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’

{
   “query”: { “match_all”: {} },
  “sort”: { “balance”: { “order”: “desc” } }
  }’

上述示例匹配所有的索引中的数据,按照balance字段降序排序,并且返回前10条(如果不指定size,默认最多返回10条)。

(15)elasticsearch 搜索

 返回两个字段(account_number balance)

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: { “match_all”: {} },
   “_source”: [“account_number”, “balance”]
  }’

 返回account_number 为20 的数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: { “match”: { “account_number”: 20 } }
  }’

返回address中包含mill的所有数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: { “match”: { “address”: “mill” } }
  }’

返回地址中包含mill或者lane的所有数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
  “query”: { “match”: { “address”: “mill lane” } }
  }’

和上面匹配单个词语不同,下面这个例子是多匹配(match_phrase短语匹配),返回地址中包含短语 “mill lane”的所有数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: { “match_phrase”: { “address”: “mill lane” } }
  }’

 以下是布尔查询,布尔查询允许我们将多个简单的查询组合成一个更复杂的布尔逻辑查询。

这个例子将两个查询组合,返回地址中含有mill和lane的所有记录数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: {
   “bool”: {
   “must”: [
   { “match”: { “address”: “mill” } },
   { “match”: { “address”: “lane” } }
   ]
  }
  }
  }’
  上述例子中,must表示所有查询必须都为真才被认为匹配。

相反, 这个例子组合两个查询,返回地址中含有mill或者lane的所有记录数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
  “query”: {
   “bool”: {
   “should”: [
   { “match”: { “address”: “mill” } },
   { “match”: { “address”: “lane” } }
   ]
   }
  }
  }’

上述例子中,bool表示查询列表中只要有任何一个为真则认为匹配。

下面例子组合两个查询,返回地址中既没有mill也没有lane的所有数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: {
   “bool”: {
   “must_not”: [
   { “match”: { “address”: “mill” } },
   { “match”: { “address”: “lane” } }
  ]
  }
  }
  }’
  上述例子中,must_not表示查询列表中没有为真的(也就是全为假)时则认为匹配。

我们可以组合must、should、must_not来实现更加复杂的多级逻辑查询。

下面这个例子返回年龄大于40岁、不居住在ID的所有数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: {
   “bool”: {
   “must”: [
   { “match”: { “age”: “40” } }
   ],
   “must_not”: [
   { “match”: { “state”: “ID” } }
  ]
  }
   }
  }’

(16) elasticsearch 过滤filter(查询条件设置)

下面这个例子使用了布尔查询返回balance在20000到30000之间的所有数据。
  curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
     “query”: {
     “bool”: {
     “must”: { “match_all”: {} },
     “filter”: {
    “range”: {
  “balance”: {
   “gte”: 20000,
   “lte”: 30000
   }
   }
   }
   }
  }
  }’

(17) elasticsearch 聚合 Aggregations

下面这个例子: 将所有的数据按照state分组(group),然后按照分组记录数从大到小排序,返回前十条(默认):

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
  “size”: 0,
   “aggs”: {
   “group_by_state”: {
   “terms”: {
    “field”: “state”
   }
   }
  }
  }’

下面这个实例按照state分组,降序排序,返回balance的平均值:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
  “size”: 0,
  “aggs”: {
   “group_by_state”: {
   “terms”: {
   “field”: “state”
   },
   “aggs”: {
   “average_balance”: {
   “avg”: {
   “field”: “balance”
   }
   }
   }
  }
  }
  }’

(18)elasticsearch 取得某个索引中某个字段中的所有出现过的值

这种操作类似于使用SQL的SELECT UNIQUE语句。当需要获取某个字段上的所有可用值时,可以使用terms聚合查询完成:

GET /index_streets/_search?search_type=count
{
“aggs”: {
“street_values”: {
“terms”: {
“field”: “name.raw”,
“size”: 0
}
}
}
}

因为目标是得到name字段上的所有出现过的值,因此search_type被设置为了count,这样在返回的响应中不会出现冗长的hits部分。另外,查询的目标字段的索引类型需要设置为not_analyzed。所以上面的field指定的是name.raw。

得到的响应如下所示:

{
“took”: 23,
“timed_out”: false,
“_shards”: {
“total”: 5,
“successful”: 5,
“failed”: 0
},
“hits”: {
“total”: 7445,
“max_score”: 0,
“hits”: []
},
“aggregations”: {
“street_values”: {
“doc_count_error_upper_bound”: 0,
“sum_other_doc_count”: 0,
“buckets”: [
{
“key”: “江苏路”,
“doc_count”: 29
},
{
“key”: “南京东路”,
“doc_count”: 28
},


(19)elasticsearch 取得某个索引/类型下某个字段中出现的不同值的个数

这种操作类似于使用SQL的select count( * ) from (select distinct * from table)语句。当需要获取某个字段上的出现的不同值的个数时,可以使用cardinality聚合查询完成:

GET /index_streets/_search?search_type=count
{
“aggs”: {
“uniq_streets”: {
“cardinality”: {
“field”: “name.raw”
}
}
}
}

因为目标是得到name字段上的所有出现过的值,因此search_type被设置为了count,这样在返回的响应中不会出现冗长的hits部分。另外,查询的目标字段如果是字符串类型的,那么其索引类型需要设置为not_analyzed。所以上面的field指定的是name.raw。

得到的响应如下所示:

{
“took”: 96,
“timed_out”: false,
“_shards”: {
“total”: 1,
“successful”: 1,
“failed”: 0
},
“hits”: {
“total”: 4136543,
“max_score”: 0,
“hits”: []
},
“aggregations”: {
“uniq_streets”: {
“value”: 1951
}
}
}

(20)elasticsearch 每个命令都支持使用?v参数,来显示详细的信息:

  curl localhost:9200/_cat/master?v 

id host ip node
yBet3cYzQbC68FRzLZDmFg 127.0.0.1 127.0.0.1 lihao

(21)elasticsearch 每个命令都支持使用help参数,来输出可以显示的列:

  curl localhost:9200/_cat/master?help 

id | | node id
host | h | host name
ip | | ip address
node | n | node name

(22)elasticsearch 通过h参数,可以指定输出的字段:

$ curl localhost:9200/_cat/master?v
id host ip node
yBet3cYzQbC68FRzLZDmFg 127.0.0.1 127.0.0.1 lihao

$ curl localhost:9200/_cat/master?h=ip,node
127.0.0.1 lihao

(23) elasticsearch 很多的命令都支持返回可读性的大小数字,比如使用mb或者kb来表示。

$ curl localhost:9200/_cat/indices?v

health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open aaa 5 1 2 0 7.2kb 7.2kb
yellow open logstash-eos-2016.09.01 5 1 297 0 202.3kb 202.3kb
yellow open bank 5 1 1001 1 451.6kb 451.6kb
yellow open website 5 1 2 0 7.8kb 7.8kb
yellow open .kibana 1 1 5 1 26.6kb 26.6kb
yellow open logstash-eos-2016.09.02 5 1 11 0 33.9kb 33.9kb
yellow open test-2016.09.01 5 1 1 0 3.9kb 3.9kb
yellow open testst_index 5 1 0 0 795b 795bcurl -X :///

:REST风格的语法谓词
  :节点ip
  :节点端口号,默认9200
  :索引名
  :索引类型
  :操作对象的ID号

curl localhost:9200/_cat

=.=
/_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

(1)elasticsearch 查看集群统计信息

 curl -XGET 'http://localhost:9200/_cluster/stats?pretty'   

(2)elasticsearch 查看所有索引

 curl 'localhost:9200/_cat/indices?v'       

(3)elasticsearch 查看集群的节点列表

 curl 'localhost:9200/_cat/nodes?v'  

(4)elasticsearch 检测集群是否健康

 curl 'localhost:9200/_cat/health?v' 

(5)elasticsearch 创建索引

 curl -XPUT 'localhost:9200/customer?pretty'

(6)elasticsearch 插入数据

 curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '

{
   “name”: “John Doe”
  }’

(7)elasticsearch 获取数据

 curl -XGET 'localhost:9200/customer/external/1?pretty'

 获取customer索引下类型为external,id为1的数据,pretty参数表示返回结果格式美观。

(8)elasticsearch 删除索引

curl -XDELETE 'localhost:9200/customer?pretty' 

(9)elasticsearch 修改数据

curl -XPUT ‘localhost:9200/customer/external/1?pretty’ -d ’
  {
   “name”: “John Doe”
  }’
  curl -XPUT ‘localhost:9200/customer/external/1?pretty’ -d ’
  {
   “name”: “Jane Doe”
  }’
先新增id为1,name为John Doe的数据,然后将id为1的name修改为Jane Doe。

(10)elasticsearch 更新数据

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '

{
   “doc”: { “name”: “Jane Doe” }
  }’

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '

{
   “doc”: { “name”: “Jane Doe”, “age”: 20 }
  }’

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '

{
   “script” : “ctx._source.age += 5”
  }’

(11)elasticsearch 删除数据

  curl -XDELETE 'localhost:9200/customer/external/2?pretty'
  将执行删除Customer中ID为2的数据

  curl -XPUT 'localhost:9200/customer'             //创建索引


  curl -XPUT 'localhost:9200/customer/external/1'-d '    //插入数据

{
“name”: “John Doe”
}’

curl ‘localhost:9200/customer/external/1’//查询数据

curl -XDELETE ‘localhost:9200/customer’//删除索引

(12)elasticsearch 批处理

批量操作中执行创建索引:

curl -XPOST ‘localhost:9200/customer/external/_bulk?pretty’ -d ’
  {“index”:{"_id":“1”}}
  {“name”: “John Doe” }
  {“index”:{"_id":“2”}}
  {“name”: “Jane Doe” }
  ’
  下面语句批处理执行更新id为1的数据然后执行删除id为2的数据

curl -XPOST ‘localhost:9200/customer/external/_bulk?pretty’ -d ’
  {“update”:{"_id":“1”}}
  {“doc”: { “name”: “John Doe becomes Jane Doe” } }
  {“delete”:{"_id":“2”}}

(13)elasticsearch 导入数据集

 curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"

curl ‘localhost:9200/_cat/indices?v’ 查看

(14)elasticsearch 查询数据

 curl 'localhost:9200/bank/_search?q=*&pretty'

 返回所有bank中的索引数据。其中 q=*  表示匹配索引中所有的数据。

 等价于:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: { “match_all”: {} }
  }’

 匹配所有数据,但只返回1个:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’

{
  “query”: { “match_all”: {} },
   “size”: 1
  }’
  注意:如果siez不指定,则默认返回10条数据。

  curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’

{
  “query”: { “match_all”: {} },
   “from”: 10,
   “size”: 10
  }’

返回从11到20的数据。(索引下标从0开始)
  curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’

{
   “query”: { “match_all”: {} },
  “sort”: { “balance”: { “order”: “desc” } }
  }’

上述示例匹配所有的索引中的数据,按照balance字段降序排序,并且返回前10条(如果不指定size,默认最多返回10条)。

(15)elasticsearch 搜索

 返回两个字段(account_number balance)

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: { “match_all”: {} },
   “_source”: [“account_number”, “balance”]
  }’

 返回account_number 为20 的数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: { “match”: { “account_number”: 20 } }
  }’

返回address中包含mill的所有数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: { “match”: { “address”: “mill” } }
  }’

返回地址中包含mill或者lane的所有数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
  “query”: { “match”: { “address”: “mill lane” } }
  }’

和上面匹配单个词语不同,下面这个例子是多匹配(match_phrase短语匹配),返回地址中包含短语 “mill lane”的所有数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: { “match_phrase”: { “address”: “mill lane” } }
  }’

 以下是布尔查询,布尔查询允许我们将多个简单的查询组合成一个更复杂的布尔逻辑查询。

这个例子将两个查询组合,返回地址中含有mill和lane的所有记录数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {curl -X :///

:REST风格的语法谓词
  :节点ip
  :节点端口号,默认9200
  :索引名
  :索引类型
  :操作对象的ID号

curl localhost:9200/_cat

=.=
/_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

(1)elasticsearch 查看集群统计信息

 curl -XGET 'http://localhost:9200/_cluster/stats?pretty'   

(2)elasticsearch 查看所有索引

 curl 'localhost:9200/_cat/indices?v'       

(3)elasticsearch 查看集群的节点列表

 curl 'localhost:9200/_cat/nodes?v'  

(4)elasticsearch 检测集群是否健康

 curl 'localhost:9200/_cat/health?v' 

(5)elasticsearch 创建索引

 curl -XPUT 'localhost:9200/customer?pretty'

(6)elasticsearch 插入数据

 curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '

{
   “name”: “John Doe”
  }’

(7)elasticsearch 获取数据

 curl -XGET 'localhost:9200/customer/external/1?pretty'

 获取customer索引下类型为external,id为1的数据,pretty参数表示返回结果格式美观。

(8)elasticsearch 删除索引

curl -XDELETE 'localhost:9200/customer?pretty' 

(9)elasticsearch 修改数据

curl -XPUT ‘localhost:9200/customer/external/1?pretty’ -d ’
  {
   “name”: “John Doe”
  }’
  curl -XPUT ‘localhost:9200/customer/external/1?pretty’ -d ’
  {
   “name”: “Jane Doe”
  }’
先新增id为1,name为John Doe的数据,然后将id为1的name修改为Jane Doe。

(10)elasticsearch 更新数据

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '

{
   “doc”: { “name”: “Jane Doe” }
  }’

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '

{
   “doc”: { “name”: “Jane Doe”, “age”: 20 }
  }’

  curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '

{
   “script” : “ctx._source.age += 5”
  }’

(11)elasticsearch 删除数据

  curl -XDELETE 'localhost:9200/customer/external/2?pretty'
  将执行删除Customer中ID为2的数据

  curl -XPUT 'localhost:9200/customer'             //创建索引


  curl -XPUT 'localhost:9200/customer/external/1'-d '    //插入数据

{
“name”: “John Doe”
}’

curl ‘localhost:9200/customer/external/1’//查询数据

curl -XDELETE ‘localhost:9200/customer’//删除索引

(12)elasticsearch 批处理

批量操作中执行创建索引:

curl -XPOST ‘localhost:9200/customer/external/_bulk?pretty’ -d ’
  {“index”:{"_id":“1”}}
  {“name”: “John Doe” }
  {“index”:{"_id":“2”}}
  {“name”: “Jane Doe” }
  ’
  下面语句批处理执行更新id为1的数据然后执行删除id为2的数据

curl -XPOST ‘localhost:9200/customer/external/_bulk?pretty’ -d ’
  {“update”:{"_id":“1”}}
  {“doc”: { “name”: “John Doe becomes Jane Doe” } }
  {“delete”:{"_id":“2”}}

(13)elasticsearch 导入数据集

 curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"

curl ‘localhost:9200/_cat/indices?v’ 查看

(14)elasticsearch 查询数据

 curl 'localhost:9200/bank/_search?q=*&pretty'

 返回所有bank中的索引数据。其中 q=*  表示匹配索引中所有的数据。

 等价于:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: { “match_all”: {} }
  }’

 匹配所有数据,但只返回1个:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’

{
  “query”: { “match_all”: {} },
   “size”: 1
  }’
  注意:如果siez不指定,则默认返回10条数据。

  curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’

{
  “query”: { “match_all”: {} },
   “from”: 10,
   “size”: 10
  }’

返回从11到20的数据。(索引下标从0开始)
  curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’

{
   “query”: { “match_all”: {} },
  “sort”: { “balance”: { “order”: “desc” } }
  }’

上述示例匹配所有的索引中的数据,按照balance字段降序排序,并且返回前10条(如果不指定size,默认最多返回10条)。

(15)elasticsearch 搜索

 返回两个字段(account_number balance)

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: { “match_all”: {} },
   “_source”: [“account_number”, “balance”]
  }’

 返回account_number 为20 的数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: { “match”: { “account_number”: 20 } }
  }’

返回address中包含mill的所有数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: { “match”: { “address”: “mill” } }
  }’

返回地址中包含mill或者lane的所有数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
  “query”: { “match”: { “address”: “mill lane” } }
  }’

和上面匹配单个词语不同,下面这个例子是多匹配(match_phrase短语匹配),返回地址中包含短语 “mill lane”的所有数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: { “match_phrase”: { “address”: “mill lane” } }
  }’

 以下是布尔查询,布尔查询允许我们将多个简单的查询组合成一个更复杂的布尔逻辑查询。

这个例子将两个查询组合,返回地址中含有mill和lane的所有记录数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: {
   “bool”: {
   “must”: [
   { “match”: { “address”: “mill” } },
   { “match”: { “address”: “lane” } }
   ]
  }
  }
  }’
  上述例子中,must表示所有查询必须都为真才被认为匹配。

相反, 这个例子组合两个查询,返回地址中含有mill或者lane的所有记录数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
  “query”: {
   “bool”: {
   “should”: [
   { “match”: { “address”: “mill” } },
   { “match”: { “address”: “lane” } }
   ]
   }
  }
  }’

上述例子中,bool表示查询列表中只要有任何一个为真则认为匹配。

下面例子组合两个查询,返回地址中既没有mill也没有lane的所有数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: {
   “bool”: {
   “must_not”: [
   { “match”: { “address”: “mill” } },
   { “match”: { “address”: “lane” } }
  ]
  }
  }
  }’
  上述例子中,must_not表示查询列表中没有为真的(也就是全为假)时则认为匹配。

我们可以组合must、should、must_not来实现更加复杂的多级逻辑查询。

下面这个例子返回年龄大于40岁、不居住在ID的所有数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: {
   “bool”: {
   “must”: [
   { “match”: { “age”: “40” } }
   ],
   “must_not”: [
   { “match”: { “state”: “ID” } }
  ]
  }
   }
  }’

(16) elasticsearch 过滤filter(查询条件设置)

下面这个例子使用了布尔查询返回balance在20000到30000之间的所有数据。
  curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
     “query”: {
     “bool”: {
     “must”: { “match_all”: {} },
     “filter”: {
    “range”: {
  “balance”: {
   “gte”: 20000,
   “lte”: 30000
   }
   }
   }
   }
  }
  }’

(17) elasticsearch 聚合 Aggregations

下面这个例子: 将所有的数据按照state分组(group),然后按照分组记录数从大到小排序,返回前十条(默认):

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
  “size”: 0,
   “aggs”: {
   “group_by_state”: {
   “terms”: {
    “field”: “state”
   }
   }
  }
  }’

下面这个实例按照state分组,降序排序,返回balance的平均值:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
  “size”: 0,
  “aggs”: {
   “group_by_state”: {
   “terms”: {
   “field”: “state”
   },
   “aggs”: {
   “average_balance”: {
   “avg”: {
   “field”: “balance”
   }
   }
   }
  }
  }
  }’

(18)elasticsearch 取得某个索引中某个字段中的所有出现过的值

这种操作类似于使用SQL的SELECT UNIQUE语句。当需要获取某个字段上的所有可用值时,可以使用terms聚合查询完成:

GET /index_streets/_search?search_type=count
{
“aggs”: {
“street_values”: {
“terms”: {
“field”: “name.raw”,
“size”: 0
}
}
}
}

因为目标是得到name字段上的所有出现过的值,因此search_type被设置为了count,这样在返回的响应中不会出现冗长的hits部分。另外,查询的目标字段的索引类型需要设置为not_analyzed。所以上面的field指定的是name.raw。

得到的响应如下所示:

{
“took”: 23,
“timed_out”: false,
“_shards”: {
“total”: 5,
“successful”: 5,
“failed”: 0
},
“hits”: {
“total”: 7445,
“max_score”: 0,
“hits”: []
},
“aggregations”: {
“street_values”: {
“doc_count_error_upper_bound”: 0,
“sum_other_doc_count”: 0,
“buckets”: [
{
“key”: “江苏路”,
“doc_count”: 29
},
{
“key”: “南京东路”,
“doc_count”: 28
},


(19)elasticsearch 取得某个索引/类型下某个字段中出现的不同值的个数

这种操作类似于使用SQL的select count( * ) from (select distinct * from table)语句。当需要获取某个字段上的出现的不同值的个数时,可以使用cardinality聚合查询完成:

GET /index_streets/_search?search_type=count
{
“aggs”: {
“uniq_streets”: {
“cardinality”: {
“field”: “name.raw”
}
}
}
}

因为目标是得到name字段上的所有出现过的值,因此search_type被设置为了count,这样在返回的响应中不会出现冗长的hits部分。另外,查询的目标字段如果是字符串类型的,那么其索引类型需要设置为not_analyzed。所以上面的field指定的是name.raw。

得到的响应如下所示:

{
“took”: 96,
“timed_out”: false,
“_shards”: {
“total”: 1,
“successful”: 1,
“failed”: 0
},
“hits”: {
“total”: 4136543,
“max_score”: 0,
“hits”: []
},
“aggregations”: {
“uniq_streets”: {
“value”: 1951
}
}
}

(20)elasticsearch 每个命令都支持使用?v参数,来显示详细的信息:

  curl localhost:9200/_cat/master?v 

id host ip node
yBet3cYzQbC68FRzLZDmFg 127.0.0.1 127.0.0.1 lihao

(21)elasticsearch 每个命令都支持使用help参数,来输出可以显示的列:

  curl localhost:9200/_cat/master?help 

id | | node id
host | h | host name
ip | | ip address
node | n | node name

(22)elasticsearch 通过h参数,可以指定输出的字段:

$ curl localhost:9200/_cat/master?v
id host ip node
yBet3cYzQbC68FRzLZDmFg 127.0.0.1 127.0.0.1 lihao

$ curl localhost:9200/_cat/master?h=ip,node
127.0.0.1 lihao

(23) elasticsearch 很多的命令都支持返回可读性的大小数字,比如使用mb或者kb来表示。

$ curl localhost:9200/_cat/indices?v

health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open aaa 5 1 2 0 7.2kb 7.2kb
yellow open logstash-eos-2016.09.01 5 1 297 0 202.3kb 202.3kb
yellow open bank 5 1 1001 1 451.6kb 451.6kb
yellow open website 5 1 2 0 7.8kb 7.8kb
yellow open .kibana 1 1 5 1 26.6kb 26.6kb
yellow open logstash-eos-2016.09.02 5 1 11 0 33.9kb 33.9kb
yellow open test-2016.09.01 5 1 1 0 3.9kb 3.9kb
yellow open testst_index 5 1 0 0 795b 795b
   “query”: {
   “bool”: {
   “must”: [
   { “match”: { “address”: “mill” } },
   { “match”: { “address”: “lane” } }
   ]
  }
  }
  }’
  上述例子中,must表示所有查询必须都为真才被认为匹配。

相反, 这个例子组合两个查询,返回地址中含有mill或者lane的所有记录数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
  “query”: {
   “bool”: {
   “should”: [
   { “match”: { “address”: “mill” } },
   { “match”: { “address”: “lane” } }
   ]
   }
  }
  }’

上述例子中,bool表示查询列表中只要有任何一个为真则认为匹配。

下面例子组合两个查询,返回地址中既没有mill也没有lane的所有数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: {
   “bool”: {
   “must_not”: [
   { “match”: { “address”: “mill” } },
   { “match”: { “address”: “lane” } }
  ]
  }
  }
  }’
  上述例子中,must_not表示查询列表中没有为真的(也就是全为假)时则认为匹配。

我们可以组合must、should、must_not来实现更加复杂的多级逻辑查询。

下面这个例子返回年龄大于40岁、不居住在ID的所有数据:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
   “query”: {
   “bool”: {
   “must”: [
   { “match”: { “age”: “40” } }
   ],
   “must_not”: [
   { “match”: { “state”: “ID” } }
  ]
  }
   }
  }’

(16) elasticsearch 过滤filter(查询条件设置)

下面这个例子使用了布尔查询返回balance在20000到30000之间的所有数据。
  curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
     “query”: {
     “bool”: {
     “must”: { “match_all”: {} },
     “filter”: {
    “range”: {
  “balance”: {
   “gte”: 20000,
   “lte”: 30000
   }
   }
   }
   }
  }
  }’

(17) elasticsearch 聚合 Aggregations

下面这个例子: 将所有的数据按照state分组(group),然后按照分组记录数从大到小排序,返回前十条(默认):

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
  “size”: 0,
   “aggs”: {
   “group_by_state”: {
   “terms”: {
    “field”: “state”
   }
   }
  }
  }’

下面这个实例按照state分组,降序排序,返回balance的平均值:

curl -XPOST ‘localhost:9200/bank/_search?pretty’ -d ’
  {
  “size”: 0,
  “aggs”: {
   “group_by_state”: {
   “terms”: {
   “field”: “state”
   },
   “aggs”: {
   “average_balance”: {
   “avg”: {
   “field”: “balance”
   }
   }
   }
  }
  }
  }’

(18)elasticsearch 取得某个索引中某个字段中的所有出现过的值

这种操作类似于使用SQL的SELECT UNIQUE语句。当需要获取某个字段上的所有可用值时,可以使用terms聚合查询完成:

GET /index_streets/_search?search_type=count
{
“aggs”: {
“street_values”: {
“terms”: {
“field”: “name.raw”,
“size”: 0
}
}
}
}

因为目标是得到name字段上的所有出现过的值,因此search_type被设置为了count,这样在返回的响应中不会出现冗长的hits部分。另外,查询的目标字段的索引类型需要设置为not_analyzed。所以上面的field指定的是name.raw。

得到的响应如下所示:

{
“took”: 23,
“timed_out”: false,
“_shards”: {
“total”: 5,
“successful”: 5,
“failed”: 0
},
“hits”: {
“total”: 7445,
“max_score”: 0,
“hits”: []
},
“aggregations”: {
“street_values”: {
“doc_count_error_upper_bound”: 0,
“sum_other_doc_count”: 0,
“buckets”: [
{
“key”: “江苏路”,
“doc_count”: 29
},
{
“key”: “南京东路”,
“doc_count”: 28
},


(19)elasticsearch 取得某个索引/类型下某个字段中出现的不同值的个数

这种操作类似于使用SQL的select count( * ) from (select distinct * from table)语句。当需要获取某个字段上的出现的不同值的个数时,可以使用cardinality聚合查询完成:

GET /index_streets/_search?search_type=count
{
“aggs”: {
“uniq_streets”: {
“cardinality”: {
“field”: “name.raw”
}
}
}
}

因为目标是得到name字段上的所有出现过的值,因此search_type被设置为了count,这样在返回的响应中不会出现冗长的hits部分。另外,查询的目标字段如果是字符串类型的,那么其索引类型需要设置为not_analyzed。所以上面的field指定的是name.raw。

得到的响应如下所示:

{
“took”: 96,
“timed_out”: false,
“_shards”: {
“total”: 1,
“successful”: 1,
“failed”: 0
},
“hits”: {
“total”: 4136543,
“max_score”: 0,
“hits”: []
},
“aggregations”: {
“uniq_streets”: {
“value”: 1951
}
}
}

(20)elasticsearch 每个命令都支持使用?v参数,来显示详细的信息:

  curl localhost:9200/_cat/master?v 

id host ip node
yBet3cYzQbC68FRzLZDmFg 127.0.0.1 127.0.0.1 lihao

(21)elasticsearch 每个命令都支持使用help参数,来输出可以显示的列:

  curl localhost:9200/_cat/master?help 

id | | node id
host | h | host name
ip | | ip address
node | n | node name

(22)elasticsearch 通过h参数,可以指定输出的字段:

$ curl localhost:9200/_cat/master?v
id host ip node
yBet3cYzQbC68FRzLZDmFg 127.0.0.1 127.0.0.1 lihao

$ curl localhost:9200/_cat/master?h=ip,node
127.0.0.1 lihao

(23) elasticsearch 很多的命令都支持返回可读性的大小数字,比如使用mb或者kb来表示。

$ curl localhost:9200/_cat/indices?v

health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open aaa 5 1 2 0 7.2kb 7.2kb
yellow open logstash-eos-2016.09.01 5 1 297 0 202.3kb 202.3kb
yellow open bank 5 1 1001 1 451.6kb 451.6kb
yellow open website 5 1 2 0 7.8kb 7.8kb
yellow open .kibana 1 1 5 1 26.6kb 26.6kb
yellow open logstash-eos-2016.09.02 5 1 11 0 33.9kb 33.9kb
yellow open test-2016.09.01 5 1 1 0 3.9kb 3.9kb
yellow open testst_index 5 1 0 0 795b 795b

欢迎关注微信公众号:“Coding World”
获取更多相关的技术
扫码关注

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

elasticsearch常用命令 的相关文章

随机推荐

  • 11G RAC 中 OCR 及Voting Disk 相关操作

    一 启动oracle clusterware 先决条件 Oracle High Availability Services daemon OHASD 运行在所有集群节点上 1 启动整个Oracle Clusterware stack crs
  • windows下git

    下载gitGit for Windows Windows安装git图文教程 喵代王 香菜的博客 CSDN博客 windows安装git 创建文件夹 右键 git bash here 同mac使用
  • 基于Spring Boot的ERP仓储管理信息系统设计与实现毕业设计源码150958

    基于Spring Boot的ERP仓储管理信息系统设计与实现 摘 要 科技进步的飞速发展引起人们日常生活的巨大变化 电子信息技术的飞速发展使得电子信息技术的各个领域的应用水平得到普及和应用 信息时代的到来已成为不可阻挡的时尚潮流 人类发展的
  • 如何实现一个IO口读取多个设备信息

    前言 1 今天遇到一个有意思的问题一个IO口如何读取多个电机的堵转问题 之后他就发了一张图片 2 看到这个问题 之前先说一个简单的 我们如何实现一个IO读取多个按键 了解了这个之后 对于多个电机堵转就很好理解了 如何实现一个IO对多个按键读
  • 直方图均衡化原理

    原文 http www cnblogs com tianyalu p 5687782 html 直方图均衡化原理 直方图均衡化的作用是图像增强 有两个问题比较难懂 一是为什么要选用累积分布函数 二是为什么使用累积分布函数处理后像素值会均匀分
  • 从零开始的Java开发 笔记目录(跑路了)

    写在前面 不全 学习资料来源于网络 已经跑路了 文章目录 阶段1 Java零基础入门 第1周 环境搭建与语法入门 第2周 Java语法之循环 数组与方法 第3周 面向对象之封装与继承 第4周 面向对象之单例模式与多态 第5周 常用工具类 上
  • linux c++遍历文件夹下所有文件,C++ 遍历目录下文件

    function 遍历目录下所有文件 返回文件总数 子文件夹总数 修改一下可以获得全部文件名等 include stdlib h include direct h include string h include io h include
  • 对OOD/OOP有较深的理解

    最近 经常有很多人在求职的时候遇到这样一个问题 对OOD OOP有较深的理解 那OOD OOP又是什么 那今天就来讲讲它们都是些什么 又如何去回答 1 OOA Object oriented analysis 面向对象分析 面向对象分析方法
  • 一款带ai基因的向导般生成ppt的神奇网站

    只要按要求填写每一页的内容 即可生成一套像模像样的ppt 无需排版 模板众多 以后ppt不需要人写了 哈哈 1 登录 https app slidebean com 2 注册 3 新建 4 模板选择 5 填写 以airbnb为例 6 结果
  • 【微信读书每日一答辅助小程序】使用python对每日一答问题进行识别,并将结果保存到剪贴板以便搜索。

    目录标题 1 环境准备 2 获取屏幕位置 3 指定区域屏幕截图 4 文字识别 5 按键识别并保存到剪贴板 在腾讯收购阅文之后 微信读书的无限卡已经不能免费看书了 这时白嫖微信读书每日一答的书币成了不错的选择 严重偏科又手速垃圾的我在等级升高
  • Win10 解决docker一直docker desktop starting进不去的问题

    这里写自定义目录标题 为什么出现这个问题 方法1 方法2 方法3 解决我的问题 后续计划 为什么出现这个问题 似乎是因为上次没有完全关闭 而是直接关闭电脑导致的 目前有三种方法 后续应该有更多 我这边方法1 2都没有解决我的问题 方法3解决
  • Rxjs 操作符实践指南

    操作符实战 1 工具方法型 count 统计总数 import range from rxjs import count from rxjs operators const numbers range 1 7 const result nu
  • python中16mod7_mod_python模块安装

    两 mod python 1 性能 使用mod python的主要优势在于比传统CGI更高的性能 一个測试 使用在Pentium 1 2GHz的机器上执行Red Hat Linux 7 3 使用4种类型的脚本 基于标准的CGI导入模块 以典
  • Android Glide加载图片圆角效果与ImageView的ScaleType冲突问题

    在imageVIew显示图片的时候一般是使用 android scaleType centerCrop 来让图片不被变形显示 但是如果现在用Glide来加载图片并给它转化出一个圆角 transform new GlideRoundTrans
  • 【导航】ESP32-C3 入门教程目录 【快速跳转】

    本文是 矜辰所致 的ESP32 C3 专栏的内容导航 结合自己的学习应用过程的总结记录 ESP32 C3入门教程 前言 一 环境篇 二 硬件篇 三 基础篇 四 Wi Fi篇 五 蓝牙篇 六 应用篇 前言 本系列教程以实际应用为目的 能够使得
  • 代码随想录 - Day37 - 贪心算法

    代码随想录 Day37 贪心算法 376 摆动序列 排除只有一个数的情况 把差值全部求出来放到dif里 在此过程中顺便去掉差值为0的情况 如果dif为空 说明里面所有差值为0 那么最长摆动序列只能是1 直接返回 如果dif不为空 把dif
  • OpenCV学习笔记——《基于OpenCV的数字图像处理》

    源码下载 下载资源包 bookln cn 常用函数库 英文 OpenCV OpenCV modules 中文 Welcome to opencv documentation OpenCV 2 3 2 documentation jetson
  • esp8266-01s介绍与使用

    esp826601s 是个比较常用的wifi模块 体积小 功能强大 说是可以用于工业 下面介绍esp826601s 可用引脚 以及可用功能 esp 01 ESP 01S 在ESP 01的基础上 优化了PCB天线 进行了一小步的升级 带来了一
  • label smooth的pytorch实现以及其公式推导(虽然短但是细)

    标签平滑 label smooth 标签平滑是一种正则化手段 目的为了解决onehot编码的缺陷 减少过拟合问题 在各种竞赛中广泛使用 涨点神器 假设 预测的结果为 y p r e d
  • elasticsearch常用命令

    curl X REST风格的语法谓词 节点ip 节点端口号 默认9200 索引名 索引类型 操作对象的ID号 curl localhost 9200 cat cat allocation cat shards cat shards inde