Elasticsearch Query using JSON


(Aniket) #1

I have written code to extract data from elasticsearch using Java Client. Code is as below.

SearchResponse responseOutput = client.prepareSearch(ConstantsValue.indexName)
.setTypes(ConstantsValue._Type)
.setFetchSource(new String[]{"STATUS", "DTCREATED"}, null)
.setQuery(
QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),

FilterBuilders.rangeFilter("DTCREATED").from(fromDate).to(toDate))).
addAggregation(AggregationBuilders.terms("STATUS_AGG").field("STATUS")
.subAggregation(AggregationBuilders.dateHistogram("DATE_AGG").field("DTCREATED").interval(interval).format(dateFormat))
	)
	.addSort("DTCREATED", SortOrder.ASC)
	.get();

This query returns me count for status day wise within particular date range in ascending date order.

But now I want to fire same query using json String. I tried my best to written json based query.

//Query to elasticsearch 
SearchRequest requestQuery =
Requests.searchRequest(ConstantsValue.indexName)
.types(ConstantsValue._Type)
.source("{size:999999,"
+ "\"_source\" : "
+ "[\"DTCREATED\", \"STATUS\"]"
+ ",\"aggs\": "
+ "{\"group_by_STATUS\": {\"terms\": {\"field\": \"STATUS\"},"
+ "\"aggs\" : "
+ "{\"group_by_DATE\" : {\"date_histogram\" : "
+ "{\"field\" : \"DTCREATED\", \"interval\" : \"month\","
+ "\"format\" : \"yyyy-MM-dd\" },"
+ "\"aggs\" : "
+ "{\"grades_count\" : { \"value_count\" : { \"field\" : \"STATUS\" } }}}}}}}");

Now how can I add date filter and dates in ascending order condition in this JSON query. Date field contains both date and time. Any help is greatly appreciated.


(system) #2

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