2016-09-16 42 views
0

我擁有數千萬個分類廣告的數百萬個日誌文檔,我想搜索所有日誌條目並將其中每個日誌條目都放在正確的存儲桶中,一個分類(具有分類的唯一ID)。我知道如何限制文檔數量,但是有沒有辦法限制桶的數量呢?彈性搜索,通過桶數而不是文檔來限制結果大小

{ 
    "size":10 #this limits by the docs length 
    "aggregations": { 
     "clfds": { 
     "terms": { 
      "field": "clsfd_id" 
     } 
     } 
    }, 
    "sort":[ 
     { 
     "clsfd_id":{ 
      "order":"asc" 
     } 
     }, 
    ], 
    "query":{ 
     "filtered":{ 
     "query":{ 
      "match_all":{ 

      } 
     }, 
     "filter":{ 
      "bool":{ 
       "should":[ 
        #filled dynamically 
       ], 
      } 
     } 
     } 
    } 
} 
+0

基數聚合是度量聚合。任何你不使用詞彙聚合的原因? – rajat

+0

@rajat,哦我在問題中使用了錯誤的查詢,糾正了它。 – SteveL

+0

在這種情況下,只需給它一個「大小」參數即可返回最近的結果。 – rajat

回答

1

我不知道這是否是你問什麼,而是你可以簡單地使用size屬性您terms聚集裏面限制返回桶的數量:

{ 
    "size":10 #this limits by the docs length 
    "aggregations": { 
     "clfds": { 
     "terms": { 
      "size": 50, 
      "field": "clsfd_id" 
     } 
     } 
    }, 
    "sort":[ 
     { 
     "clsfd_id":{ 
      "order":"asc" 
     } 
     }, 
    ], 
    "query":{ 
     "filtered":{ 
     "query":{ 
      "match_all":{ 

      } 
     }, 
     "filter":{ 
      "bool":{ 
       "should":[ 
        #filled dynamically 
       ], 
      } 
     } 
     } 
    } 
} 

如果您希望看到聚合桶下的實際文檔,可以使用the top_hits aggregation

{ 
    "aggs": { 
    "clfds": { 
     "terms": { 
     "field": "clsfd_id", 
     "size": 50 
     }, 
     "aggs": { 
     "top_clfds_hits": { 
      "top_hits": { 
      "sort": [ 
       { 
       "clsfd_id": { 
        "order": "asc" 
       } 
       } 
      ], 
      "size": 10 
      } 
     } 
     } 
    } 
    } 
}