国产韩国精品一区二区三区,一本色道综合久久加勒比,亚洲欧洲中文日韩久久av乱码,国产一区二区女内射,亚洲精品无码成人片在线观看

es怎么查詢(xún) - es哪些查詢(xún)

基本查詢(xún)

基本語(yǔ)法:

GET /索引庫名/_search
{"query":{"查詢(xún)類(lèi)型":{"查詢(xún)條件":"查詢(xún)條件值"}
}
}

這里的 query 代表一個(gè)查詢(xún)對象,里面可以有不同的查詢(xún)屬性。

查詢(xún)類(lèi)型:match_all、match、term、range 等等。

查詢(xún)條件:查詢(xún)條件會(huì )根據類(lèi)型的不同,寫(xiě)法也有差異。

查詢(xún)所有(match_all)

示例:

GET /renda/_search
{"query":{"match_all": {}
}
}

query:代表查詢(xún)對象。

match_all:代表查詢(xún)所有。

結果:

{"took":1,"timed_out":false,"_shards": {"total":5,"successful":5,"skipped":0,"failed":0},"hits": {"total":3,"max_score":1,"hits": [
{"_index":"renda","_type":"goods","_id":"2","_score":1,"_source": {"title":"白米手機","images":"","price":2699}
},
{"_index":"renda","_type":"goods","_id":"gPeQqHUB-UTJAEEuqOm9","_score":1,"_source": {"title":"小米手機","images":"","price":2699}
},
{"_index":"renda","_type":"goods","_id":"3","_score":1,"_source": {"title":"超大米手機","images":"","price":3299,"stock":200,"saleable":true,"subTitle":"大米"}
}
]
}
}

結果解析:

took- 檢索所耗費的時(shí)間,單位是毫秒。timed_out- 是否超時(shí)。?_shards- 分片信息。?hits- 命中結果,檢索結果信息。total- 搜索到的總條數。max_score- 所有結果中文檔得分的最高分。hits- 搜索結果的文檔對象數組,每個(gè)元素是一條搜索到的文檔信息。_index- 索引庫。_type- 文檔類(lèi)型。_id- 文檔 id。_score- 評分;索引庫的一個(gè)概念;關(guān)聯(lián)度。_source- 原始數據。

文檔得分:使用 ES 時(shí),對于查詢(xún)出的文檔無(wú)疑會(huì )有文檔相似度之別;而理想的排序是和查詢(xún)條件相關(guān)性越高排序越靠前,而這個(gè)排序的依據就是_score。

匹配查詢(xún)(match)

加入一條數據用于測試:

PUT /renda/goods/3
{"title":"小米電視4A","images":"","price": 3899.00
}

索引庫中有 3 部手機,1 臺電視。

match類(lèi)型查詢(xún),會(huì )把查詢(xún)條件進(jìn)行分詞,然后進(jìn)行查詢(xún),多個(gè)詞條之間是or的關(guān)系:

GET /renda/_search
{"query": {"match": {"title":"小米電視"}
}
}

響應結果:

{"took":15,"timed_out":false,"_shards": {"total":5,"successful":5,"skipped":0,"failed":0},"hits": {"total":2,"max_score":0.5753642,"hits": [
{"_index":"renda","_type":"goods","_id":"3","_score":0.5753642,"_source": {"title":"小米電視4A","images":"","price":3899}
},
{"_index":"renda","_type":"goods","_id":"gPeQqHUB-UTJAEEuqOm9","_score":0.2876821,"_source": {"title":"小米手機","images":"","price":2699}
}
]
}
}

在上面的案例中,不僅會(huì )查詢(xún)到電視,而且與小米相關(guān)的都會(huì )查詢(xún)到,多個(gè)詞之間是 or 的關(guān)系。

某些情況下,需要更精確查找,即 and 關(guān)系。比如在電商平臺精確搜索商品時(shí),希望這個(gè)關(guān)系(查詢(xún)條件切分詞之后的關(guān)系)變成 and,可以這樣做:

GET /renda/_search
{"query": {"match": {"title": {"query":"小米電視","operator":"and"}
}
}
}

響應結果:

{"took":8,"timed_out":false,"_shards": {"total":5,"successful":5,"skipped":0,"failed":0},"hits": {"total":1,"max_score":0.5753642,"hits": [
{"_index":"renda","_type":"goods","_id":"3","_score":0.5753642,"_source": {"title":"小米電視4A","images":"","price":3899}
}
]
}
}

此時(shí),只有同時(shí)包含小米和電視的詞條才會(huì )被搜索到。

詞條匹配(term)

term查詢(xún)被用于精確值匹配,這些精確值可能是數字、時(shí)間、布爾,或者那些未分詞的字符串、keyword 類(lèi)型的字符串。

效果類(lèi)似于:select * from tableName where colName=value;

GET /renda/_search
{"query":{"term":{"price": 2699.00
}
}
}

響應結果:

