0

I am working on ElasticSearch 2.3.1. I have a requirement to fetch data for last 90 days. In this I need data as well as minute wise aggregation. Problem which I am facing is that I am able to get data for last 90 days but not able to apply date range on the aggregation.

So I have two problems

Problem 1 – This should work on a result set of a match query field is different.

Problem 2 – Java API for the above query

Query which I am using is as follows:

GET _search
{
    "aggs": {
    "t1": {
      "filter": {
        "range": {
          "timestamp": {
            "from" : "now-1d/d",
        "to" : null,
        "format" : "epoch_millis",
        "include_lower" : true,
        "include_upper" : true
          }
        }
      },
      "aggs": {
        "t2": {
          "date_histogram": {
            "field": "timestamp",
            "interval": "1m"
          }
        }
      }
    }
  }
}

In short I need Java API of following type

Java API for full:
match query {
  aggregation {
     filter aggregation,
     date histogram
     }
}

1 Answer 1

1

For Problem 1 the following query works:

GET _search {
 "query": {
  "match": {
   "body": "Text_To_Search"
  }
 },
 "aggs": {
  "outer_agg": {
   "filter": {
    "bool": {
     "must": [{
      "range": {
       "timestamp": {
        "from": "now-90d/d",
        "to": null,
        "format": "epoch_millis",
        "include_lower": true,
        "include_upper": true
       }
      }
     }]
    }
   },
   "aggs": {
    "inner_agg": {
     "date_histogram": {
      "field": "timestamp",
      "interval": "1m"
     }
    }
   }
  }
 }
}

The Java API for the same is:

DateHistogramBuilder dhb = AggregationBuilders.dateHistogram("t2").field("timestamp").interval(DateHistogramInterval.MINUTE);

BoolQueryBuilder bqb = QueryBuilders.boolQuery().must(QueryBuilders.rangeQuery("timestamp").gte("now-90d").to("now").format("epoch_millis"));

FilterAggregationBuilder fab = AggregationBuilders.filter("t1").filter(bqb).subAggregation(dhb);

SearchResponse sr = TEFESConnector.getInstance().getClient().prepareSearch("index_name").setTypes("type_name").setQuery(QueryBuilders.matchQuery("field_to_search", "text_to_search")).addAggregation(fab).execute().actionGet();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.