Elasticsearch Bool过滤器用于数组同一元素上的多个条件 [英] Elasticsearch bool filter for multiple conditions on same element of array
问题描述
仅在数组的同一项上满足多个条件的情况下,我试图创建一个与文档匹配的查询/过滤器。
I'm trying to create a query/filter that matches a document only if a number of conditions are met on the same item of an array.
让我们说这是文档:
{
arr: [
{ "f1" : "a" , f2 : true },
{ "f1" : "b" , f2 : false}
]
}
我希望能够检索在同一元素上具有N个条件匹配的文档。例如: arr.f1 == a AND arr.f2 == true
应该与文档匹配,但 arr.f1 == b AND arr.f2 == true
不应该。
I want to be able to retrieve documents that have N conditions matching on the same element. For example: arr.f1 == "a" AND arr.f2 == true
should match the document but arr.f1 == "b" AND arr.f2 == true
should not.
我正在尝试嵌套布尔过滤器(除此以外,我还有其他过滤器),但是它不起作用,类似
I'm trying nested bool filters (I have other filters apart from this one) but it doesn't work, something in the lines of
"bool" : {
"must": [
{ some other filter },
{"bool": {"must" : [
{"term" : {"arr.f1" : "a"}},
{"term" : {"arr.f2" : true}},
] }}
]
}
任何想法该怎么做?
谢谢
Any idea how to do that? thanks
编辑:
我更改了映射,现在嵌套查询根据Val的响应工作。我现在无法在嵌套字段上执行存在过滤器:
edit: I changed the mapping and now a nested query works as per Val's response. I'm now not able to do an "exists" filter on the nested field:
一个简单的 { filter:{ exists :{ field: arr}}}
搜索不返回匹配。我该怎么做?
A simple { "filter" : {"exists" : { "field" : "arr" } } }
search returns no hits. How do I do that?
编辑:看来我需要做一个嵌套的存在过滤器来检查嵌套对象中是否存在一个字段。
类似于:
edit: It looks like I need to do a nested exists filter to check that a field inside the nested object exists. something like:
"filter" : {
"nested" : {"path" : "arr", "filter" : {"exists" : { "field" : "f1" } }}
}
编辑:
argh-现在突出显示不再起作用:
edit: argh - now highlight doesn't work anymore:
"highlight" : {
"fields" : [
{"arr.f1" : {}},
]
}
通过添加 include_in_parent:true
并查询嵌套字段和根对象来解决此问题。真可怕
Worked around that by adding include_in_parent : true
and querying both the nested field and the root object. It's just awful. If anyone has a better idea, they're more than welcome!
{
"query" : {
"bool" : {
"must": [
{"term" : { "arr.f1" : "a" }},
{ "nested" : { "path" : "arr",
"query" : { "bool" : { "must" : [
{"term" : { "arr.f1" : "a" }},
{"term" : { "arr.f2" : true }}
] } }
}}
]
}
},
"highlight" : {
"fields" : [
{"arr.f1" : {}},
]
}
}
如果您想知道的话,那是遗留的东西。我现在无法重新编制索引(这是显而易见的解决方案),我需要快速&肮脏的解决方法
In case you're wondering: it's legacy stuff. I can't reindex right now (that would be the obvious solution) and I need a quick & dirty workaround
推荐答案
您需要设置 arr
字段的类型格式为 嵌套
像这样:
You need to set the type of your arr
field as nested
like this:
{
"your_type": {
"properties": {
"arr": {
"type": "nested",
"properties": {
"f1": {"type":"string"},
"f2": {"type":"boolean"}
}
}
}
}
}
然后您需要使用 嵌套
查询:
Then you need to use a nested
query:
{
"nested" : {
"path" : "arr",
"query" : {
"bool" : {
"must" : [
{
"term" : {"arr.f1" : "a"}
},
{
"term" : {"arr.f2" : true}
}
]
}
}
}
}
您的存在
过滤器需要指定完整的字段路径
Your exists
filter needs to specify the full field path
"filter" : {
"nested" : {"path" : "arr", "filter" : {"exists" : { "field" : "arr.f1" } }}
}
这篇关于Elasticsearch Bool过滤器用于数组同一元素上的多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!