Skip to content

Commit fd2a8a8

Browse files
committed
Use client calls in automapping documentation
Closes #2960 (cherry picked from commit cbfd037)
1 parent 596c0e6 commit fd2a8a8

File tree

15 files changed

+580
-235
lines changed

15 files changed

+580
-235
lines changed

build/scripts/Documentation.fsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Documentation =
2020
ExecProcess (fun p ->
2121
p.WorkingDirectory <- Paths.Source("CodeGeneration") @@ docGenerator.Name
2222
p.FileName <- generator
23-
) (TimeSpan.FromMinutes 2.) |> ignore
23+
) (TimeSpan.FromMinutes 3.) |> ignore
2424

2525
// TODO: hook documentation validation into the process
2626
let Validate() =
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/5.6
2+
3+
:xpack_current: https://www.elastic.co/guide/en/x-pack/5.6
4+
5+
:github: https://github.com/elastic/elasticsearch-net
6+
7+
:nuget: https://www.nuget.org/packages
8+
9+
////
10+
IMPORTANT NOTE
11+
==============
12+
This file has been generated from https://github.com/elastic/elasticsearch-net/tree/5.x/src/Tests/Aggregations/AggregationMetaUsageTests.cs.
13+
If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file,
14+
please modify the original csharp file found at the link and submit the PR with that change. Thanks!
15+
////
16+
17+
[[aggregation-metadata]]
18+
=== Aggregation Metadata
19+
20+
Metadata can be provided per aggregation, and will be returned in the aggregation response
21+
22+
==== Fluent DSL example
23+
24+
[source,csharp]
25+
----
26+
s => s
27+
.Size(0)
28+
.Aggregations(a => a
29+
.Min("min_last_activity", m => m
30+
.Field(p => p.LastActivity)
31+
.Meta(d => d
32+
.Add("meta_1", "value_1")
33+
.Add("meta_2", 2)
34+
.Add("meta_3", new { meta_3 = "value_3" })
35+
)
36+
)
37+
)
38+
----
39+
40+
==== Object Initializer syntax example
41+
42+
[source,csharp]
43+
----
44+
new SearchRequest<Project>()
45+
{
46+
Size = 0,
47+
Aggregations = new MinAggregation("min_last_activity", Infer.Field<Project>(p => p.LastActivity))
48+
{
49+
Meta = new Dictionary<string,object>
50+
{
51+
{ "meta_1", "value_1" },
52+
{ "meta_2", 2 },
53+
{ "meta_3", new { meta_3 = "value_3" } }
54+
}
55+
}
56+
}
57+
----
58+
59+
[source,javascript]
60+
.Example json output
61+
----
62+
{
63+
"size": 0,
64+
"aggs": {
65+
"min_last_activity": {
66+
"min": {
67+
"field": "lastActivity"
68+
},
69+
"meta": {
70+
"meta_1": "value_1",
71+
"meta_2": 2,
72+
"meta_3": {
73+
"meta_3": "value_3"
74+
}
75+
}
76+
}
77+
}
78+
}
79+
----
80+
81+
==== Handling Responses
82+
83+
[source,csharp]
84+
----
85+
response.ShouldBeValid();
86+
var min = response.Aggs.Min("min_last_activity");
87+
min.Meta.Should().NotBeNull().And.ContainKeys("meta_1", "meta_2", "meta_3");
88+
----
89+

docs/client-concepts/high-level/mapping/attribute-mapping.asciidoc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ Then we map the types by calling `.AutoMap()`
7676

7777
[source,csharp]
7878
----
79-
var descriptor = new CreateIndexDescriptor("myindex")
79+
var createIndexResponse = client.CreateIndex("myindex", c => c
8080
.Mappings(ms => ms
8181
.Map<Company>(m => m.AutoMap())
8282
.Map<Employee>(m => m.AutoMap())
83-
);
83+
)
84+
);
8485
----
8586

8687
[source,javascript]

docs/client-concepts/high-level/mapping/auto-map.asciidoc

Lines changed: 129 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,19 @@ on the POCO
5151

5252
[source,csharp]
5353
----
54-
var descriptor = new CreateIndexDescriptor("myindex")
54+
var createIndexResponse = client.CreateIndex("myindex", c => c
5555
.Mappings(ms => ms
5656
.Map<Company>(m => m.AutoMap()) <1>
5757
.Map<Employee>(m => m.AutoMap()) <2>
58-
);
58+
)
59+
);
5960
----
6061
<1> Auto map `Company`
6162

6263
<2> Auto map `Employee`
6364

65+
This produces the following JSON request
66+
6467
[source,javascript]
6568
----
6669
{
@@ -159,6 +162,51 @@ var descriptor = new CreateIndexDescriptor("myindex")
159162
}
160163
----
161164

