# Filter children in elasticsearch (using NEST)

**URL:** https://discuss.elastic.co/t/filter-children-in-elasticsearch-using-nest/119442
**Category:** Elasticsearch
**Created:** [February 12, 2018, 9:51am UTC](https://discuss.elastic.co/t/filter-children-in-elasticsearch-using-nest/119442 "2018-02-12T09:51:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Qutaiba\_Mustafa](https://avatars.discourse-cdn.com/v4/letter/q/7993a0/32.png) [@Qutaiba\_Mustafa](https://discuss.elastic.co/u/Qutaiba_Mustafa)
#### Post date: [February 12, 2018, 9:51am UTC](https://discuss.elastic.co/t/filter-children-in-elasticsearch-using-nest/119442/1 "2018-02-12T09:51:50Z")

</div>

I want to filter children in elastic search without changing the parent node, i have something like this data, and want to all games, but only "isVisible = true" members should appear with each game

```
   { 
"_index": "test", 
"_type": "Game", 
"_id": "1", 
"_source": { 
"gameName" : "test game",
"teamMembers" : {
    "id" : "2",
    "name":"John",
    "isVisible" : true
    },
    {
    "id" : "3",
    "name":"emma",
    "isVisible": false
    }
  }
}

```

So, i want to get the above game as is but with only 1 team member (John) i am using C#, and tried this:

```
var result = new List<Func<QueryContainerDescriptor<Review>, QueryContainer>>();
result.Add(a => a.Nested(n => n.Path("teamMembers").Query(q => q.Match(m => m.Field("teamMembers.isVisible").Query("true")))));

```

but it didn't work.

Note: i am using Elastic search 5.

Thanks in advanced.

---

<div class="post-metadata">

### Author: ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)
#### Post date: [February 13, 2018, 12:31am UTC](https://discuss.elastic.co/t/filter-children-in-elasticsearch-using-nest/119442/2 "2018-02-13T00:31:09Z")

</div>

> [@Qutaiba\_Mustafa](#):
>
> but it didn't work.

How didn't it work?

I think you might be misunderstanding how queries on `nested` types work.

Assuming that `"teamMembers"` is an array of objects mapped as a `nested` type, the query that you're performing _will_ return `Game` documents that contain an object in the `"teamMembers"` array whose `"isVisible"` property is set to `"true"` (I'd pass the boolean `true` rather than a string here).

**It won't return _only_ the teamMembers who are visible, on the `Game` document, it will return the entire \_source document.** There are a couple of ways that you might collect only the teamMembers who are visible

- Filter them out in application code, from the response, using LINQ `.Where()` or similar
- Use [Inner Hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html) as part of the query.

---

<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: [March 13, 2018, 12:31am UTC](https://discuss.elastic.co/t/filter-children-in-elasticsearch-using-nest/119442/3 "2018-03-13T00:31:22Z")

</div>

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