|
| 1 | +[[getting-started-net]] |
| 2 | +== Getting started |
| 3 | + |
| 4 | +This page guides you through the installation process of the .NET client, shows |
| 5 | +you how to instantiate the client, and how to perform basic Elasticsearch |
| 6 | +operations with it. |
| 7 | + |
| 8 | +[discrete] |
| 9 | +=== Requirements |
| 10 | + |
| 11 | +.NET Core, .NET 5+ or .NET Framework (4.6.1 and higher). |
| 12 | + |
| 13 | +[discrete] |
| 14 | +=== Installation |
| 15 | + |
| 16 | +To install the latest version of the client for SDK style projects, run the following command: |
| 17 | + |
| 18 | +[source,shell] |
| 19 | +-------------------------- |
| 20 | +dotnet add package Elastic.Clients.Elasticsearch |
| 21 | +-------------------------- |
| 22 | + |
| 23 | +Refer to the <<installation>> page to learn more. |
| 24 | + |
| 25 | + |
| 26 | +[discrete] |
| 27 | +=== Connecting |
| 28 | + |
| 29 | +You can connect to the Elastic Cloud using an API key and the Elasticsearch |
| 30 | +endpoint. |
| 31 | + |
| 32 | +[source,net] |
| 33 | +---- |
| 34 | +var client = new ElasticsearchClient("<CLOUD_ID>", new ApiKey("<API_KEY>")); |
| 35 | +---- |
| 36 | + |
| 37 | +Your Elasticsearch endpoint can be found on the **My deployment** page of your |
| 38 | +deployment: |
| 39 | + |
| 40 | +image::images/es-endpoint.jpg[alt="Finding Elasticsearch endpoint",align="center"] |
| 41 | + |
| 42 | +You can generate an API key on the **Management** page under Security. |
| 43 | + |
| 44 | +image::images/create-api-key.png[alt="Create API key",align="center"] |
| 45 | + |
| 46 | +For other connection options, refer to the <<connecting>> section. |
| 47 | + |
| 48 | + |
| 49 | +[discrete] |
| 50 | +=== Operations |
| 51 | + |
| 52 | +Time to use Elasticsearch! This section walks you through the basic, and most |
| 53 | +important, operations of Elasticsearch. For more operations and more advanced |
| 54 | +examples, refer to the <<examples>> page. |
| 55 | + |
| 56 | + |
| 57 | +[discrete] |
| 58 | +==== Creating an index |
| 59 | + |
| 60 | +This is how you create the `my_index` index: |
| 61 | + |
| 62 | +[source,net] |
| 63 | +---- |
| 64 | +var response = await client.Indices.CreateAsync("my_index"); |
| 65 | +---- |
| 66 | + |
| 67 | + |
| 68 | +[discrete] |
| 69 | +==== Indexing documents |
| 70 | + |
| 71 | +This is a simple way of indexing a document: |
| 72 | + |
| 73 | +[source,net] |
| 74 | +---- |
| 75 | +var doc = new MyDoc |
| 76 | +{ |
| 77 | + Id = 1, |
| 78 | + User = "flobernd", |
| 79 | + Message = "Trying out the client, so far so good?" |
| 80 | +}; |
| 81 | +
|
| 82 | +var response = await client.IndexAsync(doc, "my_index"); |
| 83 | +---- |
| 84 | + |
| 85 | + |
| 86 | +[discrete] |
| 87 | +==== Getting documents |
| 88 | + |
| 89 | +You can get documents by using the following code: |
| 90 | + |
| 91 | +[source,net] |
| 92 | +---- |
| 93 | +var response = await client.GetAsync<MyDoc>(id, idx => idx.Index("my_index")); |
| 94 | +
|
| 95 | +if (response.IsValidResponse) |
| 96 | +{ |
| 97 | + var doc = response.Source; |
| 98 | +} |
| 99 | +---- |
| 100 | + |
| 101 | + |
| 102 | +[discrete] |
| 103 | +==== Searching documents |
| 104 | + |
| 105 | +This is how you can create a single match query with the .NET client: |
| 106 | + |
| 107 | +[source,net] |
| 108 | +---- |
| 109 | +var response = await client.SearchAsync<MyDoc>(s => s |
| 110 | + .Index("my_index") |
| 111 | + .From(0) |
| 112 | + .Size(10) |
| 113 | + .Query(q => q |
| 114 | + .Term(t => t.User, "flobernd") |
| 115 | + ) |
| 116 | +); |
| 117 | +
|
| 118 | +if (response.IsValidResponse) |
| 119 | +{ |
| 120 | + var doc = response.Documents.FirstOrDefault(); |
| 121 | +} |
| 122 | +---- |
| 123 | + |
| 124 | + |
| 125 | +[discrete] |
| 126 | +==== Updating documents |
| 127 | + |
| 128 | +This is how you can update a document, for example to add a new field: |
| 129 | + |
| 130 | +[source,net] |
| 131 | +---- |
| 132 | +doc.Message = "This is a new message"; |
| 133 | +
|
| 134 | +var response = await client.UpdateAsync<MyDoc, MyDoc>("my_index", 1, u => u |
| 135 | + .Doc(doc)); |
| 136 | +---- |
| 137 | + |
| 138 | + |
| 139 | +[discrete] |
| 140 | +==== Deleting documents |
| 141 | + |
| 142 | +[source,net] |
| 143 | +---- |
| 144 | +var response = await client.DeleteAsync("my_index", 1); |
| 145 | +---- |
| 146 | + |
| 147 | + |
| 148 | +[discrete] |
| 149 | +==== Deleting an index |
| 150 | + |
| 151 | +[source,net] |
| 152 | +---- |
| 153 | +var response = await client.Indices.DeleteAsync("my_index"); |
| 154 | +---- |
| 155 | + |
| 156 | + |
| 157 | +[discrete] |
| 158 | +== Further reading |
| 159 | + |
| 160 | +* Refer to the <<recommendations>> page to learn more about how to use the |
| 161 | +client the most efficiently. |
0 commit comments