165+
[source,csharp]
166+
----
167+
var connectionSettings = new ConnectionSettings(new InMemoryConnection()) <1>
168+
.DisableDirectStreaming() <2>
169+
.InferMappingFor<ParentWithStringId>(m => m
170+
.TypeName("parent")
171+
.Ignore(p => p.Description)
172+
.Ignore(p => p.IgnoreMe)
173+
);
174+
175+
var client = new ElasticClient(connectionSettings);
176+
177+
var createIndexResponse = client.CreateIndex("myindex", c => c
178+
.Mappings(ms => ms
179+
.Map<ParentWithStringId>(m => m
180+
.AutoMap()
181+
)
182+
)
183+
);
184+
----
185+
<1> we're using an _in memory_ connection for this example. In your production application though, you'll want to use an `IConnection` that actually sends a request.
186+
187+
<2> we disable direct streaming here to capture the request and response bytes. In your production application however, you'll likely not want to do this, since it causes the request and response bytes to be buffered in memory.
188+
189+
[source,javascript]
190+
----
191+
{
192+
"mappings": {
193+
"parent": {
194+
"properties": {
195+
"id": {
196+
"type": "text",
197+
"fields": {
198+
"keyword": {
199+
"ignore_above": 256,
200+
"type": "keyword"
201+
}
202+
}
203+
}
204+
}
205+
}
206+
}
207+
}
208+
----
209+
162210
Observe that NEST has inferred the Elasticsearch types based on the CLR type of our POCO properties.
163211
In this example,
164212

@@ -177,57 +225,105 @@ sub field.
177225

178226
NEST has inferred mapping support for the following .NET types
179227

180-
* `String` maps to `"text"` with a `"keyword"` sub field. See <<multi-fields, Multi Fields>>.
228+
[horizontal]
229+
`String`::
230+
231+
maps to `"text"` with a `"keyword"` sub field. See <<multi-fields, Multi Fields>>.
232+
233+
`Int32`::
234+
235+
maps to `"integer"`
236+
237+
`UInt16`::
238+
239+
maps to `"integer"`
240+
241+
`Int16`::
242+
243+
maps to `"short"`
244+
245+
`Byte`::
246+
247+
maps to `"short"`
248+
249+
`Int64`::
250+
251+
maps to `"long"`
252+
253+
`UInt32`::
181254

182-
* `Int32` maps to `"integer"`
255+
maps to `"long"`
183256

184-
* `UInt16` maps to `"integer"`
257+
`TimeSpan`::
185258

186-
* `Int16` maps to `"short"`
259+
maps to `"long"`
187260

188-
* `Byte` maps to `"short"`
261+
`Single`::
189262

190-
* `Int64` maps to `"long"`
263+
maps to `"float"`
191264

192-
* `UInt32` maps to `"long"`
265+
`Double`::
193266

194-
* `TimeSpan` maps to `"long"`
267+
maps to `"double"`
195268

196-
* `Single` maps to `"float"`
269+
`Decimal`::
197270

198-
* `Double` maps to `"double"`
271+
maps to `"double"`
199272

200-
* `Decimal` maps to `"double"`
273+
`UInt64`::
201274

202-
* `UInt64` maps to `"double"`
275+
maps to `"double"`
203276

204-
* `DateTime` maps to `"date"`
277+
`DateTime`::
205278

206-
* `DateTimeOffset` maps to `"date"`
279+
maps to `"date"`
207280

208-
* `Boolean` maps to `"boolean"`
281+
`DateTimeOffset`::
209282

210-
* `Char` maps to `"keyword"`
283+
maps to `"date"`
211284

212-
* `Guid` maps to `"keyword"`
285+
`Boolean`::
286+
287+
maps to `"boolean"`
288+
289+
`Char`::
290+
291+
maps to `"keyword"`
292+
293+
`Guid`::
294+
295+
maps to `"keyword"`
213296

214297
and supports a number of special types defined in NEST
215298

216-
* `Nest.GeoLocation` maps to `"geo_point"`
299+
[horizontal]
300+
`Nest.GeoLocation`::
301+
302+
maps to `"geo_point"`
303+
304+
`Nest.CompletionField`::
217305

218-
* `Nest.CompletionField` maps to `"completion"`
306+
maps to `"completion"`
219307

220-
* `Nest.Attachment` maps to `"attachment"`
308+
`Nest.DateRange`::
221309

222-
* `Nest.DateRange` maps to `"date_range"`
310+
maps to `"date_range"`
223311

224-
* `Nest.DoubleRange` maps to `"double_range"`
312+
`Nest.DoubleRange`::
225313

226-
* `Nest.FloatRange` maps to `"float_range"`
314+
maps to `"double_range"`
227315

228-
* `Nest.IntegerRange` maps to `"integer_range"`
316+
`Nest.FloatRange`::
229317

230-
* `Nest.LongRange` maps to `"long_range"`
318+
maps to `"float_range"`
319+
320+
`Nest.IntegerRange`::
321+
322+
maps to `"integer_range"`
323+
324+
`Nest.LongRange`::
325+
326+
maps to `"long_range"`
231327

232328
All other types map to `"object"` by default.
233329

@@ -282,10 +378,11 @@ By default, `.AutoMap()` only goes as far as depth 1
282378

283379
[source,csharp]
284380
----
285-
var descriptor = new CreateIndexDescriptor("myindex")
381+
var createIndexResponse = client.CreateIndex("myindex", c => c
286382
.Mappings(ms => ms
287383
.Map<A>(m => m.AutoMap())
288-
);
384+
)
385+
);
289386
----
290387

291388
Thus we do not map properties on the second occurrence of our Child property
@@ -310,10 +407,11 @@ Now let's specify a maxRecursion of `3`
310407

311408
[source,csharp]
312409
----
313-
var withMaxRecursionDescriptor = new CreateIndexDescriptor("myindex")
410+
createIndexResponse = client.CreateIndex("myindex", c => c
314411
.Mappings(ms => ms
315412
.Map<A>(m => m.AutoMap(3))
316-
);
413+
)
414+
);
317415
----
318416

319417
`.AutoMap()` has now mapped three levels of our Child property

0 commit comments

Comments
 (0)