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)
.setFetchSource(new String[]{"STATUS", "DTCREATED"}, null)

	.addSort("DTCREATED", SortOrder.ASC)

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 =
+ "\"_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.