Elasticsearch 应使用 must_not - 非嵌套

Elasticsearch should with must_not - non-nested

Elasticsearch 的新手,正在研究我对更改犹豫不决的遗留模型。我有一个日期字段,如果它的值为 null,我们将不会输入(我认为是因为 ES 处理 0000-00-00 的方式)。我希望能够查询具有特定日期或该字段不存在的数据。这是我拥有的:

{
    "size": 10000,
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "AccountID": "xxxx"
                    }
                },
                {
                    "term": {
                        "LocationID": "xxxx"
                    }
                },
                {
                    "should": [
                        {
                            "range": {
                                "CloseDate": {
                                    "gte": "2020-11-01",
                                    "lte": "2020-12-02"
                                }
                            }
                        },
                        {
                            "bool": {
                                "must_not": {
                                    "exists": {
                                        "field": "CloseDate"
                                    }
                                }
                            }
                        }
                    ]
                }
            ]
        }
    }
}

我在尝试此操作时遇到 [should] query malformed, no start_object after query name 错误。是否有替代语法或格式是否正确?

该错误清楚地表明您的查询格式不正确。您缺少 ] 括号。试试这个查询:

{
  "size": 10000,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "AccountID": "xxxx"
          }
        },
        {
          "term": {
            "LocationID": "xxxx"
          }
        }
      ],                                   <-- note this
      "should": [
        {
          "range": {
            "CloseDate": {
              "gte": "2020-11-01",
              "lte": "2020-12-02"
            }
          }
        },
        {
          "bool": {
            "must_not": {
              "exists": {
                "field": "CloseDate"
              }
            }
          }
        }
      ]
    }
  }
}

一个 must 子句可以在 [] 中有多个查询。请注意 should 也是 bool 查询的子句,而不是查询本身。因此它应该在 bool.

{
  "size": 10000,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "AccountID": "xxxx"
          }
        },
        {
          "term": {
            "LocationID": "xxxx"
          }
        },
        {
          "bool": {            <------------- Note this
            "should": [
              {
                "range": {
                  "CloseDate": {
                    "gte": "2020-11-01",
                    "lte": "2020-12-02"
                  }
                }
              },
              {
                "bool": {
                  "must_not": {
                    "exists": {
                      "field": "CloseDate"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}