elasticsearch - Elasticsearch中的空检查条件

标签 elasticsearch

正在对下面的2个字段进行匹配搜索,但是如果ownerIdNo为null,则我不希望为此进行搜索。

{
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "idNo": "3504"
              }
            },
            {
              "match": {
                "ownerIdNo": "null"
              }
            }
          ]
        }
      }
    }

伪代码如下:
matchSearchFor(idNo);
if(ownerIdNo != null){
  matchSearchFor(ownerIdNo);
}

最佳答案

如果不能以 flex 方式添加,则不能添加-DSL这不是一种程序语言。

您想要选择文档(ownerIdNo为null或idNo“:” 3504“)。在elasticsearch中,您必须使用should子句(OR)。

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "idNo": "3504"
          }
        },
        {
          "bool": {
            "must_not": [ --> null fields are not stored in documents
              {
                "match": {
                  "ownerIdNo": "null"
                }
              }
            ]
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

关于elasticsearch - Elasticsearch中的空检查条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61662607/

相关文章:

elasticsearch - 更新 Elasticsearch 中不存在的文档

powershell - 系统在Powershell中找不到路径/命令未找到错误

c# - 在 Nest 5.5.0 中为属性设置 not_analyzed

elasticsearch - 如何在elasticsearch中做两个嵌套聚合?

python - 通过Python脚本删除Elasticsearch索引时出现TransportError

algorithm - elasticsearch Geo_Polygon过滤算法

elasticsearch - 数组Elasticsearch的数组内的聚合值

java - 如何在 sql-server 数据库表上构建自由流搜索?

java - Elasticsearch字符串查询匹配一个字段的多个term

elasticsearch - 按相关性对 ElasticSearch 文档中的关键字字段数组进行排序