Skip to content

Commit 32b828c

Browse files
committed
Add more examples to the documentation (#8374)
1 parent c9cb337 commit 32b828c

File tree

4 files changed

+216
-1
lines changed

4 files changed

+216
-1
lines changed

docs/index.asciidoc

+1-1
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

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
==== Object initializer API
33+
34+
[source,csharp]
35+
----
36+
var response = await client.SearchAsync<Person>(new SearchRequest("persons")
37+
{
38+
Query = Query.MatchAll(new MatchAllQuery()),
39+
Aggregations = new Dictionary<string, Aggregation>
40+
{
41+
{ "agg_name", Aggregation.Max(new MaxAggregation
42+
{
43+
Field = Infer.Field<Person>(x => x.Age)
44+
})}
45+
},
46+
Size = 10
47+
});
48+
----
49+
50+
[discrete]
51+
==== Consume the response
52+
53+
[source,csharp]
54+
----
55+
var max = response.Aggregations!.GetMax("agg_name")!;
56+
Console.WriteLine(max.Value);
57+
----
58+
59+
[discrete]
60+
=== Sub-aggregation
61+
62+
[discrete]
63+
==== Fluent API
64+
65+
[source,csharp]
66+
----
67+
var response = await client
68+
.SearchAsync<Person>(search => search
69+
.Index("persons")
70+
.Query(query => query
71+
.MatchAll(_ => {})
72+
)
73+
.Aggregations(aggregations => aggregations
74+
.Add("firstnames", aggregation => aggregation
75+
.Terms(terms => terms
76+
.Field(x => x.FirstName)
77+
)
78+
.Aggregations(aggregations => aggregations
79+
.Add("avg_age", aggregation => aggregation
80+
.Max(avg => avg
81+
.Field(x => x.Age)
82+
)
83+
)
84+
)
85+
)
86+
)
87+
.Size(10)
88+
);
89+
----
90+
91+
[discrete]
92+
==== Object initializer API
93+
94+
[source,csharp]
95+
----
96+
var topLevelAggregation = Aggregation.Terms(new TermsAggregation
97+
{
98+
Field = Infer.Field<Person>(x => x.FirstName)
99+
});
100+
101+
topLevelAggregation.Aggregations = new Dictionary<string, Aggregation>
102+
{
103+
{ "avg_age", new MaxAggregation
104+
{
105+
Field = Infer.Field<Person>(x => x.Age)
106+
}}
107+
};
108+
109+
var response = await client.SearchAsync<Person>(new SearchRequest("persons")
110+
{
111+
Query = Query.MatchAll(new MatchAllQuery()),
112+
Aggregations = new Dictionary<string, Aggregation>
113+
{
114+
{ "firstnames", topLevelAggregation}
115+
},
116+
Size = 10
117+
});
118+
----
119+
120+
[discrete]
121+
==== Consume the response
122+
123+
[source,csharp]
124+
----
125+
var firstnames = response.Aggregations!.GetStringTerms("firstnames")!;
126+
foreach (var bucket in firstnames.Buckets)
127+
{
128+
var avg = bucket.Aggregations.GetAverage("avg_age")!;
129+
Console.WriteLine($"The average age for persons named '{bucket.Key}' is {avg}");
130+
}
131+
----

docs/usage/mappings.asciidoc

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[[mappings]]
2+
== Custom mapping 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

+50
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(Infer.Field<Person>(x => x.FirstName))
33+
{
34+
Value = "Florian"
35+
}),
36+
Size = 10
37+
});
38+
----
39+
40+
41+
[discrete]
42+
=== Consume 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)