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

如何在Elasticsearch中查询title/description缺失且type为A的文档

解决方案

你的需求是筛选出type字段为'A',且title或description至少一个缺失的文档,正确的Elasticsearch查询语句如下:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "type": "A"
          }
        },
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "must_not": {
                    "exists": {
                      "field": "title"
                    }
                  }
                }
              },
              {
                "bool": {
                  "must_not": {
                    "exists": {
                      "field": "description"
                    }
                  }
                }
              }
            ],
            "minimum_should_match": 1
          }
        }
      ]
    }
  }
}

逻辑说明:

  • 外层bool的must数组确保两个条件同时满足:
    1. term查询匹配type为'A'的文档;
    2. 子bool查询通过should实现或逻辑:只要title缺失,或者description缺失,两者满足其一即可。minimum_should_match:1用来保证至少触发一个should里的条件,避免无意义的匹配。

你之前的查询只处理了title缺失的情况,通过添加description缺失的should分支,就能覆盖所有符合要求的场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 05:02:48