記知識(shí)先記輪廓,關(guān)于DSL語(yǔ)法的輪廓,記住以下三句話即可:
使用Elasticsearch時(shí),我們一般是調(diào)用RestClient API的方式讀取和寫(xiě)入集群數(shù)據(jù)。有時(shí)也會(huì)使用工具查閱和操作數(shù)據(jù),比如:使用Chrome插件Multi Elasticsearch Head或者Cerebro、Kibana。筆者建議使用Kibana的方式操作集群數(shù)據(jù),使用Multi Elasticsearch Head或者Cerebro從整體上觀察集群。
既然是操作集群數(shù)據(jù),那就繞不開(kāi)ES的DSL語(yǔ)法 — 一個(gè)讓人又愛(ài)又恨的語(yǔ)法。
本文整理了一些常用DSL語(yǔ)法,方便記憶,分了如下幾類(lèi):操作索引、操作文檔、Match查詢、Term查詢、查看分詞。如果碰到復(fù)雜查詢還是建議查閱官網(wǎng)。
在Kibana上操作ES數(shù)據(jù)的方式如下:
(1) 創(chuàng)建索引:
PUT /goods{ "mappings": { "properties": { "brandName": { "type": "keyword" }, "categoryName": { "type": "keyword" }, "createTime": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss" }, "id": { "type": "keyword" }, "price": { "type": "double" }, "saleNum": { "type": "integer" }, "status": { "type": "integer" }, "stock": { "type": "integer" }, "title": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" } } }, # 根據(jù)情況選擇是否要修改 "settings": { "number_of_shards": 2, "number_of_replicas": 2 }}
(2) 刪除索引:
DELETE goods
(3) 重建索引
有些場(chǎng)景下需要重建索引,比如修改了Mapping,重建步驟如下:
POST _reindex { "source": { "index": "goods" }, "dest": { "index": "goods1" }}DELETE goodsPOST _reindex { "source": { "index": "goods1" }, "dest": { "index": "goods" }}DELETE goods1
(1) 創(chuàng)建文檔
# 這種方式,同樣的id無(wú)法重新創(chuàng)建PUT goods/_create/1{ "id": 1, "brandName": "Apple", "categoryName": "手機(jī)", "createTime": "2023-10-22 19:12:56", "price": 8799, "saleNum": 599, "status": 0, "stock": 1000, "title": "Apple iPhone 15 Pro 512GB 遠(yuǎn)峰藍(lán)色 支持移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"}# 這種方式,同樣的id會(huì)覆蓋原有的PUT goods/_doc/2{ "id": 2, "brandName": "Apple", "categoryName": "手機(jī)", "createTime": "2023-10-22 19:12:56", "price": 8799, "saleNum": 599, "status": 0, "stock": 1000, "title": "Apple iPhone 15 Pro 256GB 遠(yuǎn)峰藍(lán)色 支持移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"}
(2) 更新文檔
POST goods/_update/1{ "doc": { "title":"Apple iPhone 13 Pro (A2639) 256GB 遠(yuǎn)峰藍(lán)色 支持移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)111" }}
(3) 刪除文檔
DELETE goods/_doc/2
(4) 獲取文檔
# 獲取單個(gè)文檔GET goods/_doc/1# 批量獲取GET books/_doc/_mget{ "ids": ["1","2"]}
Match查詢會(huì)對(duì)查詢內(nèi)容做分詞,然后根據(jù)倒排索引去匹配文檔。Term查詢對(duì)查詢內(nèi)容不做分詞,直接去倒排索引里去匹配文檔。
(1) 查詢所有
POST goods/_search{ "query": { "match_all": { } }}
(2) match_phrase短語(yǔ)查詢
POST goods/_search{ "query": { "match_phrase": { "title": "支持" } }}
(3) 匹配查詢
POST goods/_search{ "query": { "match": { "title": "移動(dòng)多余" } }}
(4) 模糊匹配查詢
POST goods/_search{ "query": { "wildcard": { "title": { "value": "*鞋" } } }}
Term查詢對(duì)查詢內(nèi)容不做分詞,直接去倒排索引里去匹配文檔。
POST goods/_search{ "query": { "term": { "title": { "value": "手機(jī)" } } }}# 匹配多個(gè)termPOST goods/_search{ "query": { "terms": { "title": [ "雙卡", "待" ] } }}
復(fù)雜查詢基本會(huì)用到bool關(guān)鍵字。
(1) bool + must
# 布爾查詢,可以組合多個(gè)過(guò)濾語(yǔ)句來(lái)過(guò)濾文檔POST goods/_search{ "query": { "bool": { "must": [ { "term": { "title": { "value": "Wolfgang Mauerer" } } }, { "term": { "date": { "value": "2010-06-01" } } } ] } }}# 匹配多個(gè)字段GET product/_search{ "query": { "bool": { "must": [ { "match_phrase": { "name": "連衣裙" } }, { "match_phrase": { "en_intro": "korean" } }, { "match_phrase": { "intro": "御姐" } } ] } }}
(2) bool + filter + range
POST books/_search{ "query": { "bool": { "must": [ { "term": { "author": { "value": "Wolfgang Mauerer" } } } ], "filter": [ { "term": { "date": { "value": "2010-06-01" } } } ] } }}POST goods/_search{ "query": { "bool": { "must": [ { "match": { "title": "華為" } } ], "filter": [ { "range": { "price": { "gte": 5000, "lte": 10000 } } } ] } }}
(1) Scroll分頁(yè)
# 第一次使用 scroll APIPOST goods/_search?scroll=2m{ "query": { "match_all": {} }, "size": 2}# 進(jìn)行翻頁(yè)P(yáng)OST /_search/scroll { "scroll" : "2m", "scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFkxBWkYwOGw2U1dPSF94aHZTelFkaWcAAAAAAAADHhZoU05ERFl3WFIycXM3M3JKMmRQVkJB" }
(2) from + size分頁(yè)
POST goods/_search{ "query": { "match_all": { } }, "from": 6, "size": 2, "sort": [ { "price": { "order": "asc" } } ]}
(1) 最大、最小、平均
POST goods/_search{ "aggs": { "avg_price": { "avg": { "field": "price" } } }}POST goods/_search{ "aggs": { "min_price": { "min": { "field": "price" } } }}POST goods/_search{ "aggs": { "max_price": { "max": { "field": "price" } } }}
(2) 范圍查詢
POST goods/_search{ "query": { "range": { "price": { "gte": 10, "lte": 20 } } }}
(3) 高亮查詢
POST goods/_search{ "query": { "match": { "title": "跑鞋" } }, "highlight": { "fields": { "body": { "pre_tags": [ "<font color='red'>" ], "post_tags": [ "</font>" ] }, "title": {} } }}
(4) 分組查詢
POST goods/_search{ "aggs": { "brandNameName": { "terms": { "field": "brandName" } } }}
(5) 子查詢
POST goods/_search{ "aggs": { "brandNameName": { "terms": { "field": "brandName" }, "aggs": { "avgPrice": { "avg": { "field": "price" } } } } }}
相對(duì)一些分析進(jìn)行分析時(shí),看看ES怎么拆分的,可以用這個(gè)查看。
POST _analyze{ "analyzer": "standard", "text": "Linus 在90年代開(kāi)發(fā)出了linux操作系統(tǒng)"}POST _analyze{ "analyzer": "ik_max_word", "text": "Linus 在90年代開(kāi)發(fā)出了linux操作系統(tǒng)" }POST _analyze{ "analyzer": "ik_smart", "text": "Linus 在90年代開(kāi)發(fā)出了linux操作系統(tǒng)" }POST _analyze{ "analyzer": "ik_smart", "text": "中華人民共和國(guó)國(guó)歌" }POST _analyze{ "analyzer": "ik_max_word", "text": "中華人民共和國(guó)國(guó)歌" }
本文主要介紹了常見(jiàn)DSL的用法,主要是幫助記憶,避免一些基本的操作還要去查詢文檔的尷尬。記住以下3句話,即可記住DSL的輪廓了:
本文鏈接:http://www.www897cc.com/showinfo-26-15616-0.html巧記Elasticsearch常用DSL語(yǔ)法
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com
上一篇: 用示例和應(yīng)用程序了解必要的Golang庫(kù)
下一篇: 面試挑戰(zhàn):解密百度產(chǎn)品經(jīng)理面試題——為什么牛奶盒是方的,而可樂(lè)罐是圓的?