elastic / elasticsearch-java

Official Elasticsearch Java Client
Apache License 2.0
408 stars 237 forks source link

sourcesIncludes and sourcesExcludes is missing on SearchRequest #773

Closed MehdiBH closed 4 months ago

MehdiBH commented 5 months ago

Description

Hello, On SearchRequest class we do not have the possibility to define [sourceIncludes](https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.13.1/co/elastic/clients/elasticsearch/core/GetRequest.html#sourceIncludes())() and [sourceExcludes](https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.13.1/co/elastic/clients/elasticsearch/core/GetRequest.html#sourceExcludes())() like in java class GetRequest. However the functionality is present in the Elasticsearch documentation : ES documentation

_source_excludes (Optional, string) A comma-separated list of source fields to exclude from the response.

You can also use this parameter to exclude fields from the subset specified in _source_includes query parameter.

If the _source parameter is false, this parameter is ignored.

_source_includes (Optional, string) A comma-separated list of source fields to include in the response.

If this parameter is specified, only these source fields are returned. You can exclude fields from this subset using the _source_excludes query parameter.

If the _source parameter is false, this parameter is ignored.

l-trotta commented 5 months ago

Hello, thank you for reporting this! Currently in the java client those two options are present as part of the source field:

esClient
    .search(s -> s
        .source(so -> so
            .filter(f -> f
                .excludes("field1", "field2")
                .includes("field3")))
    , Object.class);

However I'm not sure why it differs from what the documentation specifies, I'll investigate this.

l-trotta commented 4 months ago

Here's why: the server accepts both the query parameters (_source_excludes, _source_includes), and the source nested structure in the body to define which fields to exclude from the response. They're equivalent, and the general approach the java client follows when requests have redundant query parameters is to just provide the builders for the body options.