我正在運行elasticsearch:7.6.2
我有一個包含 4 個簡單檔案的索引:
PUT demo_idx/_doc/1
{
"content": "Distributed nature, simple REST APIs, speed, and scalability, Elasticsearch is the central component of the Elastic Stack, the end"
}
PUT demo_idx/_doc/2
{
"content": "Distributed tmp nature, simple REST APIs, speed, and scalability"
}
PUT demo_idx/_doc/3
{
"content": "Distributed nature, simple REST APIs, speed, and scalability"
}
PUT demo_idx/_doc/4
{
"content": "Distributed tmp tmp nature"
}
我想搜索 text: distributed nature并獲得以下結果順序:
Doc id: 3
Doc id: 1
Doc id: 2
Doc id: 4
ie 完全匹配的檔案(doc 3 & doc 1)將在具有小斜率的檔案(doc 2)之前顯示,具有大斜率的檔案將最后顯示(doc 4)
我讀了這篇文章: 如何構建一個將單詞之間的距離和單詞的精確度考慮在內的 Elasticsearch 查詢,但這對我沒有幫助
我嘗試了以下搜索查詢:
"query": {
"bool": {
"must":
[{
"match_phrase": {
"content": {
"query": query,
"slop": 2
}
}
}]
}
}
但它沒有給我所需的結果。
我得到以下結果:
Doc id: 3 ,Score: 0.22949813
Doc id: 4 ,Score: 0.15556586
Doc id: 1 ,Score: 0.15401536
Doc id: 2 ,Score: 0.14397088
如何撰寫查詢以獲得我想要的結果?
uj5u.com熱心網友回復:
您可以使用 bool should 子句顯示與“分布式性質”完全匹配的檔案。第一個條款將提高那些與“分布式性質”完全匹配的檔案的分數,沒有任何遺漏。
POST demo_idx/_search
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"content": {
"query": "Distributed nature"
}
}
},
{
"match_phrase": {
"content": {
"query": "Distributed nature",
"slop": 2
}
}
}
]
}
}
}
搜索回應將是:
"hits" : [
{
"_index" : "demo_idx",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.45899627,
"_source" : {
"content" : "Distributed nature, simple REST APIs, speed, and scalability"
}
},
{
"_index" : "demo_idx",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.30803072,
"_source" : {
"content" : "Distributed nature, simple REST APIs, speed, and scalability, Elasticsearch is the central component of the Elastic Stack, the end"
}
},
{
"_index" : "demo_idx",
"_type" : "_doc",
"_id" : "4",
"_score" : 0.15556586,
"_source" : {
"content" : "Distributed tmp tmp nature"
}
},
{
"_index" : "demo_idx",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.14397088,
"_source" : {
"content" : "Distributed tmp nature, simple REST APIs, speed, and scalability"
}
}
]
更新1:
為了避免“欄位長度”引數對搜索查詢評分的影響,您需要禁用“內容”欄位的“規范”引數,使用更新映射 API
PUT demo_idx/_mapping
{
"properties": {
"content": {
"type": "text",
"norms": "false"
}
}
}
After this, reindex the documents again, so that norms will not be removed instantly
Now hit the search query, the search response will be in the order you expect to get.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/452254.html
標籤:弹性搜索
