diff --git a/docs/modules/elasticsearch.md b/docs/modules/elasticsearch.md new file mode 100644 index 000000000..00fa7ebe2 --- /dev/null +++ b/docs/modules/elasticsearch.md @@ -0,0 +1,59 @@ +# Elasticsearch + +[Elasticsearch](https://www.elastic.co/elasticsearch/) is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores data for lightning fast search, fine‑tuned relevancy, and powerful analytics that scale with ease. + +The following example uses the following NuGet packages: + +```console title="Install the NuGet dependencies" +dotnet add package Testcontainers.Elasticsearch +dotnet add package Elastic.Clients.Elasticsearch +dotnet add package xunit +``` + +IDEs and editors may also require the following packages to run tests: `xunit.runner.visualstudio` and `Microsoft.NET.Test.Sdk`. + +Copy and paste the following code into a new `.cs` test file within an existing test project. + +```csharp +using Elastic.Clients.Elasticsearch; +using Elastic.Transport; +using Testcontainers.Elasticsearch; +using Xunit; + +namespace TestcontainersModules; + +public class ElasticsearchContainerTest : IAsyncLifetime +{ + private readonly ElasticsearchContainer elasticsearch + = new ElasticsearchBuilder() + .Build(); + + [Fact] + public async Task ReadFromElasticsearch() + { + var connectionString = elasticsearch.GetConnectionString(); + var settings = new ElasticsearchClientSettings(new Uri(connectionString)); + settings.ServerCertificateValidationCallback(CertificateValidations.AllowAll); + + var client = new ElasticsearchClient(settings); + + var stats = await client.PingAsync(); + + Assert.True(stats.IsValidResponse); + } + + public Task InitializeAsync() + => elasticsearch.StartAsync(); + + public Task DisposeAsync() + => elasticsearch.DisposeAsync().AsTask(); +} +``` + +To execute the tests, use the command `dotnet test` from a terminal. + +## A Note To Developers + +The Testcontainers module creates a container that listens to requests over **HTTPS**. To communicate with the Elasticsearch instance, developers must create a `ElasticsearchClientSettings` instance and set the `ServerCertificateValidationCallback` delegate to `CertificateValidations.AllowAll`. Failing to do so will result in a communication failure as the .NET will reject the certificate coming from the container. + +[xunit]: https://xunit.net/