2015-11-05 42 views
0

僅供參考,我在Mac上使用ES 1.7.2。如何彙總ElasticSearch中返回結果的數量?

我一直在試圖獲得結果集上的聚合,但我得到的是所有記錄上的聚合。

比方說,我想要返回200輛福特福克斯SE和200輛車,我想知道所有車型的Trims和每輛Trim有多少輛車。所以基本上這些修剪計數,但也得到了200回的結果。

這裏是我到目前爲止所(我使用感/奇蹟......更容易測試):

GET jdbc/_search 
{ 
    "size": 200, 
    "query": { 
    "filtered": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { "term": { "listing.model": "Focus" }}, 
      { "term": { "listing.make": "Ford" }} 
      ] 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "trims": { 
     "terms": { "field": "listing.trim" } 
    }, 
    "trim_SE": { 
     "filter": { 
     "term": { "listing.trim": "SE" } 
     }, 
    "aggs": { 
     "trims": { 
     "terms": { "field": "listing.trim"} 
     } 
     } 
    } 
    }, 
    "post_filter": { 
    "term": { "listing.trim": "SE" } 
    } 
} 

所以我得到20個結果回來,像這樣:

{ 
    "took": 18, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 10338, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "myindex", 
      "_type": "mytype", 
      "_id": "472", 
      "_score": 1, 
      "_source": { 
       "listing": { 
        "vin": "111111111111", 
        "year": 2013, 
        "make": "Ford", 
        "model": "Focus", 
        "trim": "SE", 
       } 
      } 
     }, 
     {...} 
     ] 
    } 
    "aggregations": { 
     "trims": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 30, 
     "buckets": [ 
      { 
       "key": "SE", 
       "doc_count": 10338 
      }, 
      { 
       "key": "SEL", 
       "doc_count": 1000 
       }, 
      { 
       "key": "Titanium", 
       "doc_count": 874 
      }, 
      { 
       "key": "SES", 
       "doc_count": 585 
      }, 
      { 
       "key": "S", 
       "doc_count": 554 
      }, 
      { 
       "key": "", 
       "doc_count": 447 
      }, 
      { 
       "key": "ST", 
       "doc_count": 339 
      }, 
      { 
       "key": "ZTS", 
       "doc_count": 60 
      }, 
      { 
       "key": "LX", 
       "doc_count": 56 
      }, 
      { 
       "key": "Electric", 
       "doc_count": 18 
      } 
     ] 
     }, 
     "trim_SE": { 
     "doc_count": 10338, 
     "trims": { 
      "doc_count_error_upper_bound": 0, 
      "sum_other_doc_count": 0, 
      "buckets": [ 
       { 
        "key": "SE", 
        "doc_count": 10338 
       } 
      ] 
     } 
     } 
    } 
} 

但正如你所看到的,裁剪顯示的數量比200多,這意味着它正在對所有車輛進行聚合。

我需要一些幫助,我找不到任何實際上使這項工作。

感謝您的幫助!

+0

在未來,不要猶豫,馬上創建使用https://www.found.no/play/一個reproductible情況。這是分享彈性搜索問題的最佳工具。 – thibpat

+0

超級有趣的工具!感謝分享!我一定會仔細看看。 –

回答

0

您應該將查詢中相同的過濾器添加到聚合中。 請注意,我只創建了小的例子「修剪」:

{ 
    "aggs": { 
     "trims": { 
      "filter": { 
       "bool": { 
        "must": [ 
         { 
          "term": { 
           "listing.model": "Focus" 
          } 
         }, 
         { 
          "term": { 
           "listing.make": "Ford" 
          } 
         } 
        ] 
       } 
      }, 
      "aggs": { 
       "trims": { 
        "terms": { 
         "field": "listing.trim" 
        } 
       } 
      } 
     } 
    } 
} 

我不知道已經明白你的post_filter的使用,但如果需要,你可以將其添加在「過濾器「部分的聚合。

來源:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html

+0

感謝您的快速響應!不幸的是它不起作用。也許我正在收到一些數據問題。這是我回來的錯誤: 'SearchParseException [[jdbc] [4]:query [ConstantScore(BooleanFilter(+ cache(listing.model:Focus)+ cache(listing.make:Ford)))],from [0 ],size [200]:解析失敗[預期[START_OBJECT]在[字段]下,但在[trims]]]中獲得了[VALUE_STRING];' –

+0

正確,我更新了代碼以匹配術語聚合的文檔(https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html)。 – thibpat

+0

再次感謝,但我想我沒有讓自己清楚。我期望根據結果的指定大小顯示聚合和結果。例如,如果有20萬次點擊,但我只想顯示200點,那麼聚合點應該在200點。 –