elasticsearch - 如何结合 “AND”和 “OR”查询来获得两者的结果?

标签 elasticsearch

我正在尝试使用一个查询来获取两个查询的结果。
我必须为每个多字串查询请求运行两个查询,然后以编程方式将结果组合到我觉得不合适的代码中。
其中一个查询使用AND运算符,其中一个查询使用OR运算符。
这是第一个查询:

GET my_index/_search
{
  "query": {
    "bool": {
      "must_not": [
        {"exists": {"field": "deleted"}}
      ],
      "must": [
        {
          "multi_match": {
            "query": "refuse eating",
            "fields": [
              "title^3",
              "desc^2",
              "body"
            ],
            "type": "best_fields",
            "operator": "AND"
          }
        }
      ],
      "filter": [
        {"term": {"kind": "article"}},
        {"term": {"status": "published"}}
      ]
    }
  }
}
这是第二个查询:
GET my_index/_search
{
  "query": {
    "bool": {
      "must_not": [
        {"exists": {"field": "deleted"}}
      ],
      "must": [
        {
          "multi_match": {
            "query": "refuse eating",
            "fields": [
              "title^3",
              "desc^2",
              "body"
            ],
            "type": "best_fields",
            "operator": "OR"
          }
        }
      ],
      "filter": [
        {"term": {"kind": "article"}},
        {"term": {"status": "published"}}
      ]
    }
  }
}
这个想法是在顶部处获得第一个查询的结果(使用AND运算符的查询),然后在第一个结果下方获得第二个查询的结果(使用OR运算符的查询)。
有什么方法可以通过而不是两个单个查询来实现这一目标?

最佳答案

对于AND匹配,您可以利用bool/should因子较高的boost:

GET my_index/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "deleted"
          }
        }
      ],
      "minimum_should_match": 1,
      "should": [
        {
          "multi_match": {
            "query": "refuse eating",
            "fields": [
              "title^3",
              "desc^2",
              "body"
            ],
            "type": "best_fields",
            "operator": "AND",
            "boost": 2,                   <--- boost AND by 2 (or more)
            "_name": "and-match"
          }
        },
        {
          "multi_match": {
            "query": "refuse eating",
            "fields": [
              "title^3",
              "desc^2",
              "body"
            ],
            "type": "best_fields",
            "operator": "OR",
            "_name": "or-match"
          }
        }
      ],
      "filter": [
        {
          "term": {
            "kind": "article"
          }
        },
        {
          "term": {
            "status": "published"
          }
        }
      ]
    }
  }
}

关于elasticsearch - 如何结合 “AND”和 “OR”查询来获得两者的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54101580/

相关文章:

elasticsearch - 在多重搜索查询上使用嵌套短语建议程序

python - 使用 python elasticsearch API 编写脚本解析异常

elasticsearch - 如何在嵌套字段上使用 copy_to

elasticsearch - 如何防止碎片从热节点迁移到热节点或冷节点?

json - 嵌套过滤器数值范围

Elasticsearch - 获取聚合键排序为数字

ssl - Elasticsearch Shield SSL 证书

elasticsearch - Elasticsearch精确匹配词组

elasticsearch - Elasticsearch geohash_grid返回1个文档计数,但查询返回很多

c# - Elasticsearch 7和Nest 7-检索结果时,我收到键值对列表或空对象列表