elasticsearch常用命令

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章