{"took":6,"timed_out":false,"_shards": {"total":5,"successful":5,"skipped":0,"failed":0},"hits": {"total":2,"max_score":1,"hits": [
{"_index":"renda","_type":"goods","_id":"2","_score":1,"_source": {"title":"白米手機","images":"","price":2699}
},
{"_index":"renda","_type":"goods","_id":"gPeQqHUB-UTJAEEuqOm9","_score":1,"_source": {"title":"小米手機","images":"","price":2699}
}
]
}
}

布爾組合(bool)

bool把各種其它查詢(xún)通過(guò)must - 與、must_not -非、should - 或的方式進(jìn)行組合。

GET /renda/_search
{"query":{"bool":{"must": {"match": {"title":"小米"}
},"must_not": {"match": {"title":"電視"}
},"should": {"match": {"title":"手機"}
}
}
}
}

響應結果:

{"took":2,"timed_out":false,"_shards": {"total":5,"successful":5,"skipped":0,"failed":0},"hits": {"total":1,"max_score":0.5753642,"hits": [
{"_index":"renda","_type":"goods","_id":"gPeQqHUB-UTJAEEuqOm9","_score":0.5753642,"_source": {"title":"小米手機","images":"","price":2699}
}
]
}
}

范圍查詢(xún)(range)

range查詢(xún)找出那些落在指定區間內的數字或者時(shí)間。

GET/renda/_search{"query":{"range":{"price":{"gte":3000.0,"lt":4000.00}}}}

響應結果:

{"took":1,"timed_out":false,"_shards": {"total":5,"successful":5,"skipped":0,"failed":0},"hits": {"total":1,"max_score":1,"hits": [
{"_index":"renda","_type":"goods","_id":"3","_score":1,"_source": {"title":"小米電視4A","images":"","price":3899}
}
]
}
}

range查詢(xún)允許以下字符:

gt- 大于gte- 大于等于lt- 小于lte- 小于等于

模糊查詢(xún)(fuzzy)

fuzzy查詢(xún)是term查詢(xún)的模糊等價(jià),很少直接使用它。

新增一個(gè)商品:

POST /renda/goods/5
{"title":"Apple手機","images":"","price": 6899.00
}

響應結果:

