ElasticSearch 數據操做
1.插入數據
URL規則:spa
POST /{索引}/{類型}/{ID}rest
ID:能夠不傳遞,有則使用替代系統的_id,沒有則系統生成_id。code
1.1 指定ID
數據:blog
POST /haoke/user/1001 { "id":1001, "name":"張三", "age":20, "sex":"男" }
響應:索引
{ "_index": "haoke", "_type": "user", "_id": "1002", "_version": 1,#版本信息 "result": "created", #結果 "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
1.2 不指定id插入數據:
POST /haoke/user { "name":"李四", "age":23, "sex":"男" }
響應:ip
{ "_index": "haoke", "_type": "user", "_id": "e3bPeXUBg0sTC8c2CssI", "_version": 1, "result": "created", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
說明:非結構化的索引,不須要事先建立,直接插入數據默認建立索引。ci
2.更新數據
2.1 覆蓋更新
在Elasticsearch中,文檔數據是不可修改的,可是能夠經過覆蓋的方式進行更新。文檔
PUT /haoke/user/1001 { "id":1001, "name":"張三", "age":21, "sex":"女" }
更新結果以下:it
{ "_index": "haoke", "_type": "user", "_id": "1001", "_version": 2, #版本進行了+1 "result": "updated", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 2, "_primary_term": 2 }
注意 _version 增長了1io
2.2 局部更新數據
原理
在內部,依然會查詢到這個文檔數據,而後進行覆蓋操做,步驟以下:
1. 從舊文檔中檢索JSON
2. 修改它
3. 刪除舊文檔
4. 索引新文檔
示例
#注意:這裏多了_update標識 POST /haoke/user/1001/_update { "doc":{ "age":23 } }
結果
{ "_index": "haoke", "_type": "user", "_id": "1001", "_version": 3, "result": "updated", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 3, "_primary_term": 2 }
4.刪除數據
只需發起DELETE請求便可
DELETE /haoke/user/e3bPeXUBg0sTC8c2CssI
{ "_index": "haoke", "_type": "user", "_id": "e3bPeXUBg0sTC8c2CssI", "_version": 2, "result": "deleted", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 4, "_primary_term": 2 }
狀態 "result": "deleted",
再次請求
GET /haoke/user/e3bPeXUBg0sTC8c2CssI { "_index": "haoke", "_type": "user", "_id": "e3bPeXUBg0sTC8c2CssI", "found": false }
注意,result表示已經刪除,version 也會加1
刪除不存在的數據會返回 result: not_found
{ "_index": "haoke", "_type": "user", "_id": "e3bPeXUBg0sTC8c2CssI", "_version": 1, "result": "not_found", "_shards": { "total": 1, "successful": 1, "failed": 0 }, "_seq_no": 5, "_primary_term": 2 }
說明:
刪除一個文檔也不會當即從磁盤上移除,它只是被標記成已刪除。Elasticsearch將會在你以後添加更多索引的
時候纔會在後臺進行刪除內容的清理
5.搜索數據
5.1 根據id搜索數據
GET /haoke/user/1001 #返回結果 { "_index": "haoke", "_type": "user", "_id": "1001", "_version": 3, "_seq_no": 3, "_primary_term": 2, "found": true, "_source": { "id": 1001, "name": "張三", "age": 23, "sex": "女" } }
5.2 查詢所有數據
默認返回10條數據
GET /haoke/user/_search #返回結果 { "took": 2, "timed_out": false, "_shards": { "total": 2, "successful": 2, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": 1.0, "hits": [{ "_index": "haoke", "_type": "user", "_id": "1002", "_score": 1.0, "_source": { "id": 1002, "name": "張三", "age": 20, "sex": "男" } }, { "_index": "haoke", "_type": "user", "_id": "1001", "_score": 1.0, "_source": { "id": 1001, "name": "張三", "age": 23, "sex": "女" } }] } }
5.3 根據關鍵字搜索數據
GET /haoke/user/_search?q=age:20 #返回結果 { "took": 34, "timed_out": false, "_shards": { "total": 2, "successful": 2, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1.0, "hits": [{ "_index": "haoke", "_type": "user", "_id": "1002", "_score": 1.0, "_source": { "id": 1002, "name": "張三", "age": 20, "sex": "男" } }] } }
6.DSL搜索
Elasticsearch提供豐富且靈活的查詢語言叫作DSL查詢(Query DSL),它容許你構建更加複雜、強大的查詢。
DSL(Domain Specific Language特定領域語言)以JSON請求體的形式出現。
6.1 示例1:年齡等於20
POST /haoke/user/_search #請求體 { "query" : { "match" : { "age" : 20 } } } #響應 { "took": 3, "timed_out": false, "_shards": { "total": 2, "successful": 2, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1.0, "hits": [{ "_index": "haoke", "_type": "user", "_id": "1002", "_score": 1.0, "_source": { "id": 1002, "name": "張三", "age": 20, "sex": "男" } }] } }
6.2. 示例2:查詢年齡大於10歲的男性用戶
POST /haoke/user/_search #查詢結構體 { "query": { "bool": { "filter": { "range": { "age": { "gt": 10 } } }, "must": { "match": { "sex": "男" } } } } } #返回結構 { "took": 3, "timed_out": false, "_shards": { "total": 2, "successful": 2, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 0.4700036, "hits": [ { "_index": "haoke", "_type": "user", "_id": "s1MIg3UBapAzpGN_aapD", "_score": 0.4700036, "_source": { "id": 1003, "name": "李四", "age": 20, "sex": "男" } }, { "_index": "haoke", "_type": "user", "_id": "uVMLg3UBapAzpGN_EqoA", "_score": 0.4700036, "_source": { "id": 1004, "name": "王五", "age": 20, "sex": "男" } }, { "_index": "haoke", "_type": "user", "_id": "1002", "_score": 0.2876821, "_source": { "id": 1002, "name": "張三", "age": 20, "sex": "男" } } ] } }
filter: 過濾
must:匹配
6.3 全文搜索
POST /haoke/user/_search # 查詢體 { "query":{ "match":{ "name":"張三 李四" } } } # 返回 { "took": 7, "timed_out": false, "_shards": { "total": 2, "successful": 2, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 1.9616582, "hits": [ { "_index": "haoke", "_type": "user", "_id": "1002", "_score": 1.9616582, "_source": { "id": 1002, "name": "張三", "age": 20, "sex": "男" } }, { "_index": "haoke", "_type": "user", "_id": "1001", "_score": 1.9616582, "_source": { "id": 1001, "name": "張三", "age": 23, "sex": "女" } }, { "_index": "haoke", "_type": "user", "_id": "s1MIg3UBapAzpGN_aapD", "_score": 1.9616582, "_source": { "id": 1003, "name": "李四", "age": 20, "sex": "男" } } ] } }
7.高亮顯示
在query下面增長 highlight
POST /haoke/user/_search #查詢結構 { "query": { "match": { "name": "張三 李四" } }, "highlight": { "fields": { "name": {} } } } #返回 { "took": 55, "timed_out": false, "_shards": { "total": 2, "successful": 2, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 1.9616582, "hits": [ { "_index": "haoke", "_type": "user", "_id": "1002", "_score": 1.9616582, "_source": { "id": 1002, "name": "張三", "age": 20, "sex": "男" }, "highlight": { "name": [ "<em>張</em><em>三</em>" ] } }, { "_index": "haoke", "_type": "user", "_id": "1001", "_score": 1.9616582, "_source": { "id": 1001, "name": "張三", "age": 23, "sex": "女" }, "highlight": { "name": [ "<em>張</em><em>三</em>" ] } }, { "_index": "haoke", "_type": "user", "_id": "s1MIg3UBapAzpGN_aapD", "_score": 1.9616582, "_source": { "id": 1003, "name": "李四", "age": 20, "sex": "男" }, "highlight": { "name": [ "<em>李</em><em>四</em>" ] } } ] } }
8.聚合
在Elasticsearch中,支持聚合操做,相似SQL中的group by操做
POST /haoke/user/_search #查詢 { "aggs": { "all_interests": { "terms": { "field": "age" } } } } #返回 { "took": 19, "timed_out": false, "_shards": { "total": 2, "successful": 2, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 6, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "haoke", "_type": "user", "_id": "1002", "_score": 1.0, "_source": { "id": 1002, "name": "張三", "age": 20, "sex": "男" } }, { "_index": "haoke", "_type": "user", "_id": "wFMMg3UBapAzpGN_9aoN", "_score": 1.0, "_source": { "id": 1005, "name": "趙六", "age": 32, "sex": "女" } }, { "_index": "haoke", "_type": "user", "_id": "w1MNg3UBapAzpGN_j6qI", "_score": 1.0, "_source": { "id": 1005, "name": "孫七", "age": 33, "sex": "男" } }, { "_index": "haoke", "_type": "user", "_id": "1001", "_score": 1.0, "_source": { "id": 1001, "name": "張三", "age": 23, "sex": "女" } }, { "_index": "haoke", "_type": "user", "_id": "s1MIg3UBapAzpGN_aapD", "_score": 1.0, "_source": { "id": 1003, "name": "李四", "age": 20, "sex": "男" } }, { "_index": "haoke", "_type": "user", "_id": "uVMLg3UBapAzpGN_EqoA", "_score": 1.0, "_source": { "id": 1004, "name": "王五", "age": 20, "sex": "男" } } ] }, "aggregations": { "all_interests": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": 20, "doc_count": 3 }, { "key": 23, "doc_count": 1 }, { "key": 32, "doc_count": 1 }, { "key": 33, "doc_count": 1 } ] } } }
年齡 20有3條,23有1條...