0

I am looking for a way to use the native elasticsearch client in order to retrieve the source when script fields are used.

Here is a way to do it using JSON/http:

https://stackoverflow.com/a/11856215/536299

I have not been able to find a way to achieve this through the native java client.

Can anyone please help?

Community
  • 1
  • 1
balteo
  • 23,602
  • 63
  • 219
  • 412

1 Answers1

0

It turned out there is a setFetchSource(boolean fetch) method available on SearchRequestBuilder.

See below:

SearchResponse searchResponse = client
                .prepareSearch(ELASTICSEARCH_INDEX)
                .setTypes(ELASTICSEARCH_TYPE)
                .setFetchSource(true)//Notice here
                .addScriptField("distance", distanceScript(queryForm))
                .addAggregation(aggregations(queryForm))
                .setQuery(filters(queryForm))
                .execute()
                .actionGet();

This allows to fetch the source even when a script field is used.

balteo
  • 23,602
  • 63
  • 219
  • 412