Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add Elasticsearch example #1010

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions docs/modules/elasticsearch.md
Original file line number Diff line number Diff line change
@@ -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/