2017-01-16 64 views
1

我試圖在彈性搜索中執行存儲區聚合,該搜索只在查詢返回的結果上運行。如何僅聚合彈性搜索中查詢返回的結果

看來聚合運行在每個命中,但只返回它的一部分。這很好,但問題是從聚合返回的文檔與查詢返回的文檔不匹配。

這裏是映射:

LOCATION_MAPPING = { 
    id: { type: 'long' }, 
    name: { type: 'text' }, 
    street: { type: 'text' }, 
    city: { type: 'text' }, 
    state: { type: 'text' }, 
    zip: { type: 'text' }, 
    price: { type: 'text' }, 
    geolocation: { type: 'geo_point' }, 
    amenities: { type: 'nested' }, 
    reviews: { type: 'nested' }, 
}; 

下面是該查詢:

{ 
    "sort": [ 
    { 
     "_score": { 
     "order": "desc" 
     } 
    } 
    ], 
    "query": { 
    "bool": { 
     "filter": { 
     "geo_distance": { 
      "distance": "1000yd", 
      "geolocation": [ 
      -73.990768410025, 
      40.713144830193 
      ] 
     } 
     }, 
     "must": { 
     "multi_match": { 
      "query": "new york", 
      "fields": [ 
      "name^2", 
      "city", 
      "state", 
      "zip" 
      ], 
      "type": "best_fields" 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "reviews": { 
     "nested": { 
     "path": "reviews" 
     }, 
     "aggs": { 
     "location": { 
      "terms": { 
      "field": "reviews.locationId" 
      }, 
      "aggs": { 
      "avg_rating": { 
       "avg": { 
       "field": "reviews.rating" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

您所提供的信息是不夠的,瞭解你的問題,請嘗試一下dding你的模式映射,你在獲得這個查詢和預期結果後得到的結果。 – user3775217

+0

新增貼圖,希望它更清楚一點 – user3791980

回答

0

以下資源將有助於瞭解你所觀察的行爲和問題,你有:

這似乎是聚合在每個命中上運行,但只返回它的一部分。

是的,您擁有的術語聚合默認情況下只會返回前10個存儲桶,並且您可以使用大小參數(大小爲0將返回所有存儲桶)更新該存儲池。見Show all Elasticsearch aggregation buckets,相關帖子。

問題是從聚合返回的文檔與查詢返回的文檔不匹配。

在Elasticsearch響應,現在你可以看到排名前10位的得分結果(同樣有在查詢,默認值爲10的根級大小參數 - 見Elasticsearch From/Size Doc)和前10桶爲您的聚合。最高得分結果可能沒有最常用的review.locationId

我覺得你的選擇是: