# Conditionally retrieve a field in source

**URL:** <https://discuss.elastic.co/t/conditionally-retrieve-a-field-in-source/89787>\
**Category:** Elasticsearch\
**Created:** [June 16, 2017, 11:36pm UTC](https://discuss.elastic.co/t/conditionally-retrieve-a-field-in-source/89787 "2017-06-16T23:36:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fqye](https://avatars.discourse-cdn.com/v4/letter/f/c5a1d2/32.png) [@fqye](https://discuss.elastic.co/u/fqye)\
**Post date:** [June 16, 2017, 11:36pm UTC](https://discuss.elastic.co/t/conditionally-retrieve-a-field-in-source/89787/1 "2017-06-16T23:36:41Z")

</div>

Hi, I have a special requirement that I don't know how to do it. Help much appreciated.

I have a type that has field indicating if another field should be revealed. For example, a field is ifRevealAge. If true, than query result for the document should include the age field. If false, than age field should not be included in return result.

Is it possible to do it by a query condition or script?

If not, how to do it via java client api? I know the method toXContent(XContentBuilder builder, ToXContent.Params params) is supposed to do so but I can't find any documentation or references for how to use the method for filtering. The api documentation is just too concise.

Many thanks.

---

<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:** [July 14, 2017, 11:36pm UTC](https://discuss.elastic.co/t/conditionally-retrieve-a-field-in-source/89787/2 "2017-07-14T23:36:48Z")

</div>

Closed on July 14, 2017, 11:36pm UTC

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