這是我參與8月更文挑戰的第7天,活動詳情查看:8月更文挑戰markdown
本Elasticsearch相關文章的版本爲:7.4.2elasticsearch
圖書館收藏了一些書籍,相關信息存儲在book索引內,下面是兩個文檔信息:post
POST /book/_doc/1
{
"body": "elasticsearch filter",
"title": "elasticsearch basic query"
}
複製代碼
POST /book/_doc/2
{
"body": "single value search",
"title": "elasticsearch aggs query"
}
複製代碼
小明同窗想查詢elasticsearch聚合查詢方面相關的知識: 若是使用should對body和title進行match查詢:spa
POST /book/_search
{
"query": {
"bool": {
"should": [
{"match": {"body": "elasticsearch aggs"}},
{"match": {"title": "elasticsearch aggs"}}
]
}
}
}
複製代碼
你會以爲文檔2會更符合小明的須要,可是返回的查詢結果的相關性得分缺失文檔1高於文檔2:code
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.9372343,
"hits" : [
{
"_index" : "book",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.9372343,
"_source" : {
"body" : "elasticsearch filter",
"title" : "elasticsearch basic query"
}
},
{
"_index" : "book",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.87546873,
"_source" : {
"body" : "single value search",
"title" : "elasticsearch aggs query"
}
}
]
}
}
複製代碼
那是由於should查詢的相關性得分計算過程是:orm
那麼,如何才能讓匹配度更高的文檔2的相關性得分高於文檔1呢? 那麼須要使用dis_max查詢了。
dis_max查詢的得分計算:
以最佳匹配的子句的得分做爲整個文檔的相關性得分。索引
POST /book/_search?size=1000
{
"query": {
"dis_max": {
"tie_breaker": 0.3,
"queries": [
{"match": {"body": "elasticsearch aggs"}},
{"match": {"title": "elasticsearch aggs"}}
]
}
}
}
複製代碼
那麼,dis_max查詢過程當中文檔1和文檔2的得分計算過程爲:ip
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.87546873,
"hits" : [
{
"_index" : "book",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.87546873,
"_source" : {
"body" : "single value search",
"title" : "elasticsearch aggs query"
}
},
{
"_index" : "book",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.80960923,
"_source" : {
"body" : "elasticsearch filter",
"title" : "elasticsearch basic query"
}
}
]
}
}
複製代碼