Skip to content

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 17 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include::{asciidoc-dir}/../../shared/attributes.asciidoc[]

:doc-tests-src: {docdir}/../tests/Tests/Documentation
:net-client: Elasticsearch .NET Client
:latest-version: 8.1.0
:latest-version: 8.15.8

:es-docs: https://www.elastic.co/guide/en/elasticsearch/reference/{branch}

Expand Down
131 changes: 131 additions & 0 deletions docs/usage/aggregations.asciidoc
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}");
}
----
3 changes: 3 additions & 0 deletions docs/usage/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ NOTE: This is still a work in progress, more sections will be added in the near

include::recommendations.asciidoc[]
include::examples.asciidoc[]
include::query.asciidoc[]
include::mappings.asciidoc[]
include::aggregations.asciidoc[]
include::esql.asciidoc[]
34 changes: 34 additions & 0 deletions docs/usage/mappings.asciidoc
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))
)
);
----
50 changes: 50 additions & 0 deletions docs/usage/query.asciidoc
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);
}
----
Loading