簡體   English   中英

Elassandra / Elastic Search中的聚合,日期范圍查詢

[英]Aggregation, Date range query in Elassandra/Elastic Search

在搜索日期范圍聚合索引時獲得不同的結果。

如下創建索引。

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文檔中找到有關聚合的更多信息。

由於要求是過濾介於兩個日期之間的記錄,因此日期范圍過濾器是合適的方法:

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

為什么要過濾而不是常規查詢? 篩選器比搜索速度快,因為它們不會有助於文檔評分,並且可以緩存。 您可以結合使用過濾器和搜索功能,例如,獲取給定時間范圍內與短語“所有工作無濟於事,使傑克成為一個愚蠢的男孩”相匹配的所有記錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM