Query DSL入門
官網(wǎng)介紹鏈接: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html
search api就是對存儲在elastic search(以下簡稱es)中的數(shù)據(jù)進行查詢的相關API,可以類比mysql中的select語句传睹。es中的search主要分為URI Search和Query DSL欧啤,其中又以Query DSL語法為主邢隧,也是我們學習es所要重點掌握的倒慧。
DSL介紹
Domain Specific Language:領域特定語言
Elasticsearch基于JSON提供完整的查詢DSL來定義查詢纫谅。
一個查詢可由兩部分字句構成:
- Leaf query clauses 葉子查詢字句:Leaf query clauses 在指定的字段上查詢指定的值付秕,如:match询吴、term or range queries. 葉子字句可以單獨使用。
- Compound query clauses 復合查詢字句:以邏輯方式組合多個葉子口柳、復合查詢?yōu)橐粋€查詢跃闹。
一個查詢字句的行為取決于它是用在query context 還是 filter context 中:
- Query context 查詢上下文:用在查詢上下文中的字句回答“這個文檔有多匹配這個查詢?”望艺。除了決定文檔是否匹配找默,字句匹配的文檔還會計算一個字句評分吼驶,來評定文檔有多匹配蟹演,會參與相關性評分酒请。查詢上下文由 query 元素表示。
- Filter context 過濾上下文:過濾上下文由 filter 元素或 bool 中的 must not 表示囤萤。用在過濾上下文中的字句回答“這個文檔是否匹配這個查詢涛舍?”做盅,不參與相關性評分吹榴。被頻繁使用的過濾器將被ES自動緩存图筹,來提高查詢性能远剩。
如下語句:
GET /_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "Search" }},
{ "match": { "content": "Elasticsearch" }}
],
"filter": [
{ "term": { "status": "published" }},
{ "range": { "publish_date": { "gte": "2015-01-01" }}}
]
}
}
}
DSL
query string 后邊的參數(shù)原來越多,搜索條件越來越復雜痢掠,不能滿足需求足画。
GET /book/_search?q=name:java&size=10&from=0&sort=price:desc
DSL:Domain Specified Language淹辞,特定領域的語言象缀。
es特有的搜索語言央星,可在請求體中攜帶搜索條件遍希,功能強大禁谦。
查詢全部 GET /book/_search
GET /book/_search
{
"query": { "match_all": {} }
}
排序 GET /book/_search?sort=price:desc
GET /book/_search
{
"query" : {
"match" : {
"name" : " java"
}
},
"sort": [
{ "price": "desc" }
]
}
分頁查詢 GET /book/_search?size=10&from=0
GET /book/_search
{
"query": { "match_all": {} },
"from": 0,
"size": 1
}
指定返回字段 GET /book/ _search? _source=name,studymodel
GET /book/_search
{
"query": { "match_all": {} },
"_source": ["name", "studymodel"]
}
通過組合以上各種類型查詢州泊,實現(xiàn)復雜查詢遥皂。
Query DSL語法
{
QUERY_NAME: {
ARGUMENT: VALUE,
ARGUMENT: VALUE,...
}
}
{
QUERY_NAME: {
FIELD_NAME: {
ARGUMENT: VALUE,
ARGUMENT: VALUE,...
}
}
}
GET /test_index/_search
{
"query": {
"match": {
"test_field": "test"
}
}
}
組合多個搜索條件
搜索需求:title必須包含elasticsearch,content可以包含elasticsearch也可以不包含样悟,author_id必須不為111
sql where and or !=
初始數(shù)據(jù):
POST /website/_doc/1
{
"title": "my hadoop article",
"content": "hadoop is very bad",
"author_id": 111
}
?
POST /website/_doc/2
{
"title": "my elasticsearch article",
"content": "es is very bad",
"author_id": 112
}
POST /website/_doc/3
{
"title": "my elasticsearch article",
"content": "es is very goods",
"author_id": 111
}
搜索:
GET /website/_doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "elasticsearch"
}
}
],
"should": [
{
"match": {
"content": "elasticsearch"
}
}
],
"must_not": [
{
"match": {
"author_id": 111
}
}
]
}
}
}
更復雜的搜索需求:
select * from test_index where name='tom' or (hired =true and (personality ='good' and rude != true ))
GET /test_index/_search
{
"query": {
"bool": {
"must": { "match":{ "name": "tom" }},
"should": [
{ "match":{ "hired": true }},
{ "bool": {
"must":{ "match": { "personality": "good" }},
"must_not": { "match": { "rude": true }}
}}
],
"minimum_should_match": 1
}
}
}
Match all query
查詢所有
GET /_search
{
"query": {
"match_all": {}
}
}
GET /book/_search
{
"query": {
"match_none": {}
}
}
full-text search 全文檢索
官網(wǎng):https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html
全文檢索,用于對分詞的字段進行搜索震糖。會用查詢字段的分詞器對查詢的文本進行分詞生成查詢吊说∈柽叮可用于短語查詢蚤蔓、模糊查詢秀又、前綴查詢吐辙、臨近查詢等查詢場景昏苏。
新建book索引
PUT /book/
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"name":{
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"description":{
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"studymodel":{
"type": "keyword"
},
"price":{
"type": "double"
},
"timestamp": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"pic":{
"type":"text",
"index":false
}
}
}
}
插入數(shù)據(jù)
PUT /book/_doc/1
{
"name": "Bootstrap開發(fā)",
"description": "Bootstrap是由Twitter推出的一個前臺頁面開發(fā)css框架洼专,是一個非常流行的開發(fā)框架屁商,此框架集成了多種頁面效果蜡镶。此開發(fā)框架包含了大量的CSS官还、JS程序代碼妻枕,可以幫助開發(fā)者(尤其是不擅長css頁面開發(fā)的程序人員)輕松的實現(xiàn)一個css,不受瀏覽器限制的精美界面css效果愕掏。",
"studymodel": "201002",
"price":38.6,
"timestamp":"2019-08-25 19:11:35",
"pic":"group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg",
"tags": [ "bootstrap", "dev"]
}
?
PUT /book/_doc/2
{
"name": "java編程思想",
"description": "java語言是世界第一編程語言饵撑,在軟件開發(fā)領域使用人數(shù)最多滑潘。",
"studymodel": "201001",
"price":68.6,
"timestamp":"2019-08-25 19:11:35",
"pic":"group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg",
"tags": [ "java", "dev"]
}
?
PUT /book/_doc/3
{
"name": "spring開發(fā)基礎",
"description": "spring 在java領域非常流行语卤,java程序員都在用粹舵。",
"studymodel": "201001",
"price":88.6,
"timestamp":"2019-08-24 19:11:35",
"pic":"group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg",
"tags": [ "spring", "java"]
}
match query
全文檢索的標準查詢,它可以對一個字段進行模糊诅需、短語查詢堰塌。 match queries 接收 text/numerics/dates, 對它們進行分詞分析, 再組織成一個boolean查詢蔫仙∫“睿可通過operator 指定bool組合操作(or屎勘、and 默認是 or )施籍, 以及minimum_should_match 指定至少需多少個should(or)字句需滿足。還可用ananlyzer指定查詢用的特殊分析器概漱。
GET /book/_search
{
"query" : {
"match" : {
"description" : "java程序員"
}
}
}
執(zhí)行查詢:
GET /book/_search
{
"query" : {
"match" : {
"description" : "java程序員" #分詞后用or
}
}
}
GET /book/_search
{
"query": {
"match" : {
"description" : {
"query" : "java 程序員",
"operator": "and" #指定分詞后用and
}
}
}
}
模糊查詢丑慎,可以指定fuzziness最大編輯數(shù)
最大編輯數(shù)為2,說明query字符串中分詞后瓤摧,每個詞允許編輯兩次單個字符竿裂,可刪除照弥、新增腻异、修改字符。
fuzziness 參數(shù)可以被設置為 AUTO这揣,此時字符串只有 1 到 2 個字符時是 0悔常;字符串有 3 、4 或者 5 個字符時是 1给赞;字符串大于 5 個字符時是 2机打。
有時編輯距離 2 仍然是太多了,返回的結果似乎并不相關片迅。 把最大fuzziness設置為1 残邀,可以得到更好的結果和更好的性能。
GET /book/_search
{
"query": {
"match": {
"description": {
"query": "va 程序",
"fuzziness": 2
}
}
}
}
關于模糊查詢fuzziness的說明柑蛇,可以參看:https://www.elastic.co/guide/cn/elasticsearch/guide/current/fuzziness.html
還可以使用minimum_should_match指定最少需要滿足幾個詞匹配:
GET /book/_search
{
"query": {
"match": {
"description": {
"query": "av 程序員 spring",
"fuzziness": 2,
"minimum_should_match": 2
}
}
}
}
還可用max_expansions 指定模糊匹配的最大詞項數(shù)罐旗,默認是50。比如:反向索引中有 100 個詞項與 ava
模糊匹配唯蝶,只選用前50 個九秀。
GET /book/_search
{
"query": {
"match": {
"description": {
"query": "ava 程序員 spring",
"fuzziness": 2,
"minimum_should_match": 2,
"max_expansions": 50
}
}
}
}
match phrase query
match_phrase 查詢用來對一個字段進行短語查詢,可以指定 analyzer粘我、slop移動因子鼓蜒,和match的區(qū)別在于:match_query是有順序要求的痹换,而match是無序的。
GET /book/_search
{
"query": {
"match_phrase": {
"description": "java 程序員"
}
}
}
可以通過slop參數(shù)來控制單詞之間的允許間隔
GET /book/_search
{
"query": {
"match_phrase": {
"description": {
"query": "java 程序員",
"slop": 2
}
}
}
}
match phrase prefix query
match_phrase_prefix 在 match_phrase 的基礎上支持對短語的最后一個詞進行前綴匹配
查詢f開頭的:
GET /book/_search
{
"query": {
"match_phrase_prefix" : {
"description" : "spring 在 ja"
}
}
}
指定前綴匹配選用的最大詞項數(shù)量:
GET /book/_search
{
"query": {
"match_phrase_prefix" : {
"message" : {
"description" : "spring 在 ja",
"max_expansions" : 10
}
}
}
}
multi match query
如果你需要在多個字段上進行文本搜索都弹,可用multi_match 娇豫。multi_match在 match的基礎上支持對多個字段進行文本查詢。
GET /book/_search
{
"query": {
"multi_match": {
"query": "java程序員",
"fields": ["name", "description"]
}
}
}
還可以使用*匹配多個字段:
GET /book/_search
{
"query": {
"multi_match": {
"query": "java程序員",
"fields": ["name*", "desc*"]
}
}
}
query string query
query_string 查詢畅厢,讓我們可以直接用lucene查詢語法寫一個查詢串進行查詢冯痢,ES中接到請求后,通過查詢解析器解析查詢串生成對應的查詢框杜。使用它要求掌握lucene的查詢語法浦楣。
GET /book/_search
{
"query": {
"query_string" : {
"default_field" : "description",
"query" : "java 程序員 spring"
}
}
}
query_string支持多字段匹配
GET /book/_search
{
"query": {
"query_string" : {
"fields" : ["description", "name"],
"query" : "java 程序員 spring"
}
}
}
可與query同用的參數(shù),如 default_field咪辱、fields振劳,及query 串的語法請參考:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
simple query string query
simple_query_string 查同 query_string 查詢一樣用lucene查詢語法寫查詢串,較query_string不同的地方:更小的語法集油狂;查詢串有錯誤历恐,它會忽略錯誤的部分,不拋出錯誤专筷。更適合給用戶使用弱贼。
GET /book/_search
{
"query": {
"simple_query_string" : {
"query": "\"fried eggs\" +(eggplant | potato) -frittata",
"fields": ["description^5", "name$"], # 明顯錯誤的寫法,但是不報錯磷蛹,查不出數(shù)據(jù)
"default_operator": "and"
}
}
}
詞項查詢
官網(wǎng):https://www.elastic.co/guide/en/elasticsearch/reference/current/term-level-queries.html
term query
term 查詢用于查詢指定字段包含某個詞項的文檔哮洽。
GET /book/_search
{
"query": {
"term": {
"description": "spring"
}
}
}
terms query
terms 查詢用于查詢指定字段包含某些詞項的文檔。
GET /book/_search
{
"query": {
"terms": {
"tags": [
"search",
"full_text",
"dev"
]
}
}
}
Terms 查詢支持嵌套查詢的方式來獲得查詢詞項弦聂,相當于 in (select term from other)
PUT /users/_doc/2
{
"followers" : ["1", "3"]
}
PUT /tweets/_doc/1
{
"user" : "1"
}
GET /tweets/_search
{
"query" : {
"terms" : {
"user" : {
"index" : "users",
"type" : "_doc",
"id" : "2",
"path" : "followers"
}
}
}
}
嵌套查詢可用參數(shù)說明:
range query
范圍查詢
- gte:大于等于
- gt:大于
- lte:小于等于
- lt:小于
- boost:查詢權重
GET /book/_search
{
"query": {
"range": {
"price": {
"gte": 80,
"lte": 90,
"boost" : 2.0
}
}
}
}
GET /book/_search
{
"query": {
"range" : {
"date" : {
"gte" : "now-1d/d", #當前時間減1天后轉成天數(shù)
"lt" : "now/d" #當前時間轉成天數(shù)
}
}
}
}
GET /book/_search
{
"query": {
"range" : {
"born" : {
"gte": "01/01/2012",
"lte": "2013",
"format": "dd/MM/yyyy||yyyy"
}
}
}
}
時間舍入||說明:
- gt:大于的情況下鸟辅,四舍五入,比如2014-11-18||/M變成2014-11-30T23:59:59:999莺葫,不包含整個月匪凉。
- gte:大于等于的情況下,向下取整捺檬,比如2014-11-18||/M變成2014-11-01再层,包含整個月。
- lt:小于的情況下堡纬,向下取整聂受,比如2014-11-18||/M變成2014-11-01,不包含整個月烤镐。
- lte:小于等于的情況下蛋济,四舍五入,比如2014-11-18||/M變成2014-11-30T23:59:59:999炮叶,包含整個月碗旅。
時間數(shù)學計算規(guī)則請參考:
https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math
exits query
查詢指定字段值不為空的文檔渡处。相當 SQL 中的 column is not null
GET /book/_search
{
"query": {
"exists": {
"field": "description"
}
}
}
prefix query 詞項前綴查詢
GET /book/_search
{
"query": {
"prefix": {
"name": {
"value": "spring"
}
}
}
}
GET /book/_search
{
"query": {
"prefix": {
"name": "spring"
}
}
}
wildcard query 通配符查詢
GET /book/_search
{
"query": {
"wildcard" : { "name" : "spr*g" }
}
}
GET /book/_search
{
"query": {
"wildcard": {
"name" : {
"value": "spr*g",
"boost": 2
}
}
}
}
regexp query 正則查詢
GET /book/_search
{
"query": {
"regexp":{
"name": "sp.*g"
}
}
}
GET /book/_search
{
"query": {
"regexp": {
"description": {
"value": "j.*a",
"flags" : "ALL",
"max_determinized_states": 10000,
"rewrite": "constant_score"
}
}
}
}
fuzzy query 模糊查詢
返回包含與搜索詞類似的詞的文檔,該詞由Levenshtein編輯距離度量祟辟。
包括以下幾種情況:
- 更改角色(box→fox)
- 刪除字符(aple→apple)
- 插入字符(sick→sic)
- 調換兩個相鄰字符(ACT→CAT)
GET /book/_search
{
"query": {
"fuzzy": {
"description": {
"value": "jave"
}
}
}
}
GET /book/_search
{
"query": {
"fuzzy" : {
"name" : {
"value": "sp",
"boost": 1.0,
"fuzziness": 2,
"prefix_length": 0,
"max_expansions": 100
}
}
}
}
ids 根據(jù)文檔id查詢
GET /book/_search
{
"query": {
"ids" : {
"values" : ["1", "4", "100"]
}
}
}
Filter
filter與query示例
需求:用戶查詢description中有"java程序員"医瘫,并且價格大于80小于90的數(shù)據(jù)。
GET /book/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"description": "java程序員"
}
},
{
"range": {
"price": {
"gte": 80,
"lte": 90
}
}
}
]
}
}
}
使用filter:
GET /book/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"description": "java程序員"
}
}
],
"filter": {
"range": {
"price": {
"gte": 80,
"lte": 90
}
}
}
}
}
}
filter與query對比
filter:僅僅只是按照搜索條件過濾出需要的數(shù)據(jù)而已旧困,不計算任何相關度分數(shù)醇份,對相關度沒有任何影響。
query:會去計算每個document相對于搜索條件的相關度吼具,并按照相關度進行排序僚纷。
應用場景:
一般來說,如果你是在進行搜索馍悟,需要將最匹配搜索條件的數(shù)據(jù)先返回畔濒,那么用query 如果你只是要根據(jù)一些條件篩選出一部分數(shù)據(jù)剩晴,不關注其排序锣咒,那么用filter。
filter與query性能
filter:不需要計算相關度分數(shù)赞弥,不需要按照相關度分數(shù)進行排序毅整,同時還有內置的自動cache最常使用filter的數(shù)據(jù)。
query:相反绽左,要計算相關度分數(shù)悼嫉,按照分數(shù)進行排序,而且無法cache結果拼窥。范圍查詢戏蔑,keyword關鍵字查詢。
定位錯誤語法
驗證錯誤語句:
GET /book/_validate/query?explain
{
"query": {
"mach": {
"description": "java程序員"
}
}
}
返回:
{
"valid" : false,
"error" : "org.elasticsearch.common.ParsingException: no [query] registered for [mach]"
}
正確:
GET /book/_validate/query?explain
{
"query": {
"match": {
"description": "java程序員"
}
}
}
返回:
{
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"valid" : true,
"explanations" : [
{
"index" : "book",
"valid" : true,
"explanation" : "description:java description:程序員"
}
]
}
一般用在那種特別復雜龐大的搜索下鲁纠,比如你一下子寫了上百行的搜索总棵,這個時候可以先用validate api去驗證一下,搜索是否合法改含。
合法以后情龄,explain就像mysql的執(zhí)行計劃,可以看到搜索的目標等信息捍壤。
定制排序規(guī)則
默認排序規(guī)則
默認情況下骤视,是按照_score降序排序的。
然而鹃觉,某些情況下专酗,可能沒有用到_score,比如說filter盗扇。
但是query里面直接寫filter會報錯笼裳,這時就用到了constant_score唯卖。
只過濾的正確寫法:
GET /book/_search
{
"query": {
"constant_score": {
"filter" : {
"term" : {
"studymodel" : "201001"
}
}
}
}
}
定制排序規(guī)則
相當于sql中order by ?sort=sprice:desc
GET /book/_search
{
"query": {
"constant_score": {
"filter" : {
"term" : {
"studymodel" : "201001"
}
}
}
},
"sort": [
{
"price": {
"order": "asc"
}
}
]
}
Text字段排序問題
如果對一個text field進行排序,結果往往不準確躬柬,因為分詞后是多個單詞拜轨,再排序就不是我們想要的結果了。
通常解決方案是允青,將一個text field建立兩次索引橄碾,一個分詞,用來進行搜索颠锉;一個不分詞法牲,用來進行排序。
fielddate:true
PUT /website
{
"mappings": {
"properties": {
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"content": {
"type": "text"
},
"post_date": {
"type": "date"
},
"author_id": {
"type": "long"
}
}
}
}
插入數(shù)據(jù)
PUT /website/_doc/1
{
"title": "first article",
"content": "this is my second article",
"post_date": "2019-01-01",
"author_id": 110
}
?
PUT /website/_doc/2
{
"title": "second article",
"content": "this is my second article",
"post_date": "2019-01-01",
"author_id": 110
}
?
PUT /website/_doc/3
{
"title": "third article",
"content": "this is my third article",
"post_date": "2019-01-02",
"author_id": 110
}
搜索
GET /website/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"title.keyword": {
"order": "desc"
}
}
]
}
Scroll分批查詢
場景:下載某一個索引中1億條數(shù)據(jù)琼掠,到文件或是數(shù)據(jù)庫拒垃。
不能一下全查出來,系統(tǒng)內存溢出瓷蛙。所以使用scoll滾動搜索技術悼瓮,一批一批查詢。
scoll搜索會在第一次搜索的時候艰猬,保存一個當時的視圖快照横堡,之后只會基于該舊的視圖快照提供數(shù)據(jù)搜索,如果這個期間數(shù)據(jù)變更冠桃,是不會讓用戶看到的命贴。
每次發(fā)送scroll請求,我們還需要指定一個scoll參數(shù)食听,指定一個時間窗口胸蛛,每次搜索請求只要在這個時間窗口內能完成就可以了。
搜索
GET /book/_search?scroll=1m
{
"query": {
"match_all": {}
},
"size": 3
}
返回
{
"_scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAMOkWTURBNDUtcjZTVUdKMFp5cXloVElOQQ==",
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
]
}
}
獲得的結果會有一個scoll_id樱报,下一次再發(fā)送scoll請求的時候葬项,必須帶上這個scoll_id
GET /_search/scroll
{
"scroll": "1m",
"scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAMOkWTURBNDUtcjZTVUdKMFp5cXloVElOQQ=="
}
與分頁區(qū)別:
- 分頁給用戶看的 deep paging
- scroll是用戶系統(tǒng)內部操作,如下載批量數(shù)據(jù)肃弟,數(shù)據(jù)轉移玷室。零停機改變索引映射。
復合查詢
官網(wǎng):https://www.elastic.co/guide/en/elasticsearch/reference/current/compound-queries.html
constant score query
用來包裝另一個查詢笤受,將查詢匹配的文檔的評分設為一個常值穷缤。
GET /_search
{
"query": {
"constant_score" : {
"filter" : {
"term" : { "user" : "kimchy"}
},
"boost" : 1.2
}
}
}
bool query
復合查詢就是指可以對多個字段過濾篩選,類比mysql的where多條件查詢箩兽,es的復合查詢包括Constant Score Query津肛、Bool Query、Dis Max Query汗贫、Function Score Query身坐、Boosting Query秸脱,這里詳細說一說用的比較多的Bool Query。
Bool 查詢用bool操作來組合多個查詢字句為一個查詢部蛇。 可用的關鍵字:
must:根據(jù)must中的條件過濾文檔摊唇,返回的結果文檔必須嚴格匹配條件,會影響相關性算分涯鲁。
filter:根據(jù)must中的條件過濾文檔巷查,返回的結果文檔必須嚴格匹配條件,和must不同的是抹腿,filter不會影響相關性算分岛请。
should:或,根據(jù)should中的條件進行篩選警绩,返回的結果文檔應該包含should的條件崇败,影響相關性算分。
must_not:根據(jù)must_not中的條件過濾文檔肩祥,返回的結果文檔必須不包含must_not條件后室,會影響相關性算分,在filter上下文中執(zhí)行搭幻,不參與咧擂、不影響評分逞盆。
GET /book/_search
{
"query": {
"bool" : {
"must" : {
"term" : { "name" : "spring" }
},
"filter": {
"term" : { "name" : "spring" }
},
"must_not" : {
"range" : {
"price" : { "gte" : 10, "lte" : 20 }
}
},
"should" : [
{ "term" : { "tag" : "spring" } },
{ "term" : { "tag" : "java" } }
],
"minimum_should_match" : 4, # 表示命中4個詞的文檔才會返回
"boost" : 1.0
}
}
}
1檀蹋、must、must_not云芦、should支持數(shù)組俯逾,同時filter的查詢語句,es會對其進行智能緩存舅逸,因此執(zhí)行效率較高桌肴,在不需要算分的查詢語句中,可以考慮使用filter替代普通的query語句;
2琉历、查詢語句同時包含must和should時坠七,可以不滿足should的條件,因為must條件優(yōu)先級高于should旗笔,但是如果也滿足should的條件彪置,則會提高相關性算分;
3、可以使用minimum_should_match參數(shù)來控制應當滿足條件的個數(shù)或百分比;
4蝇恶、must拳魁、must_not語句里面如果包含多個條件,則各個條件間是且的關系撮弧,而should的多個條件是或的關系潘懊。
參考:
https://blog.csdn.net/supermao1013/article/details/84261526
https://blog.csdn.net/fy_java1995/article/details/106674644