简体   繁体   English

或与elasticsearch查询

[英]OR query with elasticsearch

I have an index with "name" and "description" filed. 我有一个带有“名称”和“描述”的索引。 I am running a Boolean query against my index. 我正在对索引运行布尔查询。 Sometimes the term is present in both name and description fields, in this case the documents in which both the name and description contains the search term are scored higher compared to the ones having either the name or the description having the search term. 有时,该术语同时出现在名称和描述字段中,在这种情况下,名称和描述中都包含搜索词的文档的得分要比具有名称或描述和搜索词的文档更高。

What I want is to score them equal. 我要给他们评分。 So the the documents with either name or description having the term has the same score as the document having the search term present in both name and description. 因此,具有名称或描述且具有该术语的文档的得分与具有在名称和描述中均具有搜索术语的文档的得分相同。

Is it possible? 可能吗?

Here is the example: 这是示例:

{
    "name":     "xyz",
    "description":  "abc xyz"
},
{
    "name":     "abc",
    "description":  "xyz pqr"
},
{
    "name":     "xyz",
    "description":  "abc pqr"
}

If the user search for term "xyz" I want all three documents above to have the same score. 如果用户搜索术语“ xyz”,我希望以上所有三个文档具有相同的分数。 As all documents contains the term "xyz" either in name or in description or in both fields. 由于所有文档在名称或描述中或在两个字段中均包含术语“ xyz”。

You can use a Filtered Query for this. 您可以为此使用过滤查询 Filters are not scored. 过滤器未评分。 See the query below for searching the term "xyz" : 请参见下面的查询,以搜索术语"xyz"

POST <index name>/<type>/_search
{
    "filtered": {
        "query": {
            "match_all": {}
        },
        "filter": {
            "bool": {
                "should": [
                    {
                        "term": {
                            "name": "xyz"
                        }
                    },
                    {
                        "term": {
                            "description": "xyz"
                        }
                    }
                ]
            }
        }
    }
}

I think you can either : 我想你可以:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM