【问题标题】:Elasticsearch should with must_not - non-nestedElasticsearch 应该使用 must_not - 非嵌套
【发布时间】:2020-12-03 00:24:47
【问题描述】:

刚接触 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 的错误。是否有替代语法或确实格式错误?

【问题讨论】:

  • 另外,请注意这是elasticsearch 6.3

标签: elasticsearch aws-elasticsearch


【解决方案1】:

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"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

【讨论】:

    【解决方案2】:

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

    {
      "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"
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • @Tyler Christian 你有机会看我的回答吗,期待得到你的反馈?
    • 我找不到缺少括号的地方。 VScode 确认没有 json 错误。不知道你看到了什么。
    • @TylerChristian 当您运行与问题中给出的相同查询时,您将收到错误[should] query malformed, no start_object after query name
    • 正确,但不是缺少括号。它的有效 JSON。我在代码中有一个步骤 JSON.stringify 它会首先失败。但感谢您抽出宝贵时间来查看它。
    • @TylerChristian 由于我不了解您的用例,所以我认为您希望分别有一个 must 子句和 should 子句。这就是为什么我在must 子句末尾添加了] :)
    猜你喜欢
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2023-03-05
    • 2014-02-05
    • 2020-07-14
    • 1970-01-01
    相关资源
    最近更新 更多