简体   繁体   English

使用必须和匹配过滤弹性搜索结果,给出意外结果

[英]Filtering Elastic search result with must and match giving unexpected result

{
  "body": {
    "query": {
      "bool": {
        "must": {
          "match": {
            "_id": "24"
          }
        },
        "should": [
          {
            "term": {
              "draft_id": "draft_11"
            }
          },
          {
            "term": {
              "draft_id": "non_draft"
            }
          }
        ]
      }
    }
  }
}

result contains below information结果包含以下信息

{
  "_id": "24",
  "name": "xyz",
  "draft_id": "draft_312"
}

Requirement要求

I wan to filter record based on:我想根据以下条件过滤记录:

  • id must match id 必须匹配
  • And draft_id should be either non_draft or draft_11而且 draft_id 应该是non_draftdraft_11

OR in simple words或者用简单的话

id = 24 AND (draft_id = "non_draft" OR draft_id = "draft_11")

And if you see the result, it only matches id, but not draft_id field.如果你看到结果,它只匹配 id,而不匹配 draft_id 字段。

You need to move your should clause inside must_clause.您需要将您的 should 子句移到 must_clause 中。 Only clauses in must get "AND" among themselves只有子句在它们之间必须得到“AND”

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "_id": "24"
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
              "draft_id": "draft_11"
            }
              },
              {
             "term": {
              "draft_id": "draft_11"
            }   
              }
            ]
          }
        },
        {
          "query_string": {
            "default_field": "FIELD",
            "query": "this AND that OR thus"
          }
        }
      ]
    }
  }
}

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

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