沿用該文章里的數據https://www.cnblogs.com/MRLL/p/12691763.html
查詢時發現,一模一樣的name,但是相關度不一樣
GET /z_test/doc/_search { "explain": false, "query": { "match_phrase": { "name": "測試" } } }
結果
{ "took" : 5, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 5, "max_score" : 0.5753642, "hits" : [ { "_index" : "z_test", "_type" : "doc", "_id" : "D4eQcnEBf_xjEc-wO9P0", "_score" : 0.5753642, "_source" : { "name" : "測試123" } }, { "_index" : "z_test", "_type" : "doc", "_id" : "2oeLcnEBf_xjEc-wFNK2", "_score" : 0.5753642, "_source" : { "name" : "測試" } }, { "_index" : "z_test", "_type" : "doc", "_id" : "_analyze", "_score" : 0.45840853, "_source" : { "name" : "測試" } }, { "_index" : "z_test", "_type" : "doc", "_id" : "qHeKcnEBvg5mZsCPxwX1", "_score" : 0.3672113, "_source" : { "name" : "測試" } }, { "_index" : "z_test", "_type" : "doc", "_id" : "AVSTcnEBjEFwhOIJHS0S", "_score" : 0.33573607, "_source" : { "name" : "測試1" } } ] } }
查詢文檔后得知,在相關度分值的計算中有個屬性為逆向文檔頻率,意思為該搜索字段在整個索引的文檔里出現的頻率,出現的越多所占分值權重越低
參照該文章https://blog.csdn.net/paditang/article/details/79098830
解決辦法為用以下查詢
GET /z_test/doc/_search?search_type=dfs_query_then_fetch { "explain": false, "query": { "match": { "name": {"query": "測試"} } } }
dfs_query_then_fetch意為使用全局的文檔信息打分
默認查詢參數為query then fetch
- 發送查詢到每個shard
- 找到所有匹配的文檔,並使用本地的Term/Document Frequency信息進行打分
- 對結果構建一個優先隊列(排序,標頁等)
- 返回關於結果的元數據到請求節點。注意,實際文檔還沒有發送,只是分數
- 來自所有shard的分數合並起來,並在請求節點上進行排序,文檔被按照查詢要求進行選擇
- 最終,實際文檔從他們各自所在的獨立的shard上檢索出來
- 結果被返回給用戶
dfs_query_then_fetch
- 預查詢每個shard,詢問Term和Document frequency
- 發送查詢到每隔shard
- 找到所有匹配的文檔,並使用全局的Term/Document Frequency信息進行打分
- 對結果構建一個優先隊列(排序,標頁等)
- 返回關於結果的元數據到請求節點。注意,實際文檔還沒有發送,只是分數
- 來自所有shard的分數合並起來,並在請求節點上進行排序,文檔被按照查詢要求進行選擇
- 最終,實際文檔從他們各自所在的獨立的shard上檢索出來
- 結果被返回給用戶
延伸:
詞頻:查詢字段出現的越多分數越高,如果不在意詞在某個字段中出現的頻次,而只在意是否出現過,則可以在字段映射中禁用詞頻統計:
PUT /my_index { "mappings": { "doc": { "properties": { "name": { "type": "string", "index_options": "docs" } } } } }