Elassandra / Elastic Search中的聚合,日期范围查询

问题描述 投票:0回答:1

搜索日期范围聚合索引时获得不同的结果。

创建如下索引。

curl -XPUT -H 'Content-Type: application/json' 'http://x.x.x.x:9200/date_index' -d '{
  "settings" : { "keyspace" : "keyspace1"},
  "mappings" : {
    "table1" : {
      "discover":"sent_date",
      "properties" : {
        "sent_date" : { "type": "date", "format": "yyyy-MM-dd HH:mm:ssZZ" }
        }
    }
  }
}'

尝试使用以下代码进行搜索时,我会得到不同的日期范围结果。

    curl -XGET -H 'Content-Type: application/json' 'http://x.x.x.x:9200/date_index/_search?pretty=true' -d '
    {
      "aggs" : {
        "sentdate_range_search" : {
          "date_range" : {
            "field" : "sent_date",
            "time_zone": "UTC",
            "format" : "yyyy-MM-dd HH:mm:ssZZ",
            "ranges" : [
              { "from" : "2010-05-07 11:22:34+0000", "to" : "2011-05-07 11:22:34+0000"}
            ]
      }
    }
  }
}'

样本输出,显示不同的结果,如2039,2024等。

{
  "took" : 26,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 417427,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "date_index",
        "_type" : "table1",
        "_id" : "P89200822_4210021505784",
        "_score" : 1.0,
        "_source" : {
          "sent_date" : "2039-05-22T14:45:39.000Z"
        }
      },
      {
        "_index" : "date_index",
        "_type" : "table1",
        "_id" : "P89200605_4210020537428",
        "_score" : 1.0,
        "_source" : {
           "sent_date" : "2024-06-05T07:20:57.000Z"
        }
      },
      .........
    "aggregations" : {
    "sentdate_range_search" : {
      "buckets" : [
        {
          "key" : "2010-05-07 11:22:34+00:00-2011-05-07 11:22:34+00:00",
          "from" : 1.273231354E12,
          "from_as_string" : "2010-05-07 11:22:34+00:00",
          "to" : 1.304767354E12,
          "to_as_string" : "2011-05-07 11:22:34+00:00",
          "doc_count" : 0
         }
      ]
    }
  }

仅供参考:我使用的是Cassandra数据库中的数据,其中“sent_date”字段以UTC时区存储。

请指教,谢谢

elasticsearch cassandra elassandra
1个回答
0
投票

==根据评论中的对话重新设定答案==

聚合与搜索查询不同。聚合将记录(即聚合!)与指定的维度组合在一起。问题中的查询将两个指定日期之间的记录聚合到一个存储桶中。有关聚合的更多信息,请访问the Elasticsearch documentation

由于要求是过滤两个日期之间的记录,因此日期范围过滤器是适当的方法:

GET date_index/_search
{
   "query": {
       "bool": {
           "filter": {
               "range": {
                   "sent_date": {
                       "gte": "2010-05-07 11:22:34+0000",
                       "lte": "2011-05-07 11:22:34+0000"
                   }
               }
            }
        }
    }
}

为什么过滤而不是常规查询?过滤器比搜索更快,因为它们不会对文档评分做出贡献,而且它们可以缓存。例如,您可以将过滤器和搜索组合在一起,获取与给定时间范围内的所有记录匹配的所有记录“所有工作和没有游戏使杰克成为一个沉闷的男孩”。

© www.soinside.com 2019 - 2024. All rights reserved.