-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more examples to the documentation (#8374)
- Loading branch information
Showing
4 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
[[aggregations]] | ||
== Aggregation examples | ||
|
||
This page demonstrates how to use aggregations. | ||
|
||
[discrete] | ||
=== Top-level aggreggation | ||
|
||
[discrete] | ||
==== Fluent API | ||
|
||
[source,csharp] | ||
---- | ||
var response = await client | ||
.SearchAsync<Person>(search => search | ||
.Index("persons") | ||
.Query(query => query | ||
.MatchAll(_ => {}) | ||
) | ||
.Aggregations(aggregations => aggregations | ||
.Add("agg_name", aggregation => aggregation | ||
.Max(max => max | ||
.Field(x => x.Age) | ||
) | ||
) | ||
) | ||
.Size(10) | ||
); | ||
---- | ||
|
||
[discrete] | ||
==== Object initializer API | ||
|
||
[source,csharp] | ||
---- | ||
var response = await client.SearchAsync<Person>(new SearchRequest("persons") | ||
{ | ||
Query = Query.MatchAll(new MatchAllQuery()), | ||
Aggregations = new Dictionary<string, Aggregation> | ||
{ | ||
{ "agg_name", Aggregation.Max(new MaxAggregation | ||
{ | ||
Field = Infer.Field<Person>(x => x.Age) | ||
})} | ||
}, | ||
Size = 10 | ||
}); | ||
---- | ||
|
||
[discrete] | ||
==== Consume the response | ||
|
||
[source,csharp] | ||
---- | ||
var max = response.Aggregations!.GetMax("agg_name")!; | ||
Console.WriteLine(max.Value); | ||
---- | ||
|
||
[discrete] | ||
=== Sub-aggregation | ||
|
||
[discrete] | ||
==== Fluent API | ||
|
||
[source,csharp] | ||
---- | ||
var response = await client | ||
.SearchAsync<Person>(search => search | ||
.Index("persons") | ||
.Query(query => query | ||
.MatchAll(_ => {}) | ||
) | ||
.Aggregations(aggregations => aggregations | ||
.Add("firstnames", aggregation => aggregation | ||
.Terms(terms => terms | ||
.Field(x => x.FirstName) | ||
) | ||
.Aggregations(aggregations => aggregations | ||
.Add("avg_age", aggregation => aggregation | ||
.Max(avg => avg | ||
.Field(x => x.Age) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
.Size(10) | ||
); | ||
---- | ||
|
||
[discrete] | ||
==== Object initializer API | ||
|
||
[source,csharp] | ||
---- | ||
var topLevelAggregation = Aggregation.Terms(new TermsAggregation | ||
{ | ||
Field = Infer.Field<Person>(x => x.FirstName) | ||
}); | ||
topLevelAggregation.Aggregations = new Dictionary<string, Aggregation> | ||
{ | ||
{ "avg_age", new MaxAggregation | ||
{ | ||
Field = Infer.Field<Person>(x => x.Age) | ||
}} | ||
}; | ||
var response = await client.SearchAsync<Person>(new SearchRequest("persons") | ||
{ | ||
Query = Query.MatchAll(new MatchAllQuery()), | ||
Aggregations = new Dictionary<string, Aggregation> | ||
{ | ||
{ "firstnames", topLevelAggregation} | ||
}, | ||
Size = 10 | ||
}); | ||
---- | ||
|
||
[discrete] | ||
==== Consume the response | ||
|
||
[source,csharp] | ||
---- | ||
var firstnames = response.Aggregations!.GetStringTerms("firstnames")!; | ||
foreach (var bucket in firstnames.Buckets) | ||
{ | ||
var avg = bucket.Aggregations.GetAverage("avg_age")!; | ||
Console.WriteLine($"The average age for persons named '{bucket.Key}' is {avg}"); | ||
} | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[[mappings]] | ||
== Custom mapping examples | ||
|
||
This page demonstrates how to configure custom mappings on an index. | ||
|
||
[discrete] | ||
=== Configure mappings during index creation | ||
|
||
[source,csharp] | ||
---- | ||
await client.Indices.CreateAsync<Person>(index => index | ||
.Index("index") | ||
.Mappings(mappings => mappings | ||
.Properties(properties => properties | ||
.IntegerNumber(x => x.Age!) | ||
.Keyword(x => x.FirstName!, keyword => keyword.Index(false)) | ||
) | ||
) | ||
); | ||
---- | ||
|
||
[discrete] | ||
=== Configure mappings after index creation | ||
|
||
[source,csharp] | ||
---- | ||
await client.Indices.PutMappingAsync<Person>(mappings => mappings | ||
.Indices("index") | ||
.Properties(properties => properties | ||
.IntegerNumber(x => x.Age!) | ||
.Keyword(x => x.FirstName!, keyword => keyword.Index(false)) | ||
) | ||
); | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
[[query]] | ||
== Query examples | ||
|
||
This page demonstrates how to perform a search request. | ||
|
||
[discrete] | ||
=== Fluent API | ||
|
||
[source,csharp] | ||
---- | ||
var response = await client | ||
.SearchAsync<Person>(search => search | ||
.Index("persons") | ||
.Query(query => query | ||
.Term(term => term | ||
.Field(x => x.FirstName) | ||
.Value("Florian") | ||
) | ||
) | ||
.Size(10) | ||
); | ||
---- | ||
|
||
[discrete] | ||
=== Object initializer API | ||
|
||
[source,csharp] | ||
---- | ||
var response = await client | ||
.SearchAsync<Person>(new SearchRequest<Person>("persons") | ||
{ | ||
Query = Query.Term(new TermQuery(Infer.Field<Person>(x => x.FirstName)) | ||
{ | ||
Value = "Florian" | ||
}), | ||
Size = 10 | ||
}); | ||
---- | ||
|
||
|
||
[discrete] | ||
=== Consume the response | ||
|
||
[source,csharp] | ||
---- | ||
foreach (var person in response.Documents) | ||
{ | ||
Console.WriteLine(person.FirstName); | ||
} | ||
---- |