本文共 8791 字,大约阅读时间需要 29 分钟。
curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>
<REST Verb>:REST风格的语法谓词
<Node>:节点ip <port>:节点端口号,默认9200 <Index>:索引名 <Type>:索引类型 <ID>:操作对象的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 nameip | | ip addressnode | n | node name(22)elasticsearch 通过h参数,可以指定输出的字段:
$ curl localhost:9200/_cat/master?v
id host ip nodeyBet3cYzQbC68FRzLZDmFg 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.2kbyellow open logstash-eos-2016.09.01 5 1 297 0 202.3kb 202.3kbyellow open bank 5 1 1001 1 451.6kb 451.6kbyellow open website 5 1 2 0 7.8kb 7.8kbyellow open .kibana 1 1 5 1 26.6kb 26.6kbyellow open logstash-eos-2016.09.02 5 1 11 0 33.9kb 33.9kbyellow open test-2016.09.01 5 1 1 0 3.9kb 3.9kbyellow open testst_index 5 1 0 0 795b 795b转载于:https://blog.51cto.com/6226001001/2104383