2017-09-01 32 views
0

特定の日時ウィンドウ(つまり2017-02-17T15:00:00.0002017-02-17T16:00:00.000の間)のエントリ数を検索するクエリがあります。私は次のように2つのエントリを持ってmyindex"aggs"クエリの出力結果が正しくありません

{ 
    "took": 0, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 11, 
    "max_score": 0, 
    "hits": [] 
    }, 
    "aggregations": { 
    "range": { 
     "buckets": [ 
     { 
      "key": "*-2017-02-17T15:00:00.000Z", 
      "to": 1487343600000, 
      "to_as_string": "2017-02-17T15:00:00.000Z", 
      "doc_count": 0 
     }, 
     { 
      "key": "2017-02-17T16:00:00.000Z-*", 
      "from": 1487347200000, 
      "from_as_string": "2017-02-17T16:00:00.000Z", 
      "doc_count": 0 
     } 
     ] 
    } 
    } 
} 

:これは出力され

POST /myindex/_search 
{ 
    "size": 0, 
    "aggs": { 
    "range": { 
     "date_range": { 
      "field": "Datetime", 
      "ranges": [ 
       { "to": "2017-02-17T16:00:00||-1H/H" }, 
       { "from": "2017-02-17T16:00:00||/H" } 
      ] 
     } 
    } 
} 
} 

:私はこのクエリを実行すると、私は間違った結果を得る(それが良いでしょうが、結果は予想外であることを言いました) Datetimeの値:だから

2017-02-17T15:15:00.000Z 
2017-02-17T15:02:00.000Z 

、結果は2

と同じでなければなりません

現在の出力をどのように解釈するのか分かりません。エントリの数を定義するフィールドはどれですか?

UPDATE:

データ構造:

PUT /myindex 
{ 
    "mappings": { 
     "intensity": { 
     "_all": { 
     "enabled": false 
     }, 
     "properties": { 
      "Country_Id": { 
      "type":"keyword" 
      }, 
      "Datetime": { 
      "type":"date" 
      } 
     } 
     } 
    } 
} 

サンプルデータ:

{ 
    "took": 0, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 5, 
    "max_score": 1, 
    "hits": [ 
     { 
     "_index": "myindex", 
     "_type": "intensity", 
     "_id": "4", 
     "_score": 1, 
     "_source": { 
      "Country_Id": "1", 
      "Datetime": "2017-02-18T15:01:00.000Z" 
     } 
     }, 
     { 
     "_index": "myindex", 
     "_type": "intensity", 
     "_id": "6", 
     "_score": 1, 
     "_source": { 
      "Country_Id": "1", 
      "Datetime": "2017-03-16T16:15:00.000Z" 
     } 
     }, 
     { 
     "_index": "myindex", 
     "_type": "intensity", 
     "_id": "1", 
     "_score": 1, 
     "_source": { 
      "Country_Id": "1", 
      "Datetime": "2017-02-17T15:15:00.000Z" 
     } 
     }, 
     { 
     "_index": "myindex", 
     "_type": "intensity", 
     "_id": "7", 
     "_score": 1, 
     "_source": { 
      "Country_Id": "1", 
      "Datetime": "2017-03-16T16:18:00.000Z" 
     } 
     }, 
     { 
     "_index": "myindex", 
     "_type": "intensity", 
     "_id": "3", 
     "_score": 1, 
     "_source": { 
      "Country_Id": "1", 
      "Datetime": "2017-02-17T15:02:00.000Z" 
     } 
     } 
    ] 
    } 
} 

私が得る答え:

{ 
    "took": 2, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 11, 
    "max_score": 0, 
    "hits": [] 
    }, 
    "aggregations": { 
    "range": { 
     "buckets": [ 
     { 
      "key": "2017-02-17T15:00:00.000Z-2017-02-17T16:00:00.000Z", 
      "from": 1487343600000, 
      "from_as_string": "2017-02-17T15:00:00.000Z", 
      "to": 1487347200000, 
      "to_as_string": "2017-02-17T16:00:00.000Z", 
      "doc_count": 0 
     } 
     ] 
    } 
    } 
} 
+0

私は間違っていない場合は、最初の範囲は、 '15に行く:00及び第二の範囲は、' 16から開始しますので、午後03時15分、00 'そして15:02は真ん中にあります。 – Val

+0

@Val:「2017-02-17T15:00:00.000」と「2017-02-17T16:00:00.000」の間のレコード数を取得します。私の質問で何が間違っていますか? – Dinosaurius

+1

単に時間単位の間隔で 'date_histogram'を使用するのはなぜですか? – Val

答えて

2

のあなたの範囲が間違っている、このようにそれを行う代わりに、

POST /myindex/_search 
{ 
    "size": 0, 
    "aggs": { 
    "range": { 
     "date_range": { 
      "field": "Datetime", 
      "ranges": [ 
       { 
        "from": "2017-02-17T16:00:00Z||-1H/H", 
        "to": "2017-02-17T16:00:00Z||/H" 
       } 
      ] 
     } 
    } 
} 
} 
+0

'-1H'を使いたいです。出来ますか? – Dinosaurius

+0

私の答えは – Val

+0

です。ありがとう、ありがとう。私はそれをチェックし、 '' doc_count ':0'を取得します。なぜそれが起こるのか理解できません。ちなみに、あなたは "from"の最後にカンマが足りません。 – Dinosaurius

関連する問題