ElasticSearch:查询匹配过滤器的嵌套对象

时间:2016-02-22 14:41:40

标签: elasticsearch

我有这样的索引:

{
  "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标记为嵌套映射,但它没有帮助。

1 个答案:

答案 0 :(得分:2)

在此处查看文档:{​​{3}}

在这里:https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html

这样的事情应该这样做

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

我已经修改了我的答案,以考虑过滤顶级文档和嵌套文档的细节。如果它对你有用,请告诉我!