Kam*_*her 4 database arrays isnullorempty elasticsearch
我有一组文档(类型'文章'),我想搜索具有元素/对象的文档到数组字段中
{
"_type": "article",
"_source": {
"title": "Article 1",
"locations": [
{
"address": "ES headquarter",
"city": "Berlin"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我想要两个查询(只有一个,但有一点变化):
我尝试了不同的东西,但可能我对ElasticSearch太糟糕了:
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": [
{
"type": {
"value": "article"
}
},
{
"bool": {
"must_not": {
"missing": {
"field": "location",
"existence": true,
"null_value": true
}
}
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
这不起作用.
但主要是:
如果address
是location
数组中的必填字段,则可以修改查询:
"must_not": {
"missing": {
"field": "locations.address"
}
}
Run Code Online (Sandbox Code Playgroud)
AFAIK,在ES中,您无法查询非叶元素(如您的location
字段)(请参阅问题),并且在object
类型ES中展平嵌套字段(请参阅嵌套类型,对象类型).这就是我建议查询其中一个叶元素的原因.但它要求其中一个是强制性的(遗憾的是,在您的情况下不满意).
无论如何,我使用source_filtering中的_source
参数找到了解决方案:
"must_not": {
"script": {
"script": "_source.locations.size() > 0"
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,使用"lang":"groovy"
你应该写:"script": "_source.locations.size > 0"
小智 6
如果不想启用脚本,可以将Exists 查询与must_not bool 查询结合使用,例如:
{
"query":{
"bool":{
"must_not":[
{
"exists":{
"field":"tags"
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)