# Flatten \_source field of query results

**URL:** <https://discuss.elastic.co/t/flatten-source-field-of-query-results/214841>\
**Category:** Elasticsearch\
**Created:** [January 13, 2020, 1:40pm UTC](https://discuss.elastic.co/t/flatten-source-field-of-query-results/214841 "2020-01-13T13:40:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssteiger](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@ssteiger](https://discuss.elastic.co/u/ssteiger)\
**Post date:** [January 13, 2020, 1:40pm UTC](https://discuss.elastic.co/t/flatten-source-field-of-query-results/214841/1 "2020-01-13T13:40:21Z")

</div>

Hello,

At our company we are using Elasticsearch together with [GraphQL](https://graphql.org/)  
The problem we are facing is that we would like to cleanly fetch and return the `_source` content of elasticsearch documents using the `@elastic/elasticsearch` [npm package](https://www.npmjs.com/package/@elastic/elasticsearch).

We got to the point where we can return documents using the following query:

```auto
await elasticsearchClient.search({
            index: 'my_index',
            body: {
              query: {
                match: {
                  topic
                }
              }
            },
            // -> don't return elasticsearch metadata
            filterPath: 'hits.hits._source',
            // NOTE: we are currently filtering out
            // the fields '@version' and '@timestamp'
            _source: ['id', 'timestamp', 'topic', 'index', 'type']
 })

```

The problem with this approach is that we get results that are formatted like this:

```auto
[ { _source:
     { topic: 'user-logged-in',
       index: 'tracking_events',
       id: '5e1b528aa1ca021659fb1b5b',
       type: 'redis-input',
       timestamp: '2020-01-12T17:08:26.094Z' } },
  { _source:
     { topic: 'user-logged-in',
       index: 'tracking_events',
       id: '5e1b56f0a1ca021659fb1b5c',
       type: 'redis-input',
       timestamp: '2020-01-12T17:27:13.020Z' } } ]

```

How can we get rid of the top-level `_source` field?

We would like to format the results and return something that looks like this:

```auto
[ { topic: 'user-logged-in',
    index: 'tracking_events',
    id: '5e1b528aa1ca021659fb1b5b',
    type: 'redis-input',
    timestamp: '2020-01-12T17:08:26.094Z' },
  { topic: 'user-logged-in',
    index: 'tracking_events',
    id: '5e1b56f0a1ca021659fb1b5c',
    type: 'redis-input',
    timestamp: '2020-01-12T17:27:13.020Z' } ]

```

Straightforward approach:

```auto
const formattedResults = hits.map(event => event._source)

```

But this of course is infeasible for obvious performance reasons.

Thank you for your help!

---

<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:** [January 13, 2020, 2:06pm UTC](https://discuss.elastic.co/t/flatten-source-field-of-query-results/214841/2 "2020-01-13T14:06:44Z")

</div>

Hey,

Elasticsearch has no mechanism to change the JSON output of a search response. the `filter_path` is just about omitting data, but not about changing the structure.

If you need this, it's best to do it on the client side.

--Alex

---

<div class="post-metadata">

**Author:** ![ssteiger](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@ssteiger](https://discuss.elastic.co/u/ssteiger)\
**Post date:** [January 13, 2020, 2:40pm UTC](https://discuss.elastic.co/t/flatten-source-field-of-query-results/214841/3 "2020-01-13T14:40:41Z")

</div>

Ok, thank you for your help.

---

<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:** [February 10, 2020, 2:40pm UTC](https://discuss.elastic.co/t/flatten-source-field-of-query-results/214841/4 "2020-02-10T14:40:41Z")

</div>

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