Elasticsearch聚合查詢及排序操作示例
1 es排序
# 1 排序 GET jeff/doc/_search { "query": { "match": { "from": "gu" } }, "sort": [ { "age": { "order": "desc" } } ] } # 升序 GET jeff/doc/_search { "query": { "match": { "from": "gu" } }, "sort": [ { "age": { "order": "asc" } } ] } # 并不是所有類型都支持排序(只允許數(shù)字類型做排序) GET jeff/doc/_search { "query": { "match": { "from": "gu" } }, "sort": [ { "name": { "order": "asc" } } ] }
2 match和match的區(qū)別
# match和match_all的區(qū)別? mach表示要查詢,根據(jù)字段查,match_all查所有 GET jeff/doc/_search { "query": { "match_all": {} } }
3 分頁查詢
GET jeff/doc/_search { "query": { "match_all": {} }, "sort": [ { "age": { "order": "desc" } } ], "from": 2, "size": 1 } # "from": 2,代表從第二條開始, 取一條"size": 1 # 有了這個(gè)查詢,如何分頁? 一頁有10條數(shù)據(jù) 第一頁: "from": 0, "size": 10 第二頁: "from": 10, "size": 10 第三頁: "from": 20, "size": 10
4 es 組合查詢
# 多個(gè)條件,and ,or ,not # 對(duì)到es中就是布爾查詢,must,should,must_not,filter must --- and should --- or must_not --- not filter --- 過濾 # 1 組合查詢之must # 查詢form gu和age=30的數(shù)據(jù) GET gyy/doc/_search { "query": { "bool": { "must": [ { "match": { "from": "gu" } }, { "match": { "age": "30" } } ] } } } # 查詢form gu數(shù)據(jù)() GET gyy/doc/_search { "query": { "bool": { "must": [ { "match": { "from": "gu" } } ] } } } # 同上 GET gyy/doc/_search { "query": { "match": { "from": "gu" } } } # 2 組合查詢之should,或者的條件 GET gyy/doc/_search { "query": { "bool": { "should": [ { "match": { "from": "gu" } }, { "match": { "tags": "閉月" } } ] } } } # 3 組合查詢之must_not 取反 GET gyy/doc/_search { "query": { "bool": { "must_not": [ { "match": { "from": "gu" } }, { "match": { "tags": "可愛" } }, { "match": { "age": 18 } } ] } } } # `filter`條件過濾查詢,過濾條件的范圍用`range`表示,`gt`表示大于,大于多少呢 # gt:大于 lt:小于 get:大于等于 let:小于等于 GET gyy/doc/_search { "query": { "bool": { "must": [ { "match": { "from": "gu" } } ], "filter": { "range": { "age": { "gt": 25 } } } } } } # 查詢年齡小于等于18的所有數(shù)據(jù) GET gyy/doc/_search { "query": { "bool": { "filter": { "range": { "age": { "lte": 18 } } } } } }
5 結(jié)果過濾展示字端
# 對(duì)結(jié)果進(jìn)行過濾,類似于如下 select * from user; select name,age from user; # 對(duì)應(yīng)到es的查詢 GET gyy/doc/_search { "query": { "match": { "name": "顧老二" } }, "_source": ["name", "age"] }
6 結(jié)果高亮展示
# 3 結(jié)果高亮顯示(默認(rèn)情況) GET gyy/doc/_search { "query": { "match": { "name": "石頭" } }, "highlight": { "fields": { "name": {} } } } # 定制高亮顯示的樣式 GET gyy/chengyuan/_search { "query": { "match": { "from": "gu" } }, "highlight": { "pre_tags": "<b class='key' style='color:red'>", "post_tags": "</b>", "fields": { "from": {} } } }
小結(jié):
混合開發(fā),你知道怎么處理
前后端分離,你怎么處理?
<b class='key' style='color:red'>串直接以josn格式返回,前端自行渲染
用的最多就是match+布爾+高亮+分頁
7 聚合查詢avg、max、min、sum、分組
# 聚合查詢 # 1 聚合查詢之a(chǎn)vg select max(age) as my_avg from user; GET gyy/doc/_search { "query": { "match": { "from": "gu" } }, "aggs": { "my_avg": { "avg": { "field": "age" } } }, "_source": ["name", "age"] } # 2 聚合查詢之max,size=0表示不取數(shù)據(jù),只要max的結(jié)果 GET gyy/doc/_search { "query": { "match": { "from": "gu" } }, "aggs": { "my_max": { "max": { "field": "age" } } }, "size": 0 } # 3 聚合之min GET gyy/doc/_search { "query": { "match": { "from": "gu" } }, "aggs": { "my_min": { "min": { "field": "age" } } }, "size": 0 } # 4 聚合查詢之sum GET gyy/doc/_search { "query": { "match": { "from": "gu" } }, "aggs": { "my_sum": { "sum": { "field": "age" } } }, "size": 0 } # 5 聚合之分組 GET gyy/doc/_search { "size": 0, "query": { "match_all": {} }, "aggs": { "age_group": { "range": { "field": "age", "ranges": [ { "from": 15, "to": 20 }, { "from": 20, "to": 25 }, { "from": 25, "to": 30 } ] } } } }
8 mapping和_template模版
GET _template/user_instagram # 查看模版 PUT _template/user_instagram # 修改模版 {跟字段信息數(shù)據(jù)} GET user_instagram/_mapping # 查看索引信息 PUT user_instagram/_mapping # 修改索引信息 {跟字段信息數(shù)據(jù)}
模版中如果有name字段,存的時(shí)候會(huì)在索引中自動(dòng)匹配
模版中如果沒有age字段,存的時(shí)候索引找不到字段,存不進(jìn)去。
需要現(xiàn)在模版中添加字段,再到索引中添加字段,索引生成之后需要手動(dòng)添加字段,不會(huì)自動(dòng)生成
# 查看索引信息---》mapping字典---》映射(類型,表類型,表結(jié)構(gòu)) GET user_instagram/_mapping # 6.x以后一個(gè)索引只能有一個(gè)映射類型(只能有一個(gè)表) # 創(chuàng)建映射 # 創(chuàng)建索引,并設(shè)置映射 PUT _template/user_instagram { "order" : 1, "index_patterns" : [ "user_instagram-v1_0" ], "settings" : { "index" : { "default_pipeline" : "auto_timestamp_pipeline", "mapping" : { "total_fields" : { "limit" : "10000" } }, "refresh_interval" : "600s", "number_of_shards" : "8", "number_of_replicas" : "0", "max_inner_result_window" : "50000" } }, "mappings" : { "_meta" : { "software_version_mapping" : "1.0" }, "dynamic" : "strict", "properties" : { "is_private" : { "type" : "boolean" }, "full_name" : { "type" : "text" }, "create_time" : { "type" : "date" }, "avatar_url" : { "type" : "text" }, "user_id" : { "eager_global_ordinals" : true, "type" : "keyword" }, "follower_num" : { "type" : "integer" }, "following_num" : { "type" : "integer" }, "post_count" : { "type" : "integer" }, "nickname" : { "type" : "text", "fields" : { "keyword" : { "ignore_above" : 256, "type" : "keyword" } }, "doc_values" : false }, "requested_by_viewer" : { "type" : "boolean" }, "is_verified" : { "type" : "boolean" }, "followed_by_viewer" : { "type" : "boolean" } } }, "aliases" : { "user_instagram" : { } } } # 插入測試數(shù)據(jù) PUT books/_doc/1 { "title":"大頭兒子小偷爸爸", "price":100, "addr":"北京天安門", "company":{ "name":"我愛北京天安門", "company_addr":"我的家在東北松花江傻姑娘", "employee_count":10 }, "publish_date":"2019-08-19" } PUT books/_doc/2 { "title":"白雪公主和十個(gè)小矮人", "price":"99", "addr":"黑暗森里", "company":{ "name":"我的家鄉(xiāng)在上海", "company_addr":"朋友一生一起走", "employee_count":10 }, "publish_date":"2018-05-19" } PUT books/_doc/3 { "title":"白雪公主和十個(gè)小矮人", "price":"99", "addr":"黑暗森里", "age":18 } # 查看映射 GET books GET books/_mapping
映射是什么?映射有什么用? 規(guī)定了表結(jié)構(gòu)(不是強(qiáng)制的),規(guī)定了哪個(gè)字段是可以用來全文檢索,是否是數(shù)字類型,布爾類型
mapping類型一旦確定,以后就不能修改了,但是可以插入字段
9 ik分詞
# 全文檢索,有了映射,決定了我可以對(duì)某個(gè)字段做全文檢索 # es默認(rèn)分詞對(duì)英文友好,使用中文分詞器(es的插件),ik(作者,中國人,elasticsearch開源社區(qū)負(fù)責(zé)人) # 是es的一個(gè)插件(es如何安裝插件) #第一種:命令行(內(nèi)置插件) bin/elasticsearch-plugin install analysis-smartcn 安裝中文分詞器 #第二種:url安裝(第三方插件) bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.5.0/elasticsearch-analysis-ik-7.5.0.zip #第三種:手動(dòng)安裝(推薦用) #下載,解壓到es的plugins路徑下,重啟es即可 #注意:ik分詞器跟es版本一定要對(duì)應(yīng) # 兩種分詞方式 # ik_smart:分詞分的 # ik_max_word :分詞分的多 # ik_smart分的詞少,粒度大 GET _analyze { "analyzer": "ik_smart", "text": "上海自來水來自海上" } # ik_smart分的詞多,粒度小 GET _analyze { "analyzer": "ik_max_word", "text": "上海自來水來自海上" } # 在創(chuàng)建映射的時(shí)候配置 # 以后你的操作: #文章標(biāo)題:ik_max_word #文章內(nèi)容:ik_smart #摘要 #作者 #創(chuàng)建時(shí)間
10 term和match的區(qū)別
# match:我們今天出去玩 ----》分詞---》按分詞去搜 #term:我們今天出去玩---》直接拿著[我們今天出去玩]--->去索引中查詢 # 查不到內(nèi)容,直接拿著 Python爬蟲 去查,因?yàn)闆]有索引,所以查不到 GET books/_search { "query":{ "term":{ "title":"Python爬蟲" } } } # 能查到,而且?guī)ython的都查出來了 # Python 爬蟲 分了詞,分別拿著這兩個(gè)詞去查,帶python關(guān)鍵字,帶爬蟲關(guān)鍵字都能查到 GET books/_search { "query":{ "match":{ "title":"Python爬蟲" } } }
以上就是Elasticsearch聚合查詢及排序操作示例的詳細(xì)內(nèi)容,更多關(guān)于Elasticsearch聚合查詢及排序的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
IDEA取消git對(duì)項(xiàng)目的版本控制的實(shí)現(xiàn)
取消Git版本控制可以簡化代碼管理流程、減少學(xué)習(xí)成本、簡化代碼庫管理、提高代碼安全性、加快構(gòu)建和部署速度,本文主要介紹了IDEA取消git對(duì)項(xiàng)目的版本控制的實(shí)現(xiàn),感興趣的可以了解一下2023-11-11一文帶你快速梳理ChatGPT、GPT4 和OpenAPI的關(guān)系
最近最火的幾個(gè)詞無疑是ChatGPT、GPT4 和OpenAPI,那么這三者究竟有什么關(guān)系呢,本文將帶你進(jìn)行快速梳理三者的關(guān)系,感興趣的同學(xué)可以參考閱讀下2023-06-06Delphi - Indy idMessage和idSMTP實(shí)現(xiàn)郵件的發(fā)送
這篇文章主要介紹了Delphi - Indy idMessage和idSMTP實(shí)現(xiàn)郵件的發(fā)送,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08比特幣上的數(shù)獨(dú)游戲合約的實(shí)現(xiàn)代碼
這篇文章主要介紹了比特幣上的數(shù)獨(dú)游戲合約的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01