0

I have a query that searches the number of entries in a given datetime window (i.e. between 2017-02-17T15:00:00.000 and 2017-02-17T16:00:00.000). When I execute this query, I get the incorrect result (it's better said that the result is unexpected):

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

This is the output:

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

In myindex I have two entries with the following values of Datetime:

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

So, the result should be equal to 2.

I don't understand how to interpret the current output. Which fields defines the number of entries?

UPDATE:

data structure:

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

sample data:

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

The answer that I get:

{
  "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
        }
      ]
    }
  }
}
Dinosaurius
  • 8,306
  • 19
  • 64
  • 113
  • If I'm not mistaken, the first range goes to `15:00` and the second range starts from `16:00`, so 15:15 and 15:02 are just in the middle. – Val Sep 01 '17 at 10:23
  • @Val: I want to get the number of records between `2017-02-17T15:00:00.000` and `2017-02-17T16:00:00.000`. What is wrong in my query? – Dinosaurius Sep 01 '17 at 10:25
  • 1
    Why not simply using a `date_histogram` instead with an hourly interval? – Val Sep 01 '17 at 10:26
  • @Val: Because I need it for my task. I do not require the whole histogram. – Dinosaurius Sep 01 '17 at 10:52

1 Answers1

2

Your ranges are wrong, do it like this instead

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" 
                }
            ]
        }
    }
}
}
Val
  • 207,596
  • 13
  • 358
  • 360