Elasticsearch查询:基于嵌套对象条件筛选文档,忽略缺少或不匹配的字段

m1m5dgzv  于 2023-10-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(133)

我正在尝试根据嵌套的对象条件过滤Elasticsearch文档。具体来说,我想检索这样的文档,其中字段entity.interviewRounds.status要么不存在于entity.interviewRounds数组中的任何嵌套对象中,要么,如果它存在,它的值必须是“INTERVIEW_SCHEDULING_PENDING”。
示例文档JSON:

{
  "entity": {
   "interviewRounds": [
    {
     "sequenceNumberFromOne": 1,
     "status": "INTERVIEW_SCHEDULE_PENDING"
    },
    {
     "sequenceNumberFromOne": 2
    }
   ],
  }
}

有没有人能建议一个修改过的Elasticsearch查询来实现这种过滤行为?任何帮助将不胜感激!
我已经尝试过下面的查询,但是如果数组中的任何一个对象不包含entity.interviewRounds.status字段,它将返回文档。我需要帮助来创建一个查询,该查询只返回所有嵌套对象都不包含字段或其值为“INTERVIEW_SCHEDULE_PENDING”的文档。

{
    "nested": {
        "query": {
            "bool": {
                "should": [
                    {
                        "bool": {
                            "must_not": [
                                {
                                    "exists": {
                                        "field": "entity.interviewRounds.status",
                                        "boost": 1
                                    }
                                }
                            ],
                            "adjust_pure_negative": true,
                            "boost": 1
                        }
                    },
                    {
                        "bool": {
                            "must": [
                                {
                                    "term": {
                                        "entity.interviewRounds.sequenceNumberFromOne": {
                                            "value": 1,
                                            "boost": 1
                                        }
                                    }
                                },
                                {
                                    "term": {
                                        "entity.interviewRounds.status.keyword": {
                                            "value": "INTERVIEW_SCHEDULE_PENDING",
                                            "boost": 1
                                        }
                                    }
                                }
                            ],
                            "adjust_pure_negative": true,
                            "boost": 1
                        }
                    }
                ],
                "adjust_pure_negative": true,
                "boost": 1
            }
        },
        "path": "entity.interviewRounds"
    }
cetgtptt

cetgtptt1#

你就快成功了。查询将返回嵌套字段为空或挂起的所有文档。您只需要将顶层must_not替换为must_not,以使其返回嵌套对象包含的所有字段,这些字段返回除了空或挂起之外的任何内容。
换句话说,

{
    "nested": {
        "query": {
            "bool": {
                "should": [
                    {

尝试

{
    "nested": {
        "query": {
            "bool": {
                "must_not": [
                    {

更新:这里是一个完整的例子

PUT test
{
  "mappings": {
    "properties": {
      "entity": {
        "properties": {
          "interviewRounds": {
            "type": "nested",
            "properties": {
              "sequenceNumberFromOne": {
                "type": "integer"
              },
              "status": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword"
                  }
                }
                
              }
            }
          }
        }
      }
    }
  }
}

PUT test/_bulk?refresh
{"index":{}}
{"entity":{"interviewRounds":[{"sequenceNumberFromOne":1,"status":"INTERVIEW_SCHEDULE_PENDING"},{"sequenceNumberFromOne":2}]}}
{"index":{}}
{"entity":{"interviewRounds":[{"sequenceNumberFromOne":1,"status":"INTERVIEW_SCHEDULE_PENDING"}]}}
{"index":{}}
{"entity":{"interviewRounds":[{"sequenceNumberFromOne":1,"status":"INTERVIEW_SCHEDULE_PENDING"},{"sequenceNumberFromOne":2},{"sequenceNumberFromOne":3,"status":"INTERVIEW_SCHEDULE_CONFIRMED"}]}}
{"index":{}}
{"entity":{"interviewRounds":[{"sequenceNumberFromOne":3,"status":"INTERVIEW_SCHEDULE_CONFIRMED"}]}}

POST test/_search
{
  "query": {
    "nested": {
      "query": {
        "bool": {
          "must_not": [
            {
              "term": {
                "entity.interviewRounds.status.keyword": {
                  "value": "INTERVIEW_SCHEDULE_PENDING",
                  "boost": 1
                }
              }
            },
            {
              "bool": {
                "must_not": [
                  {
                    "exists": {
                      "field": "entity.interviewRounds.status",
                      "boost": 1
                    }
                  }
                ],
                "adjust_pure_negative": true,
                "boost": 1
              }
            }
          ],
          "adjust_pure_negative": true,
          "boost": 1
        }
      },
      "path": "entity.interviewRounds"
    }
  }
}

相关问题