Search code examples
javaelasticsearchelasticsearch-java-api

Elasticsearch java api: how to define fields as you would with _source:[] in dsl


in dsl I use _source: [] to define what fields I want returned in the response:

{
    "size":100,
    "_source": [ "field1", "field2", "field3", "field4" ]
}

but I am having trouble figuring this out with the Java API Transport Client. Below is what I use to construct and call the query, so I am not sure what to use for this.

SearchResponse sr = mysvc.getClient().prepareSearch(index)
     .setSize(size)
     .setQuery(getBoolQuery())
     .addAggregation(getAggregationBuilder())
     .execute()
     .actionGet()

there is a .setSource() that can be used here but it takes a SearchSourceBuilder and I haven't any documentation on how to use it.


Solution

  • Maybe this would be helpful for you: