My code has an ElasticSearch query and aggregations in JSON format, and wants to call the ElasticSearch Java API.
For the Query portion, I can use WrapperQuery to build the query from the JSON as so:
val query = Json.obj(
"query_string" -> Json.obj("query" -> "*"))
val aggs = Json.obj(
"gender" -> Json.obj("terms" -> Json.obj("field": "gender")),
"age" -> Json.obj("terms" -> Json.obj("field": "age")))
val aggsRequestBuilder = new SearchRequestBuilder(client)
.setIndices(index())
.setQuery(QueryBuilders.wrapperQuery(query.toString())
.addAggregation(AggregationBuilders.???(aggs.toString())
But then, I also have the JSON for the aggregations and I don't see an AggregationsBuilder.wrapperAggregation() function that I can use to build aggregations object from JSON.
val aggsRequestBuilder = new SearchRequestBuilder(client)
.setIndices(index())
.setQuery(QueryBuilders.wrapperQuery(query.toString())
.addAggregation(AggregationBuilders.terms("gender").field("gender"))
.addAggregation(AggregationBuilders.terms("age").field("age"))
Updating this old thread as @monde shared with me a solution he found on SOF:
val aggsRequestBuilder = new SearchRequestBuilder(client)
.setIndices(index())
.setQuery(QueryBuilders.wrapperQuery(query.toString())
.setAggregations(agg.toString().getBytes())
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.