You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询Elasticsearch 8.9版本中not子句的对应替代语法

Elasticsearch 8.9 替代 not 子句的语法

在 Elasticsearch 8.9 版本中,not 查询已被移除,需通过 bool 查询的 must_not 子句 实现原有的否定过滤逻辑。

原 Elasticsearch 2.2 可运行语法

{
    "bool" : {
        "must" : {
            "term" : { "name.first" : "shay" }
        },
        "filter" : {
            "not" : {
                "range" : {
                    "postDate" : {
                        "from" : "2010-03-01",
                        "to" : "2010-04-01"
                    }
                }
            }
        }
    }
}

适配 Elasticsearch 8.9 的替代语法

{
    "bool" : {
        "must" : {
            "term" : { "name.first" : "shay" }
        },
        "filter" : {
            "bool": {
                "must_not": {
                    "range" : {
                        "postDate" : {
                            "gte" : "2010-03-01",
                            "lte" : "2010-04-01"
                        }
                    }
                }
            }
        }
    }
}

关键修改说明

  • 把原 filter 内的 not 结构替换为 bool.must_not,这是高版本 Elasticsearch 标准的否定查询写法。
  • 补充:range 查询的 from/to 在新版本中仍兼容,但更推荐使用 gte/lte(大于等于/小于等于)来提升语法可读性。

内容的提问来源于stack exchange,提问作者Ajay Takur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 08:19:57