Nested Query Usage

edit

Nested query allows to query nested objects / docs (see nested mapping). The query is executed against the nested objects / docs as if they were indexed as separate docs (they are, internally) and resulting in the root parent doc (or parent nested mapping).

See the Elasticsearch documentation on nested query for more details.

Fluent DSL example

edit
q
.Nested(c => c
    .Name("named_query")
    .Boost(1.1)
    .InnerHits(i => i.Explain())
    .Path(p => p.CuratedTags)
    .Query(nq => nq
        .Terms(t => t
            .Field(f => f.CuratedTags.First().Name)
            .Terms("lorem", "ipsum")
        )
    )
    .IgnoreUnmapped()
)

Object Initializer syntax example

edit
new NestedQuery
{
    Name = "named_query",
    Boost = 1.1,
    InnerHits = new InnerHits { Explain = true },
    Path = Field<Project>(p => p.CuratedTags),
    Query = new TermsQuery
    {
        Field = Field<Project>(p => p.CuratedTags.First().Name),
        Terms = new[] { "lorem", "ipsum" }
    },
    IgnoreUnmapped = false
}

Example json output.

{
  "nested": {
    "_name": "named_query",
    "boost": 1.1,
    "query": {
      "terms": {
        "curatedTags.name": [
          "lorem",
          "ipsum"
        ]
      }
    },
    "ignore_unmapped": false,
    "path": "curatedTags",
    "inner_hits": {
      "explain": true
    }
  }
}