# Can I dynamically extract fields from a document?

**URL:** <https://discuss.elastic.co/t/can-i-dynamically-extract-fields-from-a-document/158986>\
**Category:** Elasticsearch\
**Created:** [December 1, 2018, 1:05am UTC](https://discuss.elastic.co/t/can-i-dynamically-extract-fields-from-a-document/158986 "2018-12-01T01:05:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeffrey\_Pinyan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jeffrey_pinyan/32/38316_2.png) [@Jeffrey\_Pinyan](https://discuss.elastic.co/u/Jeffrey_Pinyan)\
**Post date:** [December 1, 2018, 1:05am UTC](https://discuss.elastic.co/t/can-i-dynamically-extract-fields-from-a-document/158986/1 "2018-12-01T01:05:25Z")

</div>

Can I dynamically select fields from my document to return, based on a query? Say I have a document like so:

`{"name":"jeff","friends":[{"name":"chad","age":29},{"name":"damian","age":23},{"name":"chris","age":21}]}`

I would like the returned friends to include only those with an age above 21, e.g:

`{"name":"jeff","friends":[{"name":"chad","age":29},{"name":"damian","age":23}]}`

Can I do this in a query (presumably with a script), or must I do it after the response has been received?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [December 3, 2018, 9:57am UTC](https://discuss.elastic.co/t/can-i-dynamically-extract-fields-from-a-document/158986/2 "2018-12-03T09:57:52Z")

</div>

take a look at the [nested datatype](https://www.elastic.co/guide/en/elasticsearch/reference/6.5/nested.html) and its corresponding queries and especially the [inner hits](https://www.elastic.co/guide/en/elasticsearch/reference/6.5/search-request-inner-hits.html) functionality.

--Alex

---

<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:** [December 31, 2018, 9:57am UTC](https://discuss.elastic.co/t/can-i-dynamically-extract-fields-from-a-document/158986/3 "2018-12-31T09:57:52Z")

</div>

Closed on December 31, 2018, 9:57am UTC

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