Jen
Jen

Reputation: 1338

How to exclude source fields in search APIs?

Elasticsearch Java High Level REST Client's GET API provides a way to control which fields of the _source are fetched.

val request = GetRequest(index)
    .id(id)
    .fetchSourceContext(FetchSourceContext(true, includedFields, excludedFields))

elasticClient.get(request, RequestOptions.DEFAULT)

How can I achieve this with the Search APIs?

For example for the following search request:

val source = SearchSourceBuilder()
source.query(QueryBuilders.matchAllQuery())
val request = SearchRequest(index)
    .source(source)
elasticClient.search(request, RequestOptions.DEFAULT)

Upvotes: 1

Views: 2286

Answers (1)

Amit
Amit

Reputation: 32386

Please refer this from official ES doc,

This method also accepts an array of one or more wildcard patterns to control which fields get included or excluded in a more fine-grained way:

code block

String[] includeFields = new String[] {"title", "innerObject.*"};
String[] excludeFields = new String[] {"user"};
sourceBuilder.fetchSource(includeFields, excludeFields);

Simlar to get API which you already mentioned, you can provide an array of includeFields and excludeFields to control fetching of the fields from _source fields.

Upvotes: 2

Related Questions