【问题标题】:elasticsearch nested query, more than one one object should meet conditionselasticsearch嵌套查询,多一个对象应满足条件
【发布时间】:2018-12-29 09:39:44
【问题描述】:

我对嵌套查询有一些疑问。 这是我的例子。映射为{"user":"nested"}。存在的数据是这样的:

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

如何创建查询来查找满足所有条件的文档:

  1. 用户的第一个对象,其“第一个”是“John”,“最后一个”是“Smith”;
  2. 用户的第二个对象,它的“first”是“Alice”,“last”是“White”

【问题讨论】:

  • 订单是否重要,或者您只是想检索具有John SmithAlice White 的所有文档?
  • 非常感谢!我不在乎 oeder,只想检索所有包含 John Smith 和 Alice White 的文档。

标签: elasticsearch nested


【解决方案1】:

试试下面的查询:

{  
   "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"
                                 }
                              }
                           ]
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}

【讨论】:

  • @MrWong 您可能想使用这个link 来了解更多关于投票或在有人回答时接受它。
【解决方案2】:

以下查询是您要查找的内容。您只需要有两个nested queries,一个用于您提到的每个条件,并使用must 子句组合在一个bool 中。

请注意,我假设字段 user.firstuser.last 是文本 type 具有 standard 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"
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}

希望这会有所帮助!

【讨论】:

  • 是的,它有效,谢谢。当我将数据更改为这样时{“user”:[{“first”:“John”,“last”:“100”},{“first ":"爱丽丝", "last":"200" } ] }
  • @MrWong 我在查询中添加了小写字母,因为我在回答中提到,如果您使用标准分析器,elasticsearch 会将数据小写并将其保存在倒排索引中。因此,即使您使用像john 这样的小写值,也会返回结果。您可以查看此链接以获取更多信息elastic.co/guide/en/elasticsearch/reference/current/…
  • 无论如何,我很高兴您的问题已得到解决。随意接受答案和/或投票。 ;)
【解决方案3】:

答案是:

{
    "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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    相关资源
    最近更新 更多