-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e1d21f
commit 2fda6f8
Showing
16 changed files
with
779 additions
and
376 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.