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.Tags)
    .Query(nq => nq
        .Terms(t => t
            .Field(f => f.Tags.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.Tags),
    Query = new TermsQuery
    {
        Field = Field<Project>(p => p.Tags.First().Name),
        Terms = new[] { "lorem", "ipsum" }
    },
    IgnoreUnmapped = true
}

Example json output.

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