【问题标题】:Scope 0 count terms in aggregation in ElasticSearchElasticSearch 中聚合中的范围 0 计数术语
【发布时间】: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


【解决方案1】:

您应该尝试通过递增文档计数对您的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
              }
            }
          ]
        }
      }
    }
  }
}

【讨论】:

  • 感谢您的回复。但我想获得与我的查询过滤范围相匹配的计数为 0 的 aggs。就像我按城市(=孟买)过滤一样。我在具有城市价值(孟买除外)的 aggs 中也得到 0 个计数项。 Elastic 在他们的网站上提到它应该像这样工作。您的回答给了我一种思考限制大小的方法。但这将是一个非常混乱的解决方案,添加了其他过滤器,因为每次我的过滤器更改时都需要对其进行自定义。从这里有什么办法吗? @Val
  • 嗯,默认行为是聚合在过滤文档的上下文中工作,因此在您的情况下,聚合桶应该只包含过滤器匹配的city 术语。您能否举例说明您获得的结果以及您拥有的一些示例文档?
  • 添加了一些示例文档索引它们,然后运行我与 min_count = 0 共享的相同查询,您仍然会在 count= 0 的位置获得“chennai”。
  • 我明白你现在的意思了。我已经修改了我的查询,让它按照你喜欢的方式工作。
  • 这不是我要找的,聚合适用于字段数据。它是一个黑客。我需要找到一种方法来根据城市过滤索引的字段数据,然后在过滤后的字段数据上运行聚合器......如果找到,谢谢分享解决方案。
猜你喜欢
  • 2019-08-06
  • 2014-07-09
  • 2018-11-28
  • 2021-06-06
  • 2017-10-27
  • 2021-06-06
  • 2015-11-06
  • 2015-01-21
  • 2015-06-26
相关资源
最近更新 更多