Refactoring Java API code from 2.x to 5.x

I've updated to elasticsearch java library version 5.2.0.

In 2.x,

  1. I was used to take MetricsAggregationBuilder. What do I need to do in order to refactor my related code?
  2. I was using SearchRequestBuilder.addField() in order to add a field to the search request. Nevertheless, It seems to be replaced. I've written the available methods intellisense is showing me. Which of them do I need to pick?
  • addDocValueField
  • addFieldDataField
  • addScriptField
  • addStoredField
  • storedFields
  • fields
  1. SearchRequestBuilder.setNoFields is also removed. Which would be the alternative?
  2. Currently, I'm calling scripts from Java using this code. Is there any more elegant way to call it in 5.x Java API?


return AggregationBuilders
    .script(new Script(
        ImmutableMap.of("field", this.getName())

As you can see I setting field as script parameter. Nevertheless, I don't quite understand how to get it from script code.


Hi @living_jordi,

we heavily refactored the query-related code in Elasticsearch 5 and MetricsAggregationBuilder is now eliminated. You most likely want to use AbstractAggregationBuilder as your new base class.

You should also check the code in the metrics package for reference (and a maybe more suitable base class, depending on your concrete use case).


Thanks @danielmitterdorfer.

Generally, I've just replaced old MetricsAggregationBuilder to AggregationBuilder. It seems to work by now.

Nevertheless, I've just edited the post adding some additional general 5.x refactorings issues I would need to know how to refactor them. Could you quite help out to solve them?

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