我在使用带有嵌套对象的 boolean 运算符时遇到了一些麻烦。 这是我的映射:
"IP": {
"properties": {
"ip": {
"type": "ip"
}
},
"type": "nested"
}
我想要获取包含两个指定 ip 甚至更多的文档。
假设我的文档有以下 ip:
DOC 1
192.168.1.0
192.168.0.1
10.0.0.9
DOC 2
192.168.0.1
我想通过使用此过滤器搜索来仅检索 DOC 1:
"bool": {
"must": {
"terms": {
"IP.ip": [
"192.168.0.1",
"10.0.0.9"
]
}
}
}
问题是 DOC 1 和 DOC2 均被检索。
最佳答案
您可以在 terms filter 上使用 "execution":"and"
像这样:
{
"filter": {
"bool": {
"must": [
{
"terms": {
"ip": [
"192.168.0.1",
"10.0.0.9"
],
"execution": "and"
}
}
]
}
}
}
这是我用来测试它的一些代码:
http://sense.qbox.io/gist/d6b5f4e4c0d2977a04b1795f4bbb0503f6365dfe
编辑:嵌套版本(我误解了这个问题)。
假设您的索引是按照我设置我的测试方式设置的(请参见下面的代码),那么此查询应该为您提供所需的内容。 must
列表中需要有两个 nested
子句,因为我们正在查找包含两个不同嵌套文档(每个文档对应一个 IP)的文档。
POST /test_index/_search
{
"filter": {
"bool": {
"must": [
{
"nested": {
"path": "ips",
"filter": {
"term": {
"ip": "192.168.0.1"
}
}
}
},
{
"nested": {
"path": "ips",
"filter": {
"term": {
"ip": "10.0.0.9"
}
}
}
}
]
}
}
}
这是我用来设置它的代码:
http://sense.qbox.io/gist/cd3a0ec61cb1348d5cc2bd1ef444f4898f6d8e57
关于elasticsearch - 嵌套对象的 boolean 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32205748/