Skip to content

Commit 4d67c7d

Browse files
Fix CRUD documentation. (#7302) (#7303)
* Switch CRUD examples to included test code * Remove examples folder * Fixes * Tweak tags * Tweak formatting * More formatting * Finalise Co-authored-by: Steve Gordon <[email protected]>
1 parent d6ccf89 commit 4d67c7d

File tree

410 files changed

+274
-11211
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

410 files changed

+274
-11211
lines changed

docs/client-concepts/serialization/custom-serialization.asciidoc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,9 @@ We want to serialize our source document using Pascal Casing for the JSON proper
6363
[source,csharp]
6464
----
6565
include::{doc-tests-src}/ClientConcepts/Serialization/CustomSerializationTests.cs[tag=usings]
66-
private async Task SerializeWithCustomOptionsAsync()
67-
{
6866
include::{doc-tests-src}/ClientConcepts/Serialization/CustomSerializationTests.cs[tag=custom-options-local-function]
6967
include::{doc-tests-src}/ClientConcepts/Serialization/CustomSerializationTests.cs[tag=create-client]
7068
include::{doc-tests-src}/ClientConcepts/Serialization/CustomSerializationTests.cs[tag=index-person]
71-
}
7269
----
7370
<1> A local function can be defined, accepting a `JsonSerializerOptions` parameter. Here, we set `PropertyNamingPolicy` to `null`. This returns to the default behavior for `System.Text.Json`, which uses Pascal Case.
7471
<2> When creating the `ElasticsearchClientSettings`, we supply a `SourceSerializerFactory` using a lambda. The factory function creates a new instance of `DefaultSourceSerializer`, passing in the `settings` and our `ConfigureOptions` local function. We have now configured the settings with a custom instance of the source serializer.

docs/usage/examples.asciidoc

Lines changed: 14 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[[examples]]
2-
=== CRUD usage examples
2+
== CRUD usage examples
33

44
This page helps you to understand how to perform various basic {es} CRUD
55
(create, read, update, delete) operations using the .NET client. It demonstrates
@@ -13,11 +13,7 @@ your C# file.
1313

1414
[source,csharp]
1515
----
16-
using System;
17-
using Elastic.Clients.Elasticsearch;
18-
using Elastic.Clients.Elasticsearch.QueryDsl;
19-
20-
var client = new ElasticsearchClient(); <1>
16+
include::{doc-tests-src}/Usage/CrudExamplesTests.cs[tags=using-directives;create-client]
2117
----
2218
<1> The default constructor, assumes an unsecured {es} server is running and
2319
exposed on 'http://localhost:9200'. See <<connecting, connecting>> for examples
@@ -30,13 +26,7 @@ that map to the document structure being stored in {es}.
3026

3127
[source,csharp]
3228
----
33-
public class Tweet
34-
{
35-
public int Id { get; set; } <1>
36-
public string User { get; set; }
37-
public DateTime PostDate { get; set; }
38-
public string Message { get; set; }
39-
}
29+
include::{doc-tests-src}/Usage/CrudExamplesTests.cs[tag=tweet-class]
4030
----
4131
<1> By default, the .NET client will try to find a property called `Id` on the
4232
class. When such a property is present it will index the document into {es}
@@ -45,29 +35,15 @@ using the ID specified by the value of this property.
4535

4636
[discrete]
4737
[[indexing-net]]
48-
==== Indexing a document
38+
=== Indexing a document
4939

5040
Documents can be indexed by creating an instance representing a tweet and
5141
indexing it via the client. In these examples, we will work with an index named
5242
'my-tweet-index'.
5343

5444
[source,csharp]
5545
----
56-
var tweet = new Tweet <1>
57-
{
58-
Id = 1,
59-
User = "stevejgordon",
60-
PostDate = new DateTime(2009, 11, 15),
61-
Message = "Trying out the client, so far so good?"
62-
};
63-
64-
var response = await client.IndexAsync(tweet, request => request.Index("my-tweet-index")); <2>
65-
66-
if (response.IsValid) <3>
67-
{
68-
Console.WriteLine($"Index document with ID {response.Id} succeeded."); <4>
69-
}
70-
46+
include::{doc-tests-src}/Usage/CrudExamplesTests.cs[tag=create-tweet]
7147
----
7248
<1> Create an instance of the `Tweet` class with relevant properties set.
7349
<2> Prefer the async APIs, which require awaiting the response.
@@ -77,12 +53,11 @@ operation succeeded.
7753

7854
[discrete]
7955
[[getting-net]]
80-
==== Getting a document
56+
=== Getting a document
8157

8258
[source,csharp]
8359
----
84-
var response = await client.GetAsync<Tweet>(1, idx => idx.Index("my-tweet-index")); <1>
85-
var tweet = response.Source; <2>
60+
include::{doc-tests-src}/Usage/CrudExamplesTests.cs[tag=get-tweet]
8661
----
8762
<1> The `GetResponse` is mapped 1-to-1 with the Elasticsearch JSON response.
8863
<2> The original document is deserialized as an instance of the Tweet class,
@@ -91,25 +66,13 @@ accessible on the response via the `Source` property.
9166

9267
[discrete]
9368
[[searching-net]]
94-
==== Searching for documents
69+
=== Searching for documents
9570

9671
The client exposes a fluent interface and a powerful query DSL for searching.
9772

9873
[source,csharp]
9974
----
100-
var response = await client.SearchAsync<Tweet>(s => s <1>
101-
.Index("my-tweet-index") <2>
102-
.From(0)
103-
.Size(10)
104-
.Query(q => q
105-
.Term(t => t.User, "stevejgordon") <3>
106-
)
107-
);
108-
109-
if (response.IsValid)
110-
{
111-
var tweet = response.Documents.FirstOrDefault(); <4>
112-
}
75+
include::{doc-tests-src}/Usage/CrudExamplesTests.cs[tag=search-tweet-fluent]
11376
----
11477
<1> The generic type argument specifies the `Tweet` class, which is used when
11578
deserialising the hits from the response.
@@ -126,59 +89,34 @@ aren't your thing.
12689

12790
[source,csharp]
12891
----
129-
var request = new SearchRequest("my-tweet-index") <1>
130-
{
131-
From = 0,
132-
Size = 10,
133-
Query = new TermQuery("user") { Value = "stevejgordon" }
134-
};
135-
136-
var response = await client.SearchAsync<Tweet>(request); <2>
137-
138-
if (response.IsValid)
139-
{
140-
var tweet = response.Documents.FirstOrDefault();
141-
}
92+
include::{doc-tests-src}/Usage/CrudExamplesTests.cs[tag=search-tweet-object-initializer]
14293
----
14394
<1> Create an instance of `SearchRequest`, setting properties to control the
14495
search operation.
14596
<2> Pass the request to the `SearchAsync` method on the client.
14697

14798
[discrete]
14899
[[updating-net]]
149-
==== Updating documents
100+
=== Updating documents
150101

151102
Documents can be updated in several ways, including by providing a complete
152103
replacement for an existing document ID.
153104

154105
[source,csharp]
155106
----
156-
tweet.Message = "This is a new message"; <1>
157-
158-
var response = await client.UpdateAsync<Tweet, object>("my-tweet-index", 1, u => u
159-
.Doc(tweet)); <2>
160-
161-
if (response.IsValid)
162-
{
163-
Console.WriteLine("Update document succeeded.");
164-
}
107+
include::{doc-tests-src}/Usage/CrudExamplesTests.cs[tag=update-tweet]
165108
----
166109
<1> Update a property on the existing tweet instance.
167110
<2> Send the updated tweet object in the update request.
168111

169112

170113
[discrete]
171114
[[deleting-net]]
172-
==== Deleting documents
115+
=== Deleting documents
173116

174117
Documents can be deleted by providing the ID of the document to remove.
175118

176119
[source,csharp]
177120
----
178-
var response = await client.DeleteAsync("my-tweet-index", 1);
179-
180-
if (response.IsValid)
181-
{
182-
Console.WriteLine("Delete document succeeded.");
183-
}
121+
include::{doc-tests-src}/Usage/CrudExamplesTests.cs[tag=delete-tweet]
184122
----

docs/usage/index.asciidoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[[usage]]
2-
== Using the .NET Client
2+
= Using the .NET Client
33

4+
[partintro]
45
The sections below provide tutorials on the most frequently used and some less obvious features of {es}.
56

67
For a full reference, see the {ref}/[Elasticsearch documentation] and in particular the {ref}/rest-apis.html[REST APIs] section. The {net-client} follows closely the JSON structures described there.

docs/usage/recommendations.asciidoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[[recommendations]]
2-
=== Usage recommendations
2+
== Usage recommendations
33

44
To achieve the most efficient use of the {net-client}, we recommend following
55
the guidance defined in this article.
66

77
[discrete]
8-
==== Reuse the same client instance
8+
=== Reuse the same client instance
99

1010
When working with the {net-client} we recommend that consumers reuse a single
1111
instance of `ElasticsearchClient` for the entire lifetime of the application.
@@ -22,7 +22,7 @@ by creating a singleton static instance or by registering the type with a
2222
singleton lifetime when using dependency injection containers.
2323

2424
[discrete]
25-
==== Prefer asynchronous methods
25+
=== Prefer asynchronous methods
2626

2727
The {net-client} exposes synchronous and asynchronous methods on the
2828
`ElasticsearchClient`. We recommend always preferring the asynchronous methods,

examples/Aggregations/Bucket/DatehistogramAggregationPage/09ecba5814d71e4c44468575eada9878.asciidoc

Lines changed: 0 additions & 27 deletions
This file was deleted.

examples/Aggregations/Bucket/DatehistogramAggregationPage/39a6a038c4b551022afe83de0523634e.asciidoc

Lines changed: 0 additions & 28 deletions
This file was deleted.

examples/Aggregations/Bucket/DatehistogramAggregationPage/6faf10a73f7d5fffbcb037bdb2cbaff8.asciidoc

Lines changed: 0 additions & 29 deletions
This file was deleted.

examples/Aggregations/Bucket/DatehistogramAggregationPage/70f0aa5853697e265ef3b1df72940951.asciidoc

Lines changed: 0 additions & 37 deletions
This file was deleted.

examples/Aggregations/Bucket/DatehistogramAggregationPage/73e5c88ad1488b213fb278ee1cb42289.asciidoc

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)