【问题标题】:cardinality aggregation within filter aggregation过滤器聚合中的基数聚合
【发布时间】:2017-03-07 21:42:30
【问题描述】:

我正在尝试使用基数聚合来计算不同值。

这是我的查询

{
    "size": 100,
    "_source":["awardeeName"],
    "query": {
        "match_phrase":{"awardeeName" :"The President and Fellows of Harvard College" }  
    },
    "aggs":{
        "awardeeName": {
            "filter" : { "query": { "match_phrase":{"awardeeName" :"The President and Fellows of Harvard College" }}},
            "aggs": {
                "distinct":{"cardinality":{  "field": "awardeeName"}}
           }
        }

    }               
}

使用 match_phrase 查询某些文本,使用相同的匹配短语进行聚合,然后调用基数, 结果,命中数和聚合过滤器匹配,但基数显示的数字比过滤器和总命中数惊人地大,这是结果

  {
    "took": 37,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 13.516766,
        "hits": [
            {
                "_index": "development",
                "_type": "document",
                "_id": "140a3f5b-e876-4542-b16d-56c3c5ae0e58",
                "_score": 13.516766,
                "_source": {
                    "awardeeName": "The President and Fellows of Harvard College"
                }
            },
            {
                "_index": "development",
                "_type": "document",
                "_id": "5c668b06-c612-4349-8735-2a79ee2bb55e",
                "_score": 12.913888,
                "_source": {
                    "awardeeName": "The President and Fellows of Harvard College"
                }
            },
            {
                "_index": "development",
                "_type": "document",
                "_id": "a9560519-1b2a-4e64-b85f-4645a41d5810",
                "_score": 12.913888,
                "_source": {
                    "awardeeName": "The President and Fellows of Harvard College"
                }
            }
        ]
    },
    "aggregations": {
        "awardeeName": {
            "doc_count": 3,
            "distinct": {
                "value": 7
            }
        }
    }
}

我希望基数适用于过滤器的结果,但在这种情况下基数显示 7 ,为什么显示 7 ?不同值的计数如何超过总点击数?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    awardeeName 字段上的 cardinality 聚合计算所有匹配文档在该字段上存在的不同标记的数量。

    在您的情况下,在三个匹配的文档中,awardeeName 字段包含完全相同的值 The President and Fellows of Harvard College,它恰好具有 7 个标记,因此您看到的结果是 7。

    您可能想要实现的是将 The President and Fellows of Harvard College 计为单个令牌,为此您需要一个 keyword field(而不是 text 一个)并在您的 cardinality 聚合中使用该字段。

    【讨论】:

    • keyword 将返回 0 条记录,如果匹配查询值 'college',我想同时搜索 match 和 match_phrase,保留数据类型文本,有没有办法计算不同的字段值?
    • 还有另一种方法可以将此属性映射为两个不同的字段,一个用于使用文本分析器进行全文搜索,另一个用于使用关键字分析器进行不同计数等?
    • 没错,您可能需要一个多字段,即一个用于全文搜索的字段和另一个带有关键字分析器的子字段。
    【解决方案2】:

    例子:

    GET calserver-2021.04.1*/_search
    {
      "size": 0,
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "method.keyword": "searchUser"
              }
            },
            {
              "term": {
                "statusCode": "500"
              }
            }
          ]
        }
      },
      "aggs": {
        "username_count": {
          "cardinality": {
            "field": "username.keyword",
            "precision_threshold": 40000
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 2022-01-16
      • 2015-09-17
      • 2014-02-02
      • 2017-03-09
      • 2020-07-26
      • 2020-06-20
      相关资源
      最近更新 更多