elasticsearch嵌套查询,多个对象应满足条件

brc7rcf0  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(3)|浏览(330)

我有一些关于嵌套查询的问题。这是我的例子。Map是 {"user":"nested"} .现有数据如下:

{
  "user": [
    {
      "first":"John",
      "last":"Smith"
    },
    {
      "first":"Alice",
      "last":"White"
    }
  ]
}

如何创建查询以查找满足以下所有条件的文档:
用户的第一个对象,其“first”是“john”,而“last”是“smith”;
用户的第二个对象,它的“第一个”是“爱丽丝”,“最后一个”是“白色”

mqkwyuun

mqkwyuun1#

尝试以下查询:

{  
   "query":{  
      "bool":{  
         "filter":[  
            {  
               "bool":{  
                  "must":[  
                     {  
                        "bool":{  
                           "must":[  
                              {  
                                 "nested":{  
                                    "query":{  
                                       "bool":{  
                                          "must":[  
                                             {  
                                                "match_phrase":{  
                                                   "user.first":{  
                                                      "query":"John"
                                                   }
                                                }
                                             },
                                             {  
                                                "match_phrase":{  
                                                   "user.last":{  
                                                      "query":"Smith"
                                                   }
                                                }
                                             }
                                          ]
                                       }
                                    },
                                    "path":"user"
                                 }
                              },
                              {  
                                 "nested":{  
                                    "query":{  
                                       "bool":{  
                                          "must":[  
                                             {  
                                                "match_phrase":{  
                                                   "user.first":{  
                                                      "query":"Alice"
                                                   }
                                                }
                                             },
                                             {  
                                                "match_phrase":{  
                                                   "user.last":{  
                                                      "query":"White"
                                                   }
                                                }
                                             }
                                          ]
                                       }
                                    },
                                    "path":"user"
                                 }
                              }
                           ]
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}
relj7zay

relj7zay2#

答案是:

{
    "query": {
        "bool": {
            "must": [
                {
                    "has_parent": {
                        "parent_type": "doc",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "terms": {
                                            "id": [
                                                713
                                            ]
                                        }
                                    },
                                    {
                                        "range": {
                                            "created": {
                                                "lte": "now/d"
                                            }
                                        }
                                    },
                                    {
                                        "range": {
                                            "expires": {
                                                "gte": "now/d"
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                    "nested": {
                        "path": "prices",
                        "query": {
                            "bool": {
                                "filter": [
                                    {
                                        "term": {
                                            "prices.id_prcknd": 167
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                    "term": {
                        "doc_type": "item"
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "have_prices": true
                                }
                            },
                            {
                                "term": {
                                    "is_folder": true
                                }
                            }
                        ]
                    }
                }
            ],
            "must_not": {
                "exists": {
                    "field": "folder"
                }
            }
        }
    },
    "sort": [
        {
            "is_folder": {
                "order": "desc"
            }
        },
        {
            "title_low.order": {
                "order": "asc"
            }
        }
    ],
    "size": 1000
}
eivnm1vs

eivnm1vs3#

下面的查询是你要找的。你只需要有两个 nested queries ,你提到的每种情况一个,组合在一个 bool 使用 must 条款。
注意,我假设 user.first 以及 user.last 都是文字 typestandard analyzer ```
POST <your_index_name>
{
"query":{
"bool":{
"must":[
{
"nested":{
"path":"user",
"query":{
"bool":{
"must":[
{
"match":{
"user.first":"john"
}
},
{
"match":{
"user.last":"smith"
}
}
]
}
}
}
},
{
"nested":{
"path":"user",
"query":{
"bool":{
"must":[
{
"match":{
"user.first":"alice"
}
},
{
"match":{
"user.last":"white"
}
}
]
}
}
}
}
]
}
}
}

希望这有帮助!

相关问题