-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add more examples to the documentation #8374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1d0683a
Add more examples to the documentation
flobernd 392be39
Update index.asciidoc
marciw caa1b86
Update docs/usage/aggregations.asciidoc
flobernd 8822fce
Update docs/usage/aggregations.asciidoc
flobernd b87cb98
Update docs/usage/aggregations.asciidoc
flobernd 70c3903
Update docs/usage/aggregations.asciidoc
flobernd 86901a2
Update docs/usage/mappings.asciidoc
flobernd ea6a7ca
Update docs/usage/query.asciidoc
flobernd ca6d8b7
Update docs/usage/mappings.asciidoc
flobernd 87502b9
Update docs/usage/query.asciidoc
flobernd 998adec
Update docs/usage/aggregations.asciidoc
flobernd 0fd895c
Update docs/usage/mappings.asciidoc
flobernd bf72330
Update docs/usage/query.asciidoc
flobernd 34e07e1
Add more object initializer API examples
flobernd 3bf7861
Fix double heading
marciw d736bc3
Fix double heading
marciw 1271ba1
Fix double heading 2
marciw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,133 @@ | ||
[[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 | ||
==== 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] | ||
==== Consuming the Response | ||
flobernd marked this conversation as resolved.
Show resolved
Hide resolved
marciw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
==== 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}"); | ||
} | ||
---- |
flobernd marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 hidden or 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 hidden or 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); | ||
} | ||
---- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.