Skip to content

Commit 2fda6f8

Browse files
committed
add elasticsearch-getting-started
1 parent 5e1d21f commit 2fda6f8

16 files changed

+779
-376
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ You may want to use this playground to:
66
* Learn more about `Elastic.Clients.Elasticsearch` and `NEST` clients
77
* You want to migrate your existing code from `NEST` to `Elastic.Clients.Elasticsearch`.
88

9-
> See [playground.ipynb](./playground.ipynb) to get started.
9+
> See [playground.ipynb](./src/nest-vs-elasticsearch/playground.ipynb) to get started.
1010
1111
![setup-elastic-infra](./assets/setup-elastic-infra.png)
1212

@@ -47,4 +47,6 @@ jupyter-lab
4747
* Elasticsearch.NET - <https://github.com/elastic/elasticsearch-net>
4848
* NEST + Elasticsearch.NET in one doc - <https://www.elastic.co/guide/en/elasticsearch/client/net-api/7.17/introduction.html>
4949
* .NET Interactive | Samples - <https://github.com/dotnet/interactive/tree/main/samples/notebooks/csharp>
50-
* .NET Interactive - <https://github.com/dotnet/interactive/blob/main/docs/README.md>
50+
* .NET Interactive - <https://github.com/dotnet/interactive/blob/main/docs/README.md>
51+
* Elasticsearch Labs <https://github.com/elastic/elasticsearch-labs>
52+
* <https://www.elastic.co/start-local>

