2017-07-29 145 views
0

爲簡單起見假設我有從3行指數在彈性:ElasticSearch:聚合濾波

{"id": 1, "tags": ["t1", "t2", "t3"]}, 
{"id": 2, "tags": ["t1", "t4", "t5"]} 

我需要通過某些標記而不在匹配文檔返回其他標記的結果來聚合:

{ 
    "aggs": { 
    "tags": { 
     "terms": {"field": "tags"} 
    } 
    }, 
    "query": { 
    "bool": { 
     "filter": [ 
     { 
      "terms": {"tags": ["t1", "t2"]} 
     } 
     ] 
    } 
    } 
} 

# RESULT 
{ 
    "aggregations": { 
     "tags": { 
      "buckets": [ 
       {"doc_count": 2, "key": "t1"}, 
       {"doc_count": 1, "key": "t2"}, 
       {"doc_count": 1, "key": "t3"}, # should be removed by filter 
       {"doc_count": 1, "key": "t4"}, # should be removed by filter 
       {"doc_count": 1, "key": "t5"}, # should be removed by filter 
      ], 
     } 
    }, 
    "hits": { 
     "hits": [], 
     "max_score": 0.0, 
     "total": 2 
    }, 
} 

如何(可能)postfilter這個結果?

因爲在索引3行的情況下,這隻有3個額外的項目(t3,t4,t5)。但在實際情況下,我有超過20萬行的索引,這太可怕了!我需要聚合50個標籤,但我得到超過1K標籤的結果。

回答

1

假設您的Elasticsearch版本支持它,我應該對術語聚合使用「include」屬性。您的查詢應該是前面:

POST /test/_search 
{ 
    "aggs": { 
    "tags": { 
     "terms": {"field": "tags", "include": ["t1", "t2"]} 
    } 
    }, 
    "query": { 
    "bool": { 
     "filter": [ 
     { 
      "terms": {"tags": ["t1", "t2"]} 
     } 
     ] 
    } 
    } 
} 

```