如何编写Elasticsearch DSL多字段和多项查询?

时间:2019-02-14 23:17:18

标签: elasticsearch dsl

我应该如何编写Elasticsearch(6.6)DSL查询以仅返回此示例池中的文档Doc2和Doc3:

Doc1 { "Field1" : "Term1", "Field2" : "Term2" }
Doc2 { "Field1" : "Term6", "Field2" : "Term2" }
Doc3 { "Field1" : "Term6", "Field2" : "Term5" }
Doc4 { "Field1" : "Term3", "Field2" : "Term1" }

查询逻辑 :(字段1或字段2)不等于(术语1或术语2或术语3)

1 个答案:

答案 0 :(得分:0)

假设Field1Field2的数据类型是关键字,则可以将bool query的should和must_not子句与terms query一起使用以获得所需的结果。

查询将如下所示:

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must_not": [
              {
                "terms": {
                  "Field1": [
                    "Term1",
                    "Term2",
                    "Term3"
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "terms": {
                  "Field2": [
                    "Term1",
                    "Term2",
                    "Term3"
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}