这是我参与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"
}
}
]
}
}
复制代码