Elasticsearch query using QueryBuilders java api


(Aniket) #1

I have added following attributes in elasticsearch.

STATUS: "SUCCESS", "ERROR", "FAIL"
DTCREATED: 2016-07-01T18:30:00.000Z

I want daily count for each status within specific date range.

SearchResponse responseOutput1 = client.prepareSearch(ConstantsValue.indexName).
setTypes(ConstantsValue._Type).
setQuery(
QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.rangeFilter("DTCREATED").from("2016-07-01T18:30:00.000Z").to("2016-07-31T18:30:00.000Z"))).
addAggregation(
AggregationBuilders.dateHistogram("dateagg").field("DTCREATED").interval(DateHistogram.Interval.DAY) 
)
.get();

How can I group date and status. get Count for each status daily by updating above query.


(Xavier Facq) #2

You need to make a subaggregation. take a look at https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html

something like that :

 SearchRequestBuilder srb = client.prepareSearch("your_index")
    .setTypes("your_document_type")
    .setQuery(QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("DTCREATED").from("2016-07-01T18:30:00.000Z").to("2016-07-31T18:30:00.000Z")))
    .addAggregation(
            AggregationBuilders.terms("STATUS").size(5).subAggregation(
            AggregationBuilders.dateHistogram("dateagg").field("DTCREATED").interval(DateHistogramInterval.DAY)));

(system) #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.