简体   繁体   English

Elasticsearch 将 2 个过滤器上下文查询与 OR 结合

[英]Elasticsearch combine 2 filter context queries with OR

I have the following query, it correctly finds documents that have a NAME field but do NOT have a NEW_NAME field:我有以下查询,它正确找到具有 NAME 字段但没有 NEW_NAME 字段的文档:

"query": {
  "bool": {
    "filter": {
      "exists": {
        "field": "NAME"
      }
    },
    "must_not": {
      "exists": {
        "field": "NEW_NAME"
      }
    }
  }
}

I'd like to extend this query (by OR'ing it) with another query so that it also finds documents that have a DOB field but do NOT have a NEW_DOB field, eg我想用另一个查询扩展这个查询(通过 OR'ing 它),以便它也找到具有 DOB 字段但没有 NEW_DOB 字段的文档,例如

"query": {
  "bool": {
    "filter": {
      "exists": {
        "field": "DOB"
      }
    },
    "must_not": {
      "exists": {
        "field": "NEW_DOB"
      }
    }
  }
}

so my query is something like:所以我的查询是这样的:

(NAME exists AND NEW_NAME !exist) OR (DOB exists AND NEW_DOB !exist)

I think I need to combine the above two queries using should, just not sure exactly how, this does not work:我想我需要使用应该结合上述两个查询,只是不确定如何,这不起作用:

"query": {
  "bool": {
    "should": [
        {
          "filter": {
            "exists": {
              "field": "NAME"
            }
          },
          "must_not": {
            "exists": {
              "field": "NEW_NAME"
            }
          }
        },
        {
          "filter": {
            "exists": {
              "field": "DOB"
            }
          },
          "must_not": {
            "exists": {
              "field": "NEW_DOB"
            }
          }
        }
    ]
  }
}
{
  "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "filter": {
                  "exists": {
                    "field": "NAME"
                  }
                },
                "must_not": {
                  "exists": {
                    "field": "NEW_NAME"
                  }
                }
              }
            },
            {
              "bool": {
                "filter": {
                  "exists": {
                    "field": "DOB"
                  }
                },
                "must_not": {
                  "exists": {
                    "field": "NEW_DOB"
                  }
                }
              }
            }
          ]
        }
      }
}

I just combined with must and should.我只是结合了必须和应该。

must = AND should = OR必须 = AND 应该 = OR

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

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