Skip to content

Commit

Permalink
add elasticsearch-getting-started
Browse files Browse the repository at this point in the history
  • Loading branch information
NikiforovAll committed Oct 17, 2024
1 parent 5e1d21f commit 2fda6f8
Show file tree
Hide file tree
Showing 16 changed files with 779 additions and 376 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ You may want to use this playground to:
* Learn more about `Elastic.Clients.Elasticsearch` and `NEST` clients
* You want to migrate your existing code from `NEST` to `Elastic.Clients.Elasticsearch`.

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

Expand Down Expand Up @@ -47,4 +47,6 @@ jupyter-lab
* Elasticsearch.NET - <https://github.com/elastic/elasticsearch-net>
* NEST + Elasticsearch.NET in one doc - <https://www.elastic.co/guide/en/elasticsearch/client/net-api/7.17/introduction.html>
* .NET Interactive | Samples - <https://github.com/dotnet/interactive/tree/main/samples/notebooks/csharp>
* .NET Interactive - <https://github.com/dotnet/interactive/blob/main/docs/README.md>
* .NET Interactive - <https://github.com/dotnet/interactive/blob/main/docs/README.md>
* Elasticsearch Labs <https://github.com/elastic/elasticsearch-labs>
* <https://www.elastic.co/start-local>
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,25 @@
},
"outputs": [],
"source": [
"#!import ./common.ipynb\n",
"\n",
"#r \"nuget: dotenv.net\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"using dotenv.net;\n",
"\n",
"var variables = DotEnv.Read(new DotEnvOptions(envFilePaths: new[] {\"../.env\"}));\n",
"var variables = DotEnv.Read(new DotEnvOptions(envFilePaths: new[] {\"../../.env\"}));\n",
"\n",
"if (!variables.TryGetValue(\"PLAYGROUND_CONNECTION_STRING\", out var connectionStringInput)\n",
" || string.IsNullOrEmpty(connectionStringInput))\n",
Expand All @@ -27,6 +41,20 @@
"\n",
"var connectionString = new Uri(connectionStringInput);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"outputs": [],
"source": [
"#!import ./common.ipynb"
"#r \"nuget: Testcontainers.Elasticsearch\""
]
},
{
Expand All @@ -39,9 +39,6 @@
},
"outputs": [],
"source": [
"using Nest;\n",
"using Elastic.Clients.Elasticsearch;\n",
"using Elastic.Transport;\n",
"using Testcontainers.Elasticsearch;\n",
"\n",
"var elasticsearchContainer = new ElasticsearchBuilder()\n",
Expand Down
258 changes: 258 additions & 0 deletions src/elasticsearch-getting-started/00-quickstart.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#r \"nuget: Elastic.Clients.Elasticsearch\"\n",
"#r \"nuget: System.Net.Http.Json\"\n",
"#!import ./Utils.cs\n",
"#!import ../_infra/get-connection-string.ipynb\n",
"\n",
"using Elastic.Transport;\n",
"using Elastic.Clients.Elasticsearch;\n",
"using Elastic.Transport.Products.Elasticsearch;\n",
"\n",
"var elasticSettings = new ElasticsearchClientSettings(connectionString)\n",
" .DisableDirectStreaming()\n",
" .ServerCertificateValidationCallback(CertificateValidations.AllowAll);\n",
"\n",
"var client = new ElasticsearchClient(elasticSettings);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test the Client\n",
"\n",
"Before you continue, confirm that the client has connected with this test.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"var info = await client.InfoAsync();\n",
"\n",
"display(info);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Index some test data\n",
"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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"using System.Text.Json.Serialization;\n",
"\n",
"public class Book\n",
"{\n",
" [JsonPropertyName(\"title\")]\n",
" public string Title { get; set; }\n",
"\n",
" [JsonPropertyName(\"summary\")]\n",
" public string Summary { get; set; }\n",
"\n",
" [JsonPropertyName(\"publish_date\")]\n",
" public DateTimeOffset PublishDate { get; set; }\n",
"\n",
" [JsonPropertyName(\"num_reviews\")]\n",
" public int NumReviews { get; set; }\n",
"\n",
" [JsonPropertyName(\"publisher\")]\n",
" public string Publisher { get; set; }\n",
"\n",
"\n",
" public object? TitleVector { get; set; }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"using Elastic.Clients.Elasticsearch;\n",
"using Elastic.Clients.Elasticsearch.IndexManagement;\n",
"using Elastic.Clients.Elasticsearch.Mapping;\n",
"\n",
"const string BookIndex = \"book_index\";\n",
"var indexDescriptor = new CreateIndexRequestDescriptor<Book>(BookIndex)\n",
" .Mappings(m => m\n",
" .Properties(pp => pp\n",
" .Text(p => p.Title)\n",
" .DenseVector(\n",
" Infer.Property<Book>(p => p.TitleVector),\n",
" d => d.Dims(384).Index(true).Similarity(DenseVectorSimilarity.Cosine))\n",
" .Text(p => p.Summary)\n",
" .Date(p => p.PublishDate)\n",
" .IntegerNumber(p => p.NumReviews)\n",
" .Keyword(p => p.Publisher)\n",
" )\n",
" );"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"var indexResponse = await client.Indices.CreateAsync(indexDescriptor);\n",
"\n",
"display(indexResponse);\n",
"ToJson(DumpGetRequest(indexResponse.DebugInformation)).Display();"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"using System.Net.Http;\n",
"using System.Net.Http.Json;\n",
"\n",
"var http = new HttpClient();\n",
"var url = \"https://raw.githubusercontent.com/elastic/elasticsearch-labs/main/notebooks/search/data.json\";\n",
"var books = await http.GetFromJsonAsync<Book[]>(url);\n",
"\n",
"display(books);\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"var bulkResponse = await client.BulkAsync(BookIndex, d => d\n",
" .IndexMany<Book>(books, (bd, b) => bd.Index(BookIndex).));\n",
"\n",
"display(DumpGetRequest(bulkResponse));"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"var searchResponse = await client.SearchAsync<Book>(s => \n",
" s \n",
" .Index(BookIndex)\n",
" .Query(q => q.Match(d => d.Field(f => f.Title).Query(\"JavaScript\")))\n",
");\n",
"\n",
"DumpGetRequest(searchResponse).Display();\n",
"\n",
"searchResponse.Display();"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"languageName": "csharp",
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 2fda6f8

Please sign in to comment.