[英]Scope 0 count terms in aggregation in ElasticSearch
我正在對文檔中的“位置”字段進行匯總,同一文檔中也有一個“城市”字段。我正在對城市字段中的文檔進行查詢,並對位置字段中的文檔進行匯總。
{
"aggs": {
"locations": {
"terms": {
"field": "location",
"min_doc_count": 0
}
}
},
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"city": "mumbai",
"_cache": true
}
}
]
}
}
}
}
}
現在計數和聚合都很好,並且命中率也很好。但是我的問題是我想將'doc-count'設置為0進行聚合,並且聚合存儲桶將所有計數為0的標簽歸還給我,甚至落在其他城市我只想為那個城市獲得0個計數位置。想要將0個計數位置的上下文范圍限定到城市。 我嘗試通過將嵌套聚合放置在嵌套城市中的位置來實現此目的,然后進行aggs或將過濾器aggs與術語agg組合在一起,但仍然獲得相同的結果。是否有任何方法可以實現此目的,或者Elasticsearch本質上是可以這樣工作的 ES版本-1.6
我的映射如下所示:
{
"service": {
"_source": {
"enabled": true
},
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
},
"location": {
"type": "string",
"index": "not_analyzed"
},
"city": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
樣本文檔索引
{“ name”:“ a”,“ location”:“ x”,“ city”:“孟買”}
{“ name”:“ b”,“ location”:“ x”,“ city”:“孟買”}
{“ name”:“ c”,“ location”:“ y”“ city”:“ chennai”}
您應該嘗試通過遞增文檔計數對terms
聚合(嵌入到filter
聚合中)進行排序,然后首先獲得所有文檔計數為0的術語。 請注意,默認情況下,您只會獲得前10個字詞,如果文件數為0的字詞較少,則會看到所有字詞,否則,可能需要將size
參數增加到大於10的值。
{
"aggs": {
"city_filter": {
"filter": {
"term": {
"city": "mumbai"
}
},
"aggs": {
"locations": {
"terms": {
"field": "location",
"min_doc_count": 0,
"size": 20, <----- add this if you have more than ten 0-doc-count terms
"order": { <----- add this to see 0-doc-count first
"_count": "asc"
}
}
}
}
}
},
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"city": "mumbai",
"_cache": true
}
}
]
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.