我们如何将存在查询与搜索查询结合使用?

How can we use exists query in tandem with the search query?

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

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

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

使用 ES 5,您可以 boost your exists 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"
        }
    }
}

我相信您正在寻找提升查询功能 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
      }
   }
}