【问题标题】:ElasticSearch- not returning proper value countElasticSearch-没有返回正确的值计数
【发布时间】:2014-12-26 16:25:30
【问题描述】:

ES 1.4.2 Value Count 聚合返回的值不正确。
当我运行以下查询以获取“持续时间”字段中数组元素的总数时,value_count 聚合器正在获取唯一值的计数。

查询:

{  
  "query": {  
    "filtered": {
      "query": {
        "match": {
          "sessions.applicationId": {
            "query": 208,
            "type": "boolean"
          }
        }
      },
      "filter": {
        "and": {
          "filters": [
            {
              "range": {
                "eventDate": {
                  "from": 1388916360000,
                  "to": 1389402273384,
                  "include_lower": true,
                  "include_upper": true
                }
              }
            }
          ]
        }
      }
    }
  },
  "aggregations": {
    "Session_Count": {
      "value_count": {
        "field": "durations"
      }
    }
  }
}

结果命中

"hits": [
  {
    "_index": "users",
    "_type": "sessions",
    "_id": "18967_20140105_CF538C86DEBC432DBDE40887FE6CA051",
    "_score": 1,
    "_source": {
      "eventDate": "2014-01-05T17:01:18",
      "manufacturer": "apple",
      "applicationId": "208",
      "durations": [
        2,
        2
      ]
    }
  },
  {
    "_index": "users",
    "_type": "sessions",
    "_id": "2386_20140109_5AC476D2FC784826A3B3A6584578597E",
    "_score": 1,
    "_source": {
      "eventDate": "2014-01-09T15:55:53",
      "manufacturer": "apple",
      "applicationId": "208",
      "durations": [
        1,
        1
      ]
    } 
]

"aggregations": {
  "Session_Count": {
    "value": 2
  }
}

在“持续时间”数组 [2,2] 和 [1,1] 的 value_count 的上述结果中,结果为 2。我期望它为 4,如 ES 参考文档中给出的那样。
对于不同的值,例如持续时间 [1,2],[3],[3,2,4] 的值是正确的,它是 6。

这是 Elastic Search Value_count 功能的问题还是我在这里遗漏了什么。
谁能告诉我这个。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    value_count 为您提供唯一值的数量,而不是值的总数。

    要获取值的总数,您需要使用以下 -

    将持续时间声明为多字段并添加一个名为totalTokens 的字段,类型为token_count。 链接 - http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html#token_count

    这里发生的情况是,每个文档都会创建一个名为 durations.totalTokens 的附加字段,其中包含该字段的令牌数量。 现在应用该字段的总和聚合以获得正确答案。

    {  
      "query": {  
        "filtered": {
          "query": {
            "match": {
              "sessions.applicationId": {
                "query": 208,
                "type": "boolean"
              }
            }
          },
          "filter": {
            "and": {
              "filters": [
                {
                  "range": {
                    "eventDate": {
                      "from": 1388916360000,
                      "to": 1389402273384,
                      "include_lower": true,
                      "include_upper": true
                    }
                  }
                }
              ]
            }
          }
        }
      },
      "aggregations": {
        "Session_Count": {
          "sum": {
            "field": "duration.totalTokens"
          }
        }
      }
    }
    

    【讨论】:

    • 谢谢。有什么办法可以得到有条件的部分计数。查找持续时间 > 5 的会话数
    • 我觉得它与当前的问题不同,而且这本身应该是一个问题,您能否将其粘贴为不同的问题?
    【解决方案2】:

    在这里具体说明一下,聚合总是返回单个存储桶的计数。这就是为什么它返回单个持续时间的计数而不是完整的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      相关资源
      最近更新 更多