Skip to content

Commit 1d0683a

Browse files
committed
Add more examples to the documentation
1 parent b077a88 commit 1d0683a

File tree

4 files changed

+168
-1
lines changed

4 files changed

+168
-1
lines changed

docs/index.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ include::{asciidoc-dir}/../../shared/attributes.asciidoc[]
66

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

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

docs/usage/aggregations.asciidoc

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[[aggregations]]
2+
== Aggregation Examples
3+
4+
This page demonstrates how to use aggregations.
5+
6+
[discrete]
7+
=== Top Level Aggreggation
8+
9+
[discrete]
10+
==== Fluent API
11+
12+
[source,csharp]
13+
----
14+
var response = await client
15+
.SearchAsync<Person>(search => search
16+
.Index("persons")
17+
.Query(query => query
18+
.MatchAll(_ => {})
19+
)
20+
.Aggregations(aggregations => aggregations
21+
.Add("agg_name", aggregation => aggregation
22+
.Max(max => max
23+
.Field(x => x.Age)
24+
)
25+
)
26+
)
27+
.Size(10)
28+
);
29+
----
30+
31+
[discrete]
32+
==== Consuming the Response
33+
34+
[source,csharp]
35+
----
36+
var max = response.Aggregations!.GetMax("agg_name")!;
37+
Console.WriteLine(max.Value);
38+
----
39+
40+
[discrete]
41+
=== Sub Aggregation
42+
43+
[discrete]
44+
==== Fluent API
45+
46+
[source,csharp]
47+
----
48+
var response = await client
49+
.SearchAsync<Person>(search => search
50+
.Index("persons")
51+
.Query(query => query
52+
.MatchAll(_ => {})
53+
)
54+
.Aggregations(aggregations => aggregations
55+
.Add("firstnames", aggregation => aggregation
56+
.Terms(terms => terms
57+
.Field(x => x.FirstName)
58+
)
59+
.Aggregations(aggregations => aggregations
60+
.Add("avg_age", aggregation => aggregation
61+
.Max(avg => avg
62+
.Field(x => x.Age)
63+
)
64+
)
65+
)
66+
)
67+
)
68+
.Size(10)
69+
);
70+
----
71+
72+
[discrete]
73+
==== Consuming the Response
74+
75+
[source,csharp]
76+
----
77+
var firstnames = response.Aggregations!.GetStringTerms("firstnames")!;
78+
foreach (var bucket in firstnames.Buckets)
79+
{
80+
var avg = bucket.Aggregations.GetAverage("avg_age")!;
81+
Console.WriteLine($"The average age for persons named '{bucket.Key}' is {avg}");
82+
}
83+
----

docs/usage/mappings.asciidoc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[[mappings]]
2+
== Custom Mappings Examples
3+
4+
This page demonstrates how to configure custom mappings on an index.
5+
6+
[discrete]
7+
=== Configure Mappings during Index Creation
8+
9+
[source,csharp]
10+
----
11+
await client.Indices.CreateAsync<Person>(index => index
12+
.Index("index")
13+
.Mappings(mappings => mappings
14+
.Properties(properties => properties
15+
.IntegerNumber(x => x.Age!)
16+
.Keyword(x => x.FirstName!, keyword => keyword.Index(false))
17+
)
18+
)
19+
);
20+
----
21+
22+
[discrete]
23+
=== Configure Mappings after Index Creation
24+
25+
[source,csharp]
26+
----
27+
await client.Indices.PutMappingAsync<Person>(mappings => mappings
28+
.Indices("index")
29+
.Properties(properties => properties
30+
.IntegerNumber(x => x.Age!)
31+
.Keyword(x => x.FirstName!, keyword => keyword.Index(false))
32+
)
33+
);
34+
----

docs/usage/query.asciidoc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[[query]]
2+
== Query Examples
3+
4+
This page demonstrates how to perform a search request.
5+
6+
[discrete]
7+
=== Fluent API
8+
9+
[source,csharp]
10+
----
11+
var response = await client
12+
.SearchAsync<Person>(search => search
13+
.Index("persons")
14+
.Query(query => query
15+
.Term(term => term
16+
.Field(x => x.FirstName)
17+
.Value("Florian")
18+
)
19+
)
20+
.Size(10)
21+
);
22+
----
23+
24+
[discrete]
25+
=== Object Initializer API
26+
27+
[source,csharp]
28+
----
29+
var response = await client
30+
.SearchAsync<Person>(new SearchRequest<Person>("persons")
31+
{
32+
Query = Query.Term(new TermQuery("firstName"!)
33+
{
34+
Value = "Florian"
35+
}),
36+
Size = 10
37+
});
38+
----
39+
40+
41+
[discrete]
42+
=== Consuming the Response
43+
44+
[source,csharp]
45+
----
46+
foreach (var person in response.Documents)
47+
{
48+
Console.WriteLine(person.FirstName);
49+
}
50+
----

0 commit comments

Comments
 (0)