【问题标题】:How can we use exists query in tandem with the search query?我们如何将存在查询与搜索查询结合使用?
【发布时间】:2017-01-11 10:40:54
【问题描述】:

我在 Elasticsearch 中有一个场景,我的索引文档是这样的:-

    {"id":1,"name":"xyz", "address": "xyz123"}
    {"id":1,"name":"xyz", "address": "xyz123"}
    {"id":1,"name":"xyz", "address": "xyz123", "note": "imp"}

这里的要求强调我们必须进行术语匹配查询,然后向他们提供相关性分数,这是一件直截了当的事情,但这里的附加方面是,如果在搜索结果中找到的任何文档都有注释字段,那么应该给出更高的相关性。我们如何使用 DSL 查询来实现它?使用 exists 我们可以检查哪些文档包含注释,以及如何在 ES 查询中与 ma​​tch 查询集成。尝试了很多方法,但都没有奏效。

【问题讨论】:

    标签: elasticsearch elasticsearch-plugin


    【解决方案1】:

    使用 ES 5,您可以通过 boostexists query 为具有 note 字段的文档提供更高的分数。例如,

    {
        "query": {
            "bool": {
                "must": {
                    "match": {
                        "name": {
                            "query": "your term"
                        }
                    }
                },
                "should": { 
                    "exists": {
                        "field": "note",
                        "boost": 4
                    }
                }
            }
        }
    }
    

    使用 ES 2,您可以尝试 boosted filtered subset

    {
        "query": {
            "function_score": {
                "query": {
                    "match": { "name": "your term" }
                },
                "functions": [
                {
                    "filter": { "exists" : { "field" : "note" }},
                    "weight": 4
                }
                ],
                "score_mode": "sum"
            }
        }
    }
    

    【讨论】:

    • 据我所知 boost 不适用于存在。好吧,我会尝试这个查询,并会及时通知您。
    • 您是否在结果中看到没有字段注释的文档?
    • @nikoshr 我工作的环境有 ES 2.3.3。现在无法将 ES 更新为其生产环境。我给的版本的任何东西
    【解决方案2】:

    我相信您正在寻找增强查询功能 https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-boosting-query.html

    {
       "query": {
          "boosting": {
             "positive": {
                <put yours original query here>           
             },
             "negative": {
                "filtered": {
                   "filter": {
                      "exists": {
                         "field": "note"
                      }
                   }
                }
             },
             "negative_boost": 4
          }
       }
    }
    

    【讨论】:

    • 过滤器查询会将结果限制为具有注释字段的文档。但要求是结果可以包含没有注释字段的文档,只有当文档具有注释字段时,它应该被赋予更高的相关性并且应该出现在顶部结果中。
    • 确实你是对的,我误读了文档。查看更新的答案。
    猜你喜欢
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 2017-11-22
    • 2022-01-14
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多