【发布时间】:2015-12-30 21:53:20
【问题描述】:
我正在对文档中的“位置”字段进行聚合,其中同一文档中还有一个“城市”字段。我正在查询城市字段上的文档并聚合位置字段上的文档。
{
"aggs": {
"locations": {
"terms": {
"field": "location",
"min_doc_count": 0
}
}
},
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"city": "mumbai",
"_cache": true
}
}
]
}
}
}
}
}
现在计数和聚合与命中一起很好。但我的问题是我想在“doc-count”设置为 0 的情况下进行聚合,聚合桶返回我所有计数为 0 甚至下降的 lcoations在其他城市。我只想为该城市获取 0 个计数位置。想要将 0 个计数位置的上下文范围限定为城市。 我尝试通过嵌套聚合将位置放置在嵌套城市中然后执行 aggs,或者将过滤器 aggs 与术语 agg 组合但仍然得到相同的结果。 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"
}
}
}
}
要索引的示例文档
{ “名称”:“一个”, “位置”:“x”, “城市”:“孟买” }
{ “名称”:“b”, “位置”:“x”, “城市”:“孟买” }
{ “名称”:“c”, “位置”:“y” “城市”:“钦奈” }
【问题讨论】:
-
是的,请阅读关于 ES 文档的说明,ES 就是这样构建的。有没有人能够破解这个技巧...
-
请显示您当前的查询,并最终显示您正在使用的映射。
-
映射:
"service" :{ "_source" : {"enabled" : true }, "properties":{ "name" : {"type" : "string", "index" : "not_analyzed"}, "location" : {"type" : "string", "index" : "not_analyzed"}, "city" : {"type" : "string", "index" : "not_analyzed"} -
查询:
{ "aggs": { "locations": { "terms": { "field": "location", "min_doc_count": 0, } } }, "query": { "filtered": { "filter": { "bool": { "must": [ { "term": { "city": "mumbai", "_cache": true } } ] } } } } }@Val
标签: lucene elasticsearch