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

**URL:** <https://discuss.elastic.co/t/refactoring-java-api-code-from-2-x-to-5-x/75496>\
**Category:** Elasticsearch\
**Created:** [February 17, 2017, 1:11pm UTC](https://discuss.elastic.co/t/refactoring-java-api-code-from-2-x-to-5-x/75496 "2017-02-17T13:11:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![living\_jordi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/living_jordi/32/61363_2.png) [@living\_jordi](https://discuss.elastic.co/u/living_jordi)\
**Post date:** [February 17, 2017, 1:11pm UTC](https://discuss.elastic.co/t/refactoring-java-api-code-from-2-x-to-5-x/75496/1 "2017-02-17T13:11:25Z")

</div>

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?

Code:

```
return AggregationBuilders
    .terms(this.getName())
    .field(this.getName())
    .script(new Script(
        ScriptType.FILE,
        "painless",
        "year",
        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.

Thanks.

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [February 17, 2017, 3:45pm UTC](https://discuss.elastic.co/t/refactoring-java-api-code-from-2-x-to-5-x/75496/2 "2017-02-17T15:45:23Z")

</div>

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](https://github.com/elastic/elasticsearch/tree/5.x/core/src/main/java/org/elasticsearch/search/aggregations/metrics) for reference (and a maybe more suitable base class, depending on your concrete use case).

Daniel

---

<div class="post-metadata">

**Author:** ![living\_jordi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/living_jordi/32/61363_2.png) [@living\_jordi](https://discuss.elastic.co/u/living_jordi)\
**Post date:** [February 20, 2017, 7:57am UTC](https://discuss.elastic.co/t/refactoring-java-api-code-from-2-x-to-5-x/75496/3 "2017-02-20T07:57:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 20, 2017, 7:58am UTC](https://discuss.elastic.co/t/refactoring-java-api-code-from-2-x-to-5-x/75496/4 "2017-03-20T07:58:18Z")

</div>

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