src/get-connection-string.ipynb renamed to src/_infra/get-connection-string.ipynb

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,25 @@
1313
},
1414
"outputs": [],
1515
"source": [
16-
"#!import ./common.ipynb\n",
17-
"\n",
16+
"#r \"nuget: dotenv.net\""
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": null,
22+
"metadata": {
23+
"dotnet_interactive": {
24+
"language": "csharp"
25+
},
26+
"polyglot_notebook": {
27+
"kernelName": "csharp"
28+
}
29+
},
30+
"outputs": [],
31+
"source": [
1832
"using dotenv.net;\n",
1933
"\n",
20-
"var variables = DotEnv.Read(new DotEnvOptions(envFilePaths: new[] {\"../.env\"}));\n",
34+
"var variables = DotEnv.Read(new DotEnvOptions(envFilePaths: new[] {\"../../.env\"}));\n",
2135
"\n",
2236
"if (!variables.TryGetValue(\"PLAYGROUND_CONNECTION_STRING\", out var connectionStringInput)\n",
2337
" || string.IsNullOrEmpty(connectionStringInput))\n",
@@ -27,6 +41,20 @@
2741
"\n",
2842
"var connectionString = new Uri(connectionStringInput);"
2943
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": null,
48+
"metadata": {
49+
"dotnet_interactive": {
50+
"language": "csharp"
51+
},
52+
"polyglot_notebook": {
53+
"kernelName": "csharp"
54+
}
55+
},
56+
"outputs": [],
57+
"source": []
3058
}
3159
],
3260
"metadata": {

src/setup-elastic-infrastructure.ipynb renamed to src/_infra/setup-elastic-infrastructure.ipynb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
"outputs": [],
1515
"source": [
16-
"#!import ./common.ipynb"
16+
"#r \"nuget: Testcontainers.Elasticsearch\""
1717
]
1818
},
1919
{
@@ -39,9 +39,6 @@
3939
},
4040
"outputs": [],
4141
"source": [
42-
"using Nest;\n",
43-
"using Elastic.Clients.Elasticsearch;\n",
44-
"using Elastic.Transport;\n",
4542
"using Testcontainers.Elasticsearch;\n",
4643
"\n",
4744
"var elasticsearchContainer = new ElasticsearchBuilder()\n",
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"dotnet_interactive": {
8+
"language": "csharp"
9+
},
10+
"polyglot_notebook": {
11+
"kernelName": "csharp"
12+
}
13+
},
14+
"outputs": [],
15+
"source": [
16+
"#r \"nuget: Elastic.Clients.Elasticsearch\"\n",
17+
"#r \"nuget: System.Net.Http.Json\"\n",
18+
"#!import ./Utils.cs\n",
19+
"#!import ../_infra/get-connection-string.ipynb\n",
20+
"\n",
21+
"using Elastic.Transport;\n",
22+
"using Elastic.Clients.Elasticsearch;\n",
23+
"using Elastic.Transport.Products.Elasticsearch;\n",
24+
"\n",
25+
"var elasticSettings = new ElasticsearchClientSettings(connectionString)\n",
26+
" .DisableDirectStreaming()\n",
27+
" .ServerCertificateValidationCallback(CertificateValidations.AllowAll);\n",
28+
"\n",
29+
"var client = new ElasticsearchClient(elasticSettings);"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"## Test the Client\n",
37+
"\n",
38+
"Before you continue, confirm that the client has connected with this test.\n"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": null,
44+
"metadata": {
45+
"dotnet_interactive": {
46+
"language": "csharp"
47+
},
48+
"polyglot_notebook": {
49+
"kernelName": "csharp"
50+
}
51+
},
52+
"outputs": [],
53+
"source": [
54+
"var info = await client.InfoAsync();\n",
55+
"\n",
56+
"display(info);"
57+
]
58+
},
59+
{
60+
"cell_type": "markdown",
61+
"metadata": {},
62+
"source": [
63+
"## Index some test data\n",
64+
"Our client is set up and connected to our Elastic deployment. Now we need some data to test out the basics of Elasticsearch queries. We'll use a small index of books with the following fields"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": null,
70+
"metadata": {
71+
"dotnet_interactive": {
72+
"language": "csharp"
73+
},
74+
"polyglot_notebook": {
75+
"kernelName": "csharp"
76+
}
77+
},
78+
"outputs": [],
79+
"source": [
80+
"using System.Text.Json.Serialization;\n",
81+
"\n",
82+
"public class Book\n",
83+
"{\n",
84+
" [JsonPropertyName(\"title\")]\n",
85+
" public string Title { get; set; }\n",
86+
"\n",
87+
" [JsonPropertyName(\"summary\")]\n",
88+
" public string Summary { get; set; }\n",
89+
"\n",
90+
" [JsonPropertyName(\"publish_date\")]\n",
91+
" public DateTimeOffset PublishDate { get; set; }\n",
92+
"\n",
93+
" [JsonPropertyName(\"num_reviews\")]\n",
94+
" public int NumReviews { get; set; }\n",
95+
"\n",
96+
" [JsonPropertyName(\"publisher\")]\n",
97+
" public string Publisher { get; set; }\n",
98+
"\n",
99+
"\n",
100+
" public object? TitleVector { get; set; }\n",
101+
"}"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"metadata": {
108+
"dotnet_interactive": {
109+
"language": "csharp"
110+
},
111+
"polyglot_notebook": {
112+
"kernelName": "csharp"
113+
}
114+
},
115+
"outputs": [],
116+
"source": [
117+
"using Elastic.Clients.Elasticsearch;\n",
118+
"using Elastic.Clients.Elasticsearch.IndexManagement;\n",
119+
"using Elastic.Clients.Elasticsearch.Mapping;\n",
120+
"\n",
121+
"const string BookIndex = \"book_index\";\n",
122+
"var indexDescriptor = new CreateIndexRequestDescriptor<Book>(BookIndex)\n",
123+
" .Mappings(m => m\n",
124+
" .Properties(pp => pp\n",
125+
" .Text(p => p.Title)\n",
126+
" .DenseVector(\n",
127+
" Infer.Property<Book>(p => p.TitleVector),\n",
128+
" d => d.Dims(384).Index(true).Similarity(DenseVectorSimilarity.Cosine))\n",
129+
" .Text(p => p.Summary)\n",
130+
" .Date(p => p.PublishDate)\n",
131+
" .IntegerNumber(p => p.NumReviews)\n",
132+
" .Keyword(p => p.Publisher)\n",
133+
" )\n",
134+
" );"
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": null,
140+
"metadata": {
141+
"dotnet_interactive": {
142+
"language": "csharp"
143+
},
144+
"polyglot_notebook": {
145+
"kernelName": "csharp"
146+
}
147+
},
148+
"outputs": [],
149+
"source": [
150+
"var indexResponse = await client.Indices.CreateAsync(indexDescriptor);\n",
151+
"\n",
152+
"display(indexResponse);\n",
153+
"ToJson(DumpGetRequest(indexResponse.DebugInformation)).Display();"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": null,
159+
"metadata": {
160+
"dotnet_interactive": {
161+
"language": "csharp"
162+
},
163+
"polyglot_notebook": {
164+
"kernelName": "csharp"
165+
}
166+
},
167+
"outputs": [],
168+
"source": [
169+
"using System.Net.Http;\n",
170+
"using System.Net.Http.Json;\n",
171+
"\n",
172+
"var http = new HttpClient();\n",
173+
"var url = \"https://raw.githubusercontent.com/elastic/elasticsearch-labs/main/notebooks/search/data.json\";\n",
174+
"var books = await http.GetFromJsonAsync<Book[]>(url);\n",
175+
"\n",
176+
"display(books);\n"
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": null,
182+
"metadata": {
183+
"dotnet_interactive": {
184+
"language": "csharp"
185+
},
186+
"polyglot_notebook": {
187+
"kernelName": "csharp"
188+
}
189+
},
190+
"outputs": [],
191+
"source": [
192+
"var bulkResponse = await client.BulkAsync(BookIndex, d => d\n",
193+
" .IndexMany<Book>(books, (bd, b) => bd.Index(BookIndex).));\n",
194+
"\n",
195+
"display(DumpGetRequest(bulkResponse));"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": null,
201+
"metadata": {
202+
"dotnet_interactive": {
203+
"language": "csharp"
204+
},
205+
"polyglot_notebook": {
206+
"kernelName": "csharp"
207+
}
208+
},
209+
"outputs": [],
210+
"source": [
211+
"var searchResponse = await client.SearchAsync<Book>(s => \n",
212+
" s \n",
213+
" .Index(BookIndex)\n",
214+
" .Query(q => q.Match(d => d.Field(f => f.Title).Query(\"JavaScript\")))\n",
215+
");\n",
216+
"\n",
217+
"DumpGetRequest(searchResponse).Display();\n",
218+
"\n",
219+
"searchResponse.Display();"
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": null,
225+
"metadata": {
226+
"dotnet_interactive": {
227+
"language": "csharp"
228+
},
229+
"polyglot_notebook": {
230+
"kernelName": "csharp"
231+
}
232+
},
233+
"outputs": [],
234+
"source": []
235+
}
236+
],
237+
"metadata": {
238+
"kernelspec": {
239+
"display_name": ".NET (C#)",
240+
"language": "C#",
241+
"name": ".net-csharp"
242+
},
243+
"polyglot_notebook": {
244+
"kernelInfo": {
245+
"defaultKernelName": "csharp",
246+
"items": [
247+
{
248+
"aliases": [],
249+
"languageName": "csharp",
250+
"name": "csharp"
251+
}
252+
]
253+
}
254+
}
255+
},
256+
"nbformat": 4,
257+
"nbformat_minor": 2
258+
}

0 commit comments

Comments
 (0)