Elasticsearch -根据子数组筛选出项目

5hcedyr0  于 2022-12-11  发布在  ElasticSearch
关注(0)|答案(1)|浏览(139)

我试图只获取包含“livsforloeb”的Vrvriksomheds,其中“periode”的值为空“gyldigTil”。
当前查询为:

"query": {
      "bool": {
        "must_not": [
            {
                "exists": {
                    "field": "Vrvirksomhed.livsforloeb.periode.gyldigTil"
                }
            }
        ]
    }
}

这适用于大多数数据集,但是如果Vrvirksomhed有多个livsforloeb,其中一个在'gyldigTil'字段中有空值,Vrvirksomhed将不匹配查询。这是有意义的,因为该字段存在于之前的livsforloeb之一中。
有没有办法查询出livsforloeb的任何一个在'gyldigTil'字段中是否包含空值?
数据示例:

"hits": [
            {
                "_index": "cvr-v-20220630",
                "_type": "_doc",
                "_id": "4006437179",
                "_score": 12.423692,
                "_source": {
                    "Vrvirksomhed": {
                        "livsforloeb": [
                            {
                                "sidstOpdateret": "2016-05-18T21:06:48.000+02:00",
                                "periode": {
                                    "gyldigFra": "2016-03-01",
                                    "gyldigTil": "2016-05-18"
                                }
                            },
                            {
                                "sidstOpdateret": "2018-05-01T15:09:19.000+02:00",
                                "periode": {
                                    "gyldigFra": "2018-03-01",
                                    "gyldigTil": "2018-04-30"
                                }
                            },
                            {
                                "sidstOpdateret": "2022-07-05T09:37:48.000+02:00",
                                "periode": {
                                    "gyldigFra": "2022-07-01",
                                    "gyldigTil": null
                                }
                            }
                        ]
                        
                    }
                }
            }
        ]
js4nwp54

js4nwp541#

您的查询将不起作用,因为数组将被Elasticsearch在引擎盖下展平,因此字段将存在。
https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html
您可以移动到一个更复杂的结构,这样就不会像“嵌套”一样发生这种情况:

Map

PUT test_mustnot
{
  "mappings": {
    "properties": {
      "Vrvirksomhed": {
        "properties": {
          "livsforloeb": {
            "type": "nested",
            "properties": {
              "periode": {
                "properties": {
                  "gyldigFra": {
                    "type": "date"
                  },
                  "gyldigTil": {
                    "type": "date"
                  }
                }
              },
              "sidstOpdateret": {
                "type": "date"
              }
            }
          }
        }
      }
    }
  }
}

查询

POST test_mustnot/_search
{
    "query": {
        "nested": {
            "path": "Vrvirksomhed.livsforloeb",
            "query": {
                "bool": {
                    "must_not": {
                        "exists": {
                            "field": "Vrvirksomhed.livsforloeb.periode.gyldigTil"
                        }
                    }
                }
            }
        }
    }
}

相关问题