1、檢查集群狀態
curl 'localhost:9200/_cat/health?v' -u admin:xxxx
2、獲取節點信息
curl 'localhost:9200/_cat/nodes?v' -u admin:xxxx
3、列出所有索引
curl 'localhost:9200/_cat/indices?v' -u admin:xxxx
4、創建索引
現在我們創建一個名為“kzf”的索引,然后再查看所有的索引 curl -XPUT 'localhost:9200/kzf?pretty' -u admin:xxxx
5、插入數據到索引
插入一些數據到集群索引。我們必須給ES指定所以的類型。 必須制定數據type:external,ID:1 ,數據主體{"name": "John Doe"} curl -XPUT 'localhost:9200/kzf/external/1?pretty' -d '{"name": "John Doe"}' -u admin:xxxx
6、查詢索引數據

1、指定索引類型和索引id curl -XGET 'localhost:9200/kzf/external/1?pretty' -u admin:xxxx 2、查詢索引下面的所有(注意:如果siez不指定,則默認返回10條數據。) curl 'localhost:9200/bank/_search?q=*&pretty' 上面示例返回所有bank中的索引數據。其中 q=* 表示匹配索引中所有的數據。 等價於: curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} } }' 3、查詢指定范圍的數據(從第10行開始,往后查詢100條數據) curl -H "Content-Type: application/json" -XPOST 'localhost:9200/fy_smart_news_sentiment_tag/_search?pretty' -u admin:xxxx -d '{ "query": { "match_all": {}} , "from": 10, "size": 100 }' 4、匹配所有的索引中的數據,按照balance字段降序排序,並且返回前10條(如果不指定size,默認最多返回10條)。 curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -u admin:xxxx -d ' { "query": { "match_all": {} }, "sort": { "balance": { "order": "desc" } } }' 5、下面例子展示如何返回兩個字段(account_number balance) curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -u admin:xxxx -d ' { "query": { "match_all": {} }, "_source": ["account_number", "balance"] }'
根據字段值查詢

1、返回account_number 為20 的數據 curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -u admin:xxxx -d ' { "query": { "match": { "account_number": 20 } } }' 2、返回address中包含mill的所有數據:: curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -u admin:xxxx -d ' { "query": { "match": { "address": "mill" } } }' 3、返回地址中包含mill或者lane的所有數據: curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -u admin:xxxx -d ' { "query": { "match": { "address": "mill lane" } } }' 4、和上面匹配單個詞語不同,下面這個例子是多匹配(match_phrase短語匹配),返回地址中包含短語 “mill lane”的所有數據: curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -u admin:xxxx -d ' { "query": { "match_phrase": { "address": "mill lane" } } }'
以下是布爾查詢,布爾查詢允許我們將多個簡單的查詢組合成一個更復雜的布爾邏輯查詢。

這個例子將兩個查詢組合,返回地址中含有mill和lane的所有記錄數據: curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }' 上述例子中,must表示所有查詢必須都為真才被認為匹配。 相反, 這個例子組合兩個查詢,返回地址中含有mill或者lane的所有記錄數據: curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "should": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }' 上述例子中,bool表示查詢列表中只要有任何一個為真則認為匹配。 下面例子組合兩個查詢,返回地址中既沒有mill也沒有lane的所有數據: curl -H "Content-Type: application/json" -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 -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must": [ { "match": { "age": "40" } } ], "must_not": [ { "match": { "state": "ID" } } ] } } }'
過濾filter(查詢條件設置)

下面這個例子使用了布爾查詢返回balance在20000到30000之間的所有數據。 curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must": { "match_all": {} }, "filter": { "range": { "balance": { "gte": 20000, "lte": 30000 } } } } } }'
聚合 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" } } } } } }'
7、刪除索引
curl -XDELETE 'localhost:9200/kzf?pretty' -u admin:xxxx
8、更新數據
將id為1文檔的name字段更新為Jane Doe curl -XPOST 'localhost:9200/customer/kzf/1/_update?pretty' -u admin:xxxx -d ' { "doc": { "name": "Jane Doe" } }'
9、下面語句將在一個批量操作中執行創建索引:
curl -XPOST 'localhost:9200/kzf/external/_bulk?pretty' -u admin:xxxx -d ' {"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" } '
10、通過文件倒入大量數據
首先把數據寫入json文件命名為asdf.json curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@asdf.json"