2017-09-01 80 views
0

假設我有一個帖子指數,其數據看起來是這樣的:篩選彙總數據使用彈性搜索1.7

{ 
    "title": "Some Recipe", 
    "book_id": 123, 
    "content": "A list of ingredients", 
    "tags": ["apple", "cinnamon", "honey"] 
} 

我想有檢索查詢所有的標籤,與給定的短語開始,包含該標籤的帖子數量。

到目前爲止,我已經試過如下:

{ 
    "size": 0, 
    "query": { 
    "bool": { 
     "must": { 
     "term": { 
      "book_id": 123 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "tags": { 
     "filter": { "prefix": { "tags": "app" } }, 
     "aggs": { 
     "tags" : { "terms": { "field": "tags" } } 
     } 
    } 
    } 
} 

我相信這是返回的所有標籤的所有文件和具有與app開頭的標籤文檔的數量。

是否有可能讓所有以app開頭的標籤(在這種情況下爲apple)以及具有該標籤的文檔數量?

回答

0

terms彙總允許您使用include設置的條款filter the values。試試這個:

{ 
    "size": 0, 
    "query": { 
    "bool": { 
     "must": { 
     "term": { 
      "book_id": 123 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "tags": { 
     "filter": { "prefix": { "tags": "app" } }, 
     "aggs": { 
     "tags" : { 
      "terms": { 
      "field": "tags", 
      "include": "app.*"   <--- add this 
      } 
     } 
     } 
    } 
    } 
}