# Elasticsearch Java API client about source filtering function

**URL:** <https://discuss.elastic.co/t/elasticsearch-java-api-client-about-source-filtering-function/321173>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [December 14, 2022, 1:04am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-about-source-filtering-function/321173 "2022-12-14T01:04:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cchuang4](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cchuang4/32/114678_2.png) [@cchuang4](https://discuss.elastic.co/u/cchuang4)\
**Post date:** [December 14, 2022, 1:04am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-about-source-filtering-function/321173/1 "2022-12-14T01:04:57Z")

</div>

Hello,  
I would like to migrate from High Level rest Java to new java api client.  
I have old method using the source filtering feature in High level rest java, like

```auto
 sourceBuilder.fetchSource(includeFields, excludeFields);

```

In new java api, I knew the SourceBuilder is not available, but shoud some function be used like fetchSource() function to let me custom return fields of query?

Many thanks.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [December 14, 2022, 2:34am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-about-source-filtering-function/321173/2 "2022-12-14T02:34:44Z")

</div>

Hi @cchuang4

You can try this part

```
SearchRequest searchRequest = new SearchRequest.Builder()
    .index("idx_name")
    .query(query)
    .source(SourceConfig.of(s -> s.filter(f -> f.includes(List.of("fieldA", "fieldB")))))
    .build();

```

SourceConfig give access to include and exclude fields.

---

<div class="post-metadata">

**Author:** ![cchuang4](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cchuang4/32/114678_2.png) [@cchuang4](https://discuss.elastic.co/u/cchuang4)\
**Post date:** [December 19, 2022, 7:05am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-about-source-filtering-function/321173/3 "2022-12-19T07:05:10Z")

</div>

Thank you for your example.

---

<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:** [January 16, 2023, 7:05am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-about-source-filtering-function/321173/4 "2023-01-16T07:05:29Z")

</div>

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