2017-03-24 96 views
1

我在ES映射中有一個父子關係,我想通過子集上的聚合值(平均值)來篩選父項。也就是說,我只想檢索那些價值在給定範圍內的父母。在Elasticsearch中通過子集聚合篩選父項

我試圖用aggs和後置過濾器來做,但無法讓它工作。

{ 
    "apartments" : { 
    "mappings" : { 
     "apartment_availability" : { 
     "_parent" : { 
      "type" : "apartment" 
     }, 
     "_routing" : { 
      "required" : true 
     }, 
     "properties" : { 
      "availability_date" : { 
      "type" : "date" 
      }, 
      "apartment_id" : { 
      "type" : "long" 
      }, 
      "id" : { 
      "type" : "long" 
      }, 
      "price_cents" : { 
      "type" : "long" 
      }, 
      "status" : { 
      "type" : "text", 
      "fields" : { 
       "keyword" : { 
       "type" : "keyword", 
       "ignore_above" : 256 
       } 
      } 
      } 
     } 
     }, 
     "apartment" : { 
     "properties" : { 
      "id" : { 
      "type" : "long" 
      }, 
     } 
     } 
    } 
    } 
} 

如果奧魯用戶選擇期間3月24日至3月31日和€150 - 300€價格區間,那麼我們想向他們展示,在這期間是免費的,所有公寓,其平均價格爲期間在€150-€300範圍內。

下面是我們到目前爲止有:

{ 
    "query": { 
    "bool": { 
     "filter": { 
     "bool": { 
      "must": [{ 
      "has_child": { 
       "type": "apartment_availability", 
       "min_children": 8, 
       "max_children": 8, 
       "query": { 
       "bool": { 
        "must": [{ 
        "term": { 
         "status": "available" 
        } 
        }, { 
        "range": { 
         "availability_date": { 
         "gte": "2017-03-24", 
         "lte": "2017-03-31" 
         } 
        } 
        }] 
       } 
       } 
      } 
      }] 
     } 
     } 
    } 
    } 
} 

回答

0

我的建議,使用bucket_selector聚集的公寓中進行選擇:

GET /apartments/apartment/_search 
{ 
    "query": { 
    "bool": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "has_child": { 
       "type": "apartment_availability", 
       "query": { 
        "bool": { 
        "must": [ 
         { 
         "term": { 
          "status": "available" 
         } 
         }, 
         { 
         "range": { 
          "availability_date": { 
          "gte": "2017-04-01", 
          "lte": "2017-04-03" 
          } 
         } 
         } 
        ] 
        } 
       } 
       } 
      } 
      ] 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "apartments_ids": { 
     "terms": { 
     "field": "id", 
     "size": 10 
     }, 
     "aggs": { 
     "avails": { 
      "children": { 
      "type": "apartment_availability" 
      }, 
      "aggs": { 
      "filter_avails": { 
       "filter": { 
       "bool": { 
        "must": [ 
        { 
         "term": { 
         "status": "available" 
         } 
        }, 
        { 
         "range": { 
         "availability_date": { 
          "gte": "2017-04-01", 
          "lte": "2017-04-03" 
         } 
         } 
        } 
        ] 
       } 
       }, 
       "aggs": { 
       "average": { 
        "avg": { 
        "field": "price_cents" 
        } 
       } 
       } 
      } 
      } 
     }, 
     "avg_bucket_filter": { 
      "bucket_selector": { 
      "buckets_path": { 
       "avg": "avails>filter_avails.average" 
      }, 
      "script": "params.avg > 150 && params.avg < 300" 
      } 
     } 
     } 
    } 
    } 
}