{"_index":"renda","_type":"goods","_id":"5","_version":1,"result":"created","_shards": {"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":2}

fuzzy查詢(xún)是term查詢(xún)的模糊等價(jià),它允許用戶(hù)搜索詞條與實(shí)際詞條的拼寫(xiě)出現偏差,但是偏差的編輯距離不得超過(guò)2:

GET /renda/_search
{"query": {"fuzzy": {"title":"applas"}
}
}

上面的查詢(xún),也能查詢(xún)到 apple 手機:

{"took":4,"timed_out":false,"_shards": {"total":5,"successful":5,"skipped":0,"failed":0},"hits": {"total":1,"max_score":0.17260925,"hits": [
{"_index":"renda","_type":"goods","_id":"5","_score":0.17260925,"_source": {"title":"Apple手機","images":"","price":6899}
}
]
}
}

結果過(guò)濾

默認情況下,Elasticsearch 在搜索的結果中,會(huì )把文檔中保存在_source的所有字段都返回。

如果只想獲取其中的部分字段,可以添加_source的過(guò)濾。

直接指定字段

示例:

GET /renda/_search
{"_source": ["title","price"],"query": {"term": {"price": 2699
}
}
}

返回的結果:

{"took":2,"timed_out":false,"_shards": {"total":5,"successful":5,"skipped":0,"failed":0},"hits": {"total":2,"max_score":1,"hits": [
{"_index":"renda","_type":"goods","_id":"2","_score":1,"_source": {"price":2699,"title":"白米手機"}
},
{"_index":"renda","_type":"goods","_id":"gPeQqHUB-UTJAEEuqOm9","_score":1,"_source": {"price":2699,"title":"小米手機"}
}
]
}
}

指定 includes 和 excludes

includes:來(lái)指定想要顯示的字段。

excludes:來(lái)指定不想要顯示的字段。

二者都是可選的。

示例:

GET /renda/_search
{"_source": {"includes":["title","price"]
},"query": {"term": {"price": 2699
}
}
}

與下面的結果將是一樣的:

GET /renda/_search
{"_source": {"excludes": ["images"]
},"query": {"term": {"price": 2699
}
}
}

響應結果:

{"took":1,"timed_out":false,"_shards": {"total":5,"successful":5,"skipped":0,"failed":0},"hits": {"total":2,"max_score":1,"hits": [
{"_index":"renda","_type":"goods","_id":"2","_score":1,"_source": {"price":2699,"title":"白米手機"}
},
{"_index":"renda","_type":"goods","_id":"gPeQqHUB-UTJAEEuqOm9","_score":1,"_source": {"price":2699,"title":"小米手機"}
}
]
}
}

過(guò)濾(Filter)

Elasticsearch 使用的查詢(xún)語(yǔ)言(DSL)擁有一套查詢(xún)組件,這些組件可以以無(wú)限組合的方式進(jìn)行搭配。

這套組件可以在以下兩種情況下使用:過(guò)濾情況 - ?ltering context 和查詢(xún)情況 - query context。

如何選擇查詢(xún)與過(guò)濾:

通常的規則是,使用查詢(xún)(query)語(yǔ)句來(lái)進(jìn)行全文搜索或者其它任何需要影響相關(guān)性得分的搜索;除此以外的情況都使用過(guò)濾(?lters)。

條件查詢(xún)中進(jìn)行過(guò)濾:

所有的查詢(xún)都會(huì )影響到文檔的評分及排名。如果需要在查詢(xún)結果中進(jìn)行過(guò)濾,并且不希望過(guò)濾條件影響評分,那么就不要把過(guò)濾條件作為查詢(xún)條件來(lái)用,而是使用 filter 方式:

GET /renda/_search
{"query":{"bool":{"must":{"match": {"title":"小米手機"}},"filter":{"range":{"price":{"gt":2000.00,"lt":3800.00}}
}
}
}
}

無(wú)查詢(xún)條件,直接過(guò)濾:

如果一次查詢(xún)只有過(guò)濾,沒(méi)有查詢(xún)條件,不希望進(jìn)行評分,可以使用 constant_score 取代只有 ?lter 語(yǔ)句的 bool 查詢(xún)。在性能上是完全相同的,但對于提高查詢(xún)簡(jiǎn)潔性和清晰度有很大幫助。

GET /renda/_search
{"query":{"constant_score": {"filter": {"range": {"price":{"gt":2000.00,"lt":3000.00}}
}
}
}
}

排序

單字段排序

sort可以按照不同的字段進(jìn)行排序,并且通過(guò)order指定排序的方式。

GET /renda/_search
{"query": {"match": {"title":"小米手機"}
},"sort": [
{"price": {"order":"desc"}
}
]
}

多字段排序

假定想要結合使用price和_score進(jìn)行查詢(xún),并且匹配的結果首先按照價(jià)格排序,然后按照相關(guān)性得分排序:

GET /renda/_search
{"query":
{"bool":
{"must":
{"match":
{"title":"小米手機"}
},"filter":{"range":
{"price":
{"gt":2000,"lt":4000
}
}
}
}
},"sort": [
{"price":
{"order":"desc"}
},
{"_score":
{"order":"desc"}

}
]
}

分頁(yè)

Elasticsearch 中數據都存儲在分片中,當執行搜索時(shí)每個(gè)分片獨立搜索后,數據再經(jīng)過(guò)整合返回。那么,如何實(shí)現分頁(yè)查詢(xún)呢?

Elasticsearch 的分頁(yè)與 MySQL 數據庫非常相似,都是指定兩個(gè)值:

from- 目標數據的偏移值(開(kāi)始位置),默認 from 為 0。size- 每頁(yè)大小。GET/renda/_search{"query":{"match_all":{}},"sort":[{"price":{"order":"asc"}}],"from":3,"size":3}

結果:

{"took":1,"timed_out":false,"_shards": {"total":5,"successful":5,"skipped":0,"failed":0},"hits": {"total":4,"max_score":null,"hits": [
{"_index":"renda","_type":"goods","_id":"5","_score":null,"_source": {"title":"Apple手機","images":"","price":6899},"sort": [6899]
}
]
}
}

高亮

高亮原理:

服務(wù)端搜索數據,得到搜索結果。把搜索結果中,搜索關(guān)鍵字都加上約定好的標簽。前端頁(yè)面提前寫(xiě)好標簽的 CSS 樣式,即可高亮。

Elasticsearch 中實(shí)現高亮的語(yǔ)法比較簡(jiǎn)單:

GET /renda/_search
{"query": {"match": {"title":"手機"}
},"highlight": {"pre_tags":"","post_tags":"","fields": {"title": {}
}
}
}

在使用 match 查詢(xún)的同時(shí),加上一個(gè) highlight 屬性:

pre_tags:前置標簽
post_tags:后置標簽
?elds:需要高亮的字段
title:這里聲明 title 字段需要高亮

結果:

{"took":2,"timed_out":false,"_shards": {"total":5,"successful":5,"skipped":0,"failed":0},"hits": {"total":3,"max_score":0.2876821,"hits": [
{"_index":"renda","_type":"goods","_id":"5","_score":0.2876821,"_source": {"title":"Apple手機","images":"","price":6899},"highlight": {"title": ["Apple手機"]
}
},
{"_index":"renda","_type":"goods","_id":"2","_score":0.2876821,"_source": {"title":"白米手機","images":"","price":2699},"highlight": {"title": ["白米手機"]
}
},
{"_index":"renda","_type":"goods","_id":"gPeQqHUB-UTJAEEuqOm9","_score":0.2876821,"_source": {"title":"小米手機","images":"","price":2699},"highlight": {"title": ["小米手機"]
}
}
]
}
}

想了解更多,歡迎關(guān)注我的微信公眾號:Renda_Zhang

上一篇:

下一篇:

? 推薦閱讀

分享
国产韩国精品一区二区三区,一本色道综合久久加勒比,亚洲欧洲中文日韩久久av乱码,国产一区二区女内射,亚洲精品无码成人片在线观看