- .NET Clients: other versions:
- Introduction
- Breaking changes
- API Conventions
- Elasticsearch.Net - Low level client
- NEST - High level client
- Troubleshooting
- Search
- Query DSL
- Full text queries
- Term level queries
- Exists Query Usage
- Fuzzy Date Query Usage
- Fuzzy Numeric Query Usage
- Fuzzy Query Usage
- Ids Query Usage
- Prefix Query Usage
- Date Range Query Usage
- Long Range Query Usage
- Numeric Range Query Usage
- Term Range Query Usage
- Regexp Query Usage
- Term Query Usage
- Terms List Query Usage
- Terms Lookup Query Usage
- Terms Query Usage
- Terms Set Query Usage
- Type Query Usage
- Wildcard Query Usage
- Compound queries
- Joining queries
- Geo queries
- Geo Bounding Box Query Usage
- Geo Distance Query Usage
- Geo Polygon Query Usage
- Geo Shape Circle Query Usage
- Geo Shape Envelope Query Usage
- Geo Shape Geometry Collection Query Usage
- Geo Shape Indexed Shape Query Usage
- Geo Shape Line String Query Usage
- Geo Shape Multi Line String Query Usage
- Geo Shape Multi Point Query Usage
- Geo Shape Multi Polygon Query Usage
- Geo Shape Point Query Usage
- Geo Shape Polygon Query Usage
- Specialized queries
- Span queries
- NEST specific queries
- Aggregations
- Metric Aggregations
- Average Aggregation Usage
- Cardinality Aggregation Usage
- Extended Stats Aggregation Usage
- Geo Bounds Aggregation Usage
- Geo Centroid Aggregation Usage
- Max Aggregation Usage
- Median Absolute Deviation Aggregation Usage
- Min Aggregation Usage
- Percentile Ranks Aggregation Usage
- Percentiles Aggregation Usage
- Scripted Metric Aggregation Usage
- Stats Aggregation Usage
- Sum Aggregation Usage
- Top Hits Aggregation Usage
- Value Count Aggregation Usage
- Weighted Average Aggregation Usage
- Bucket Aggregations
- Adjacency Matrix Usage
- Auto Date Histogram Aggregation Usage
- Children Aggregation Usage
- Composite Aggregation Usage
- Date Histogram Aggregation Usage
- Date Range Aggregation Usage
- Filter Aggregation Usage
- Filters Aggregation Usage
- Geo Distance Aggregation Usage
- Geo Hash Grid Aggregation Usage
- Global Aggregation Usage
- Histogram Aggregation Usage
- Ip Range Aggregation Usage
- Missing Aggregation Usage
- Nested Aggregation Usage
- Parent Aggregation Usage
- Range Aggregation Usage
- Reverse Nested Aggregation Usage
- Sampler Aggregation Usage
- Significant Terms Aggregation Usage
- Significant Text Aggregation Usage
- Terms Aggregation Usage
- Pipeline Aggregations
- Average Bucket Aggregation Usage
- Bucket Script Aggregation Usage
- Bucket Selector Aggregation Usage
- Bucket Sort Aggregation Usage
- Cumulative Sum Aggregation Usage
- Derivative Aggregation Usage
- Extended Stats Bucket Aggregation Usage
- Max Bucket Aggregation Usage
- Min Bucket Aggregation Usage
- Moving Average Ewma Aggregation Usage
- Moving Average Holt Linear Aggregation Usage
- Moving Average Holt Winters Aggregation Usage
- Moving Average Linear Aggregation Usage
- Moving Average Simple Aggregation Usage
- Moving Function Aggregation Usage
- Percentiles Bucket Aggregation Usage
- Serial Differencing Aggregation Usage
- Stats Bucket Aggregation Usage
- Sum Bucket Aggregation Usage
- Matrix Aggregations
- Metric Aggregations
NOTE: You are looking at documentation for an older release. For the latest information, see the current release documentation.
Inner Hits Usage
editInner Hits Usage
editThe parent/child and nested features allow the return of documents that have matches in a different scope. In the parent/child case, parent document are returned based on matches in child documents or child document are returned based on matches in parent documents. In the nested case, documents are returned based on matches in nested inner objects.
In both cases, the actual matches in the different scopes that caused a document to be returned is hidden. In many cases, it’s very useful to know which inner nested objects (in the case of nested) or children/parent documents (in the case of parent/child) caused certain information to be returned. The inner hits feature can be used for this. This feature returns per search hit in the search response additional nested hits that caused a search hit to match in a different scope.
Inner hits can be used by defining an inner_hits
definition on a nested
, has_child
or has_parent
query and filter.
See the Elasticsearch documentation on Inner hits for more detail.
Query Inner Hits
editFluent DSL example
edits => s .Index(Index) .Type(RoyalSeeder.RoyalType) .Query(q => q.HasChild<Prince>(hc => hc .Query(hcq => hcq.Match(m => m.Field(p => p.FullTextField).Query("default"))) .InnerHits(ih => ih .DocValueFields(f => f.Field(p => p.Name)) .Name("princes") .Highlight(h => h.Fields(f => f.Field(p => p.FullTextField))) .IgnoreUnmapped(false) .Version() ) ) || q.Nested(n => n .Path(p => p.Foes) .Query(nq => nq.MatchAll()) .InnerHits(i => i.Version()) ) ) .Version()
Object Initializer syntax example
editnew SearchRequest<King>(Index, RoyalSeeder.RoyalType) { Query = new HasChildQuery { TypeRelation = typeof(Prince), Query = new MatchQuery { Field = Field<Prince>(p => p.FullTextField), Query = "default" }, InnerHits = new InnerHits { Name = "princes", DocValueFields = Field<Prince>(p => p.Name), Highlight = Highlight.Field(Field<Prince>(p => p.FullTextField)), IgnoreUnmapped = false, Version = true } } || new NestedQuery { Path = Field<King>(p => p.Foes), Query = new MatchAllQuery(), InnerHits = new InnerHits() { Version = true } }, Version = true }
Example json output.
{ "query": { "bool": { "should": [ { "has_child": { "type": "prince", "query": { "match": { "fullTextField": { "query": "default" } } }, "inner_hits": { "name": "princes", "docvalue_fields": [ "name" ], "highlight": { "fields": { "fullTextField": {} } }, "ignore_unmapped": false, "version": true } } }, { "nested": { "query": { "match_all": {} }, "path": "foes", "inner_hits": { "version": true } } } ] } }, "version": true }
Handling Responses
editresponse.Hits.Should().NotBeEmpty(); foreach (var hit in response.Hits) { hit.Id.Should().NotBeNullOrEmpty(); hit.Index.Should().NotBeNullOrEmpty(); hit.Version?.Should().Be(1); var princes = hit.InnerHits["princes"].Documents<Prince>(); princes.Should().NotBeEmpty(); foreach (var princeHit in hit.InnerHits["princes"].Hits.Hits) { var highlights = princeHit.Highlights; highlights.Should().NotBeNull("princes should have highlights"); highlights.Should().ContainKey("fullTextField", "we are highlighting this field"); var hl = highlights["fullTextField"]; hl.Highlights.Should() .NotBeEmpty("all docs have the same text so should all highlight") .And.Contain(s => s.Contains("<em>default</em>"), "default to be highlighted as its part of the query"); princeHit.Fields.Should().NotBeNull("all princes have a keyword name so fields should be returned"); var docValueName = princeHit.Fields.ValueOf<Prince, string>(p => p.Name); docValueName.Should().NotBeNullOrWhiteSpace("value of name on Fields"); princeHit.Version?.Should().Be(1); } var foes = hit.InnerHits["foes"].Documents<King>(); foes.Should().NotBeEmpty(); }