Elasticsearch—根据子数组筛选条目



我试图只获取具有'livsforloeb'的任何'周期'与null 'gyldigTil'值的Vrvriksomheds。

当前查询为:

"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
}
}
]

}
}
}
]

你的查询将不工作,因为数组将被Elasticsearch平展,所以字段将存在。

https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html

你可以移动到一个更复杂的结构,这样就不会发生这种情况,比如"nested":

<<p>映射/strong>
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"
}
}
}
}
}
}
}

最新更新