ES存在查询

ES exists query

我有一份文件 nested field:

我只想获取 testData.testDataIdnull

的文档

这是我的查询:

{
  "bool": {
    "must_not": [
      { "exists": { "field": "testData.testDataId", "boost": 1.0 } }
    ],
    "adjust_pure_negative": true,
    "boost": 1.0
  }
}

但我也有那些 'testData' 为 null 的文档。是否可以禁用?

例如: 我有这 3 个文件

[
  {
    "name": "test_user_1",
    "additionalInfo": { "phoneNumber": "00000000", "address": "test_address" },
    "testData": { "testDataId": "some_id", "description": "test_description" }
  },
  {
    "name": "test_user_2",
    "additionalInfo": { "phoneNumber": "00000000", "address": "test_address" },
    "testData": { "description": "test_description" }
  },
  {
    "name": "test_user_3",
    "additionalInfo": { "phoneNumber": "00000000", "address": "test_address" }
  }
]

我只想检索 test_user_2

Tldr;

由于您正在处理 nested field 您需要使用嵌套查询。

修复

GET /72491619/_search
{
  "query": {
    "nested": {
      "path": "testData",
      "query": {
        "bool": {
          "must_not": [
            {
              "exists": {
                "field": "testData.testDataId"
                }
            }
          ]
        }
      }
    }
  }
}

重现

为了设置索引和数据,我使用了下面的 api 调用:

PUT /72491619/
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "additionalInfo":{
        "type": "nested",
        "properties": {
          "phoneNumber": {
            "type": "keyword"
          },
          "address": {
            "type": "keyword"
          }
        }
      },
      "testData":{
        "type": "nested",
        "properties": {
          "testDataId": {
            "type": "keyword"
          },
          "description": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

POST /_bulk
{"index":{"_index":"72491619"}}
{"name":"test_user_1","additionalInfo":{"phoneNumber":"00000000","address":"test_address"},"testData":{"testDataId":"some_id","description":"test_description"}}
{"index":{"_index":"72491619"}}
{"name":"test_user_2","additionalInfo":{"phoneNumber":"00000000","address":"test_address"},"testData":{"description":"test_description"}}
{"index":{"_index":"72491619"}}
{"name":"test_user_3","additionalInfo":{"phoneNumber":"00000000","address":"test_address"}}