为什么Elasticsearch会为词条查询计算分数? [英] why does elasticsearch calculates score for term queries?

查看:72
本文介绍了为什么Elasticsearch会为词条查询计算分数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于一个使用术语查询的唯一字段值来进行简单查询.例如:

I want to make a simple query based on knowing a unique field value using a term query. For instance:

{
  "query": {
    "term": {
      "products.product_id": {
        "value": "Ubsdf-234kjasdf"
      }
    }
  }
}

关于术语查询,Elasticsearch文档指出:

Regarding term queries, Elasticsearch documentation states:

返回在提供的字段中包含确切术语的文档.您可以使用术语查询根据精确度值(例如价格,产品ID或用户名)来查找文档.

Returns documents that contain an exact term in a provided field. You can use the term query to find documents based on a precise value such as a price, a product ID, or a username.

另一方面,文档还建议针对相关性很重要的查询计算 _score (对于涉及完全匹配的过滤器上下文则不是这种情况).

On the other hand, documentation also suggests that the _score is calculated for queries where relevancy matters (and is not the case for filter context which involves exact match).

我觉得有点混乱.为什么Elasticsearch为应该与完全匹配相关而不相关的词条查询计算 _score ?

I find it a bit confusing. Why does Elasticsearch calculates _score for term queries which are supposed to be concerned with exact match and not relevancy?

推荐答案

项查询不进行分析,因此它们不会进入分析阶段,因此用于精确匹配,但在以下情况下仍会计算其分数在查询上下文中使用.

term queries are not analyzed, hence they would not go with the analysis phase, hence used for an exact match, but their score is still calculated when used in query context.

在过滤器上下文中使用术语查询时,这意味着您不是在搜索它们,而是对其进行过滤,因此不会为它们计算分数.

When you use term queries in filter context, then it means you are not searching on them, and rather doing filtering on them, hence there is no score calculated for them.

有关查询和过滤器上下文的详细信息在官方ES文档中.

下面的示例中都显示了过滤器和查询上下文中的术语查询示例

Both the example of term query in filter and query context shown in my below example

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "title": "c"
                    }
                }
            ]
        }
    },
    "size": 10
}

结果得分

"hits": [
            {
                "_index": "cpp",
                "_type": "_doc",
                "_id": "4",
                "_score": 0.2876821, --> notice score is calculated
                "_source": {
                    "title": "c"
                }
            }
        ]

过滤条件中的词条查询

{
    "query": {
        "bool": {
            "filter": [ --> prev cluase replaced by `filter`
                {
                    "term": {
                        "title": "c"
                    }
                }
            ]
        }
    },
    "size": 10
}

以及具有过滤条件的搜索结果

 "hits": [
            {
                "_index": "cpp",
                "_type": "_doc",
                "_id": "4",
                "_score": 0.0, --> notice score is 0.
                "_source": {
                    "title": "c"
                }
            }
        ]

这篇关于为什么Elasticsearch会为词条查询计算分数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