1. 假设数据
doc1:
title: black cat.
content: The cat like eat fish.
doc2:
title: red dog.
content: The dog like eat bone.
doc3:
title: yellow cat.
content: yellow cat look good.
doc4:
title: white dog.
content: People like white dog.
doc5:
title: animal.
content: cat is animal, and dog is also animal.
2. 查询需求
查询title或content中包含cat或is的帖子。
GET /forum/article/_search
{
"query": {
"bool": {
"should": [
{ "match": { "title": "cat is" }},
{ "match": { "content": "cat is" }}
]
}
}
}
3.结果分析
查询结果排序可能是:doc1>doc3>doc5
4. 原因分析
计算每个document的relevance score:每个query的分数相加,乘以matched query数量,除以总query数量。
doc1----> title查询分数:1.1,content查询分数:1.2,两者相加得2.3;matched query数量为2(title和content各1次),总查询数量为2(title和content各被查询了1次)。relevance score:2.3 * 2 / 2 = 2.3
doc3与doc1类似。
doc5----> title查询分数:没有match到,为0,content查询分数:2.3(cat和is都匹配到了,分数较高),两者相加得2.3;matched query数量为1(content 1次),总查询数量为2(title和content各被查询了1次)。relevance score:2.3 * 1 / 2 = 1.15
5. best fields策略
如果我们想要到查询结果是:一个filed中能够匹配到尽可能多的关键字,则排在前面,即让doc5排在前面,则可以使用下面的查询方式:
GET /forum/article/_search
{
"query": {
"dis_max": {
"queries": [
{ "match": { "title": "cat is" }},
{ "match": { "content": "cat is" }}
]
}
}
}
这样就会直接以最大的filed分数作为relevance score,那么doc1的relevance score分数为1.2,doc5的relevance score分数为2.3