或然最大化查询
或然最大化(dis_max
)查询返回匹配一个或多个查询子句的任何文档。对于匹配多个查询子句的文档,相关性分数设置为所有匹配查询子句中的最高相关性分数。
当返回文档的相关性分数相同时,您可以使用 tie_breaker
参数为匹配多个查询子句的文档赋予更多权重。
示例
考虑一个包含以下两个文档的索引,您将它们索引如下:
PUT testindex1/_doc/1
{
"title": " The Top 10 Shakespeare Poems",
"description": "Top 10 sonnets of England's national poet and the Bard of Avon"
}
PUT testindex1/_doc/2
{
"title": "Sonnets of the 16th Century",
"body": "The poems written by various 16-th century poets"
}
使用 dis_max
查询搜索包含词语“Shakespeare poems”的文档
GET testindex1/_search
{
"query": {
"dis_max": {
"queries": [
{ "match": { "title": "Shakespeare poems" }},
{ "match": { "body": "Shakespeare poems" }}
]
}
}
}
响应包含这两个文档
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.3862942,
"hits": [
{
"_index": "testindex1",
"_id": "1",
"_score": 1.3862942,
"_source": {
"title": " The Top 10 Shakespeare Poems",
"description": "Top 10 sonnets of England's national poet and the Bard of Avon"
}
},
{
"_index": "testindex1",
"_id": "2",
"_score": 0.2876821,
"_source": {
"title": "Sonnets of the 16th Century",
"body": "The poems written by various 16-th century poets"
}
}
]
}
}
参数
下表列出了 dis_max
查询支持的所有顶级参数。
参数 | 描述 |
---|---|
查询 | 一个或多个查询子句的数组,用于匹配文档。文档必须至少匹配一个查询子句才能在结果中返回。如果文档匹配多个查询子句,则相关性分数设置为所有匹配查询子句中的最高相关性分数。必需。 |
tie_breaker | 一个介于 0 和 1.0 之间的浮点因子,用于为匹配多个查询子句的文档赋予更多权重。在这种情况下,文档的相关性分数使用以下算法计算:取所有匹配查询子句中的最高相关性分数,将所有其他匹配子句的分数乘以 tie_breaker 值,然后将这些相关性分数相加并进行归一化。可选。默认为 0(表示仅最高分有效)。 |