# Java Range Aggregation over Unix timestamps

**URL:** <https://discuss.elastic.co/t/java-range-aggregation-over-unix-timestamps/52298>\
**Category:** Elasticsearch\
**Created:** [June 9, 2016, 8:35am UTC](https://discuss.elastic.co/t/java-range-aggregation-over-unix-timestamps/52298 "2016-06-09T08:35:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvieghofer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvieghofer/32/10214_2.png) [@mvieghofer](https://discuss.elastic.co/u/mvieghofer)\
**Post date:** [June 9, 2016, 8:35am UTC](https://discuss.elastic.co/t/java-range-aggregation-over-unix-timestamps/52298/1 "2016-06-09T08:35:02Z")

</div>

Hi everybody!  
In our elasticsearch (version 2.3.1) index we have a `showDate` field for every document. This field contains a unix timestamp and I want to find out how many documents have a `showDate` in the last 24 hours and how many documents have a `showDate` in the last 7 days.

In my Java code this is how I tried to accomplish this:

```auto
public static final String TODAY = "today";
public static final String THIS_WEEK = "thisWeek";
protected static final int SECONDS_PER_DAY = (24 * 60 * 60);

//...

Date now = new Date();
double timestampNow = now.getTime() / 1000;
double timestampYesterday = timestampNow - SECONDS_PER_DAY;
double timestampThisWeek = timestampNow - (SECONDS_PER_DAY * 7);
AbstractAggregationBuilder aggregation = AggregationBuilders.range("time").field("showDate")
                .addRange(TODAY, timestampYesterday, timestampNow)
                .addRange(THIS_WEEK, timestampThisWeek, timestampNow);
searchRequestBuilder.addAggregation(aggregation);

```

When I execute this searchRequest and look at the produced query I get something like this

```auto
{
  "from": 0,
  "size": 15,
  "query": {
      ...
  }
  "aggregations": {
    "time": {
      "range": {
        "field": "showDate",
        "ranges": [
          {
            "key": "today",
            "from": 1.465369290E9,
            "to": 1.465455690E9
          },
          {
            "key": "thisWeek",
            "from": 1.464850890E9,
            "to": 1.465455690E9
          }
        ]
      }
    }
  }
}

```

When I execute the query in a plugin such as Kopf or Head I get the expected result. When I execute it in my java service the docCount is always 0.

The only difference I noticed is that the plugins convert the double value into a long value. So after I sent the query the `1.465455690E9` numbers are converted to `1465455690`. Does anyone know how I get the same result in my Java service as I get from the plugins?

**Update 1**  
I've just updated my code and I use a dateRange now. This gives me the expected query, however the DocCount is still always 0.

```auto
// ...
Date now = new Date();
long timestampNow = now.getTime() / 1000;
long timestampYesterday = timestampNow - SECONDS_PER_DAY;
long timestampThisWeek = timestampNow - (SECONDS_PER_DAY * 7);
AbstractAggregationBuilder aggregation = AggregationBuilders.dateRange(KEY).field("showDate").format("epoch_second")
                .addRange(TODAY, timestampYesterday, timestampNow)
                .addRange(THIS_WEEK, timestampThisWeek, timestampNow);
// ...

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:45pm UTC](https://discuss.elastic.co/t/java-range-aggregation-over-unix-timestamps/52298/2 "2017-07-05T22:45:04Z")

</div>


