ElasticSearch-没有返回正确的值计数

时间:2014-12-25 20:11:08

标签: elasticsearch

ES 1.4.2 Value Count聚合返回incorret值 当我在查询下面运行以获取“durations”字段中数组元素的总数时,value_count aggr将获取唯一值的计数。

查询:

{  
  "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
  }
}

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

这是弹性搜索Value_count功能的问题还是我在这里遗漏了什么。
任何人都可以告诉我这件事。

2 个答案:

答案 0 :(得分:0)

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"
      }
    }
  }
}

答案 1 :(得分:0)

在这里具体来说,聚合总是返回各个桶的计数。这就是为什么它返回个别持续时间的计数而不是完整的结果。

相关问题