Filter children in elasticsearch (using NEST)

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.

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 as part of the query.

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