【问题标题】:ElasticSearch: query nested objects, that match filterElasticSearch:查询嵌套对象,匹配过滤器
【发布时间】:2016-02-22 14:41:40
【问题描述】:

我有这样的索引:

{
  "id":2,
  "name":"test home",
  "city_id":"31",
  "county_id":"1",
  "zip_code":"123",
  "residencePlans":[
    {
      "id" : 1,
      "unit_price_from":480240,
      "bathrooms_count":3,
      "interior_area_sqrft":23,
      "floor_range_hight":5,
      "bedrooms_count":5,
      "elevator_type_id":4,
      "price_psqft":3756,
    },
    {
      "id" : 2,
      "unit_price_from":123456,
      "bathrooms_count":1,
      "interior_area_sqrft":12,
      "floor_range_hight":4,
      "bedrooms_count":2,
      "elevator_type_id":3,
      "price_psqft":1234,
    }
  ],
}

然后我使用了一些过滤器。其中一些应用于顶部对象,一些应用于嵌套。

我需要查询 residencePlans,即匹配过滤器,为他们申请。例如,在 ResidencePlans.bathrooms_count >= 3 上的过滤器应该只返回 id = 1 而不是 2 的住宅。

{
  "id": [2],
  "residencePlans.id": [1]
}

我将居住计划标记为嵌套映射,但它没有帮助。

【问题讨论】:

  • 您的意思是 residencePlans.unit_price_from >= 300000 吗?您示例中的两个 residencePlans 的“unit_price_from”都大于 3。
  • 我的错字。我的意思是 bathrooms_count

标签: elasticsearch


【解决方案1】:

在此处查看文档:https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html

在这里:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html

应该这样做

{
  "query": {
    "bool": {
      "must": [
        { "match": { "id": 1 }}, 
        {
          "nested": {
            "path": "residencePlans", 
            "query": {
              "bool": {
                "must": [ 
                  { "gte": { "residencePlans.unit_price_from": 3 }}
                ]
               }
             }
           }
         }
       ]
     },
     inner_hits: {}
   }
 }

我已经修改了我的答案,以考虑过滤顶级文档和嵌套文档的细节。请让我知道它是否适合您!

【讨论】:

  • 是的,在这种情况下,我将获得完整的文档,但我需要与此过滤器匹配的顶级文档和居住计划的 id。
  • 澄清一下,问题是您没有在结果中获得匹配的居住计划?
  • 相反:我得到了所有的居住计划,但只想要匹配。
  • 我添加了预期结果的示例
  • 好问题,答案修改为包括 inner_hits,如果它适合你,请告诉我。
猜你喜欢
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-10
  • 2014-06-16
相关资源
最近更新 更多