![](/img/trans.png)
[英]How to search for documents in an elasticsearch index having value as empty array for a field?
[英]Exclude empty array fields - but include documents missing the field - in elasticsearch
我正在嘗試對elasticsearch運行查詢,該查詢將找到符合以下條件之一的文檔:
tags
),或者 foo
作為tags
數組的元素 問題是我當前的查詢將返回具有tags
字段(其中值為空數組)的文檔。 據推測這是因為elasticsearch將空數組與根本沒有該字段一樣。 這是我正在運行的完整查詢,返回的結果是錯誤的:
{
"from": 0,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"exists": {
"field": "_rankings.public"
}
},
{
"or": [
{
"missing": {
"existence": true,
"field": "tags",
"null_value": false
}
},
{
"terms": {
"execution": "or",
"tags": [
"foo"
]
}
}
]
}
]
}
},
"query": {
"match_all": {}
}
}
},
"size": 10000,
"sort": [
{
"_rankings.public": {
"ignore_unmapped": true,
"order": "asc"
}
}
]
}
由於您已經提到的原因,我不認為您可以這么容易地“即開即用”地實現此目的:空數組和沒有值的字段(對應於該數組)之間沒有區別。
您唯一的選擇可能是對該“標簽”字段使用“空值”,並且,如果您對進入文檔的數據有任何控制權,則將“ []”數組視為“ [[_your_null_value_of_choice_“]]” 。 並在您的查詢"null_value": false
更改為"null_value": false
到true
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.