Elastic Search 1.7.3嵌套过滤器:匹配对象数组中的术语 [英] Elastic Search 1.7.3 Nested filter: matching terms in an array of objects
问题描述
我试图在我的elasticsearch中查询以下文档:
I am trying to query for the following document in my elasticsearch:
"amenity": [
"Free Wifi",
"Free Breakfast",
"Veg Only",
"Swimming Pool",
"Newspaper",
"Bar",
"Credit Card",
"Pickup & Drop",
"Gym",
"Elevator",
"Valet Parking"
],
"dodont": [
{
"do_or_dont": "Do",
"what": "Vegetarians"
},
{
"do_or_dont": "Do",
"what": "Family"
},
{
"do_or_dont": "Dont",
"what": "Loud Music"
},
{
"do_or_dont": "Dont",
"what": "Booze"
}
]
这是我编写的查询:
"filter": {
"and": {
"filters": [
{
"nested" : {
"path" : "dodont",
"filter" : {
"bool" : {
"must": [{"and" : [
{
"term" : {"dodont.do_or_dont" : "Do"}
},
{
"term" : {"dodont.what" : "Vegetarians"}
}
]},
{"and" : [
{
"term" : {"dodont.do_or_dont" : "Do"}
},
{
"term" : {"dodont.what" : "Family"}
}
]}]
}
}
}
}
]
}
}
现在此查询返回空结果,但是当我在上面的代码的bool中将"must"更改为"should"时,它将返回上面的文档作为结果(只有1个文档与此过滤器匹配,如上所示)),但理想情况下,必须"条件应返回上述文档,我想为Do和Dons传递多个对象,而我只想要与所有对象都匹配的结果,但我不能这样做.我该怎么办?
Now this query returns empty result, but when I change the "must" to "should" in the bool in above code, it returns the above document as the result (there is only 1 document matching this filter the one shown above), but ideally, the "must" condition should return the above document, I want to pass multiple objects for Do's and donts and I only want the results which match all of them, but I am not able to do so. How should I go about it?
推荐答案
您需要在嵌套文档中拆分两个条件,因为 dodont
嵌套数组的每个元素从概念上讲都是一个单独的文档:
You need to split out the two conditions on your nested document, since each element of the dodont
nested array is conceptually a separate document:
{
"filter": {
"and": {
"filters": [
{
"nested": {
"path": "dodont",
"filter": {
"and": [
{
"term": {
"dodont.do_or_dont": "Do"
}
},
{
"term": {
"dodont.what": "Vegetarians"
}
}
]
}
}
},
{
"nested": {
"path": "dodont",
"filter": {
"and": [
{
"term": {
"dodont.do_or_dont": "Do"
}
},
{
"term": {
"dodont.what": "Family"
}
}
]
}
}
}
]
}
}
}
这篇关于Elastic Search 1.7.3嵌套过滤器:匹配对象数组中的术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!