diff --git a/docs/index.asciidoc b/docs/index.asciidoc index c70fe7f73a..70d8bbadad 100644 --- a/docs/index.asciidoc +++ b/docs/index.asciidoc @@ -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 include::intro.asciidoc[] diff --git a/docs/usage/aggregations.asciidoc b/docs/usage/aggregations.asciidoc new file mode 100644 index 0000000000..1f159763aa --- /dev/null +++ b/docs/usage/aggregations.asciidoc @@ -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(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(new SearchRequest("persons") +{ + Query = Query.MatchAll(new MatchAllQuery()), + Aggregations = new Dictionary + { + { "agg_name", Aggregation.Max(new MaxAggregation + { + Field = Infer.Field(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(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(x => x.FirstName) +}); + +topLevelAggregation.Aggregations = new Dictionary +{ + { "avg_age", new MaxAggregation + { + Field = Infer.Field(x => x.Age) + }} +}; + +var response = await client.SearchAsync(new SearchRequest("persons") +{ + Query = Query.MatchAll(new MatchAllQuery()), + Aggregations = new Dictionary + { + { "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}"); +} +---- diff --git a/docs/usage/mappings.asciidoc b/docs/usage/mappings.asciidoc new file mode 100644 index 0000000000..13d62f6314 --- /dev/null +++ b/docs/usage/mappings.asciidoc @@ -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(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(mappings => mappings + .Indices("index") + .Properties(properties => properties + .IntegerNumber(x => x.Age!) + .Keyword(x => x.FirstName!, keyword => keyword.Index(false)) + ) +); +---- diff --git a/docs/usage/query.asciidoc b/docs/usage/query.asciidoc new file mode 100644 index 0000000000..b365825cdb --- /dev/null +++ b/docs/usage/query.asciidoc @@ -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(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(new SearchRequest("persons") + { + Query = Query.Term(new TermQuery(Infer.Field(x => x.FirstName)) + { + Value = "Florian" + }), + Size = 10 + }); +---- + + +[discrete] +=== Consume the response + +[source,csharp] +---- +foreach (var person in response.Documents) +{ + Console.WriteLine(person.FirstName); +} +----