Some time ago, I created a declarative configuration system in our codebase. The purpose is to compare the indices deployed on our ES cluster to what we want them to be, according to the code, and then update them if needed.
To do this, I’ve been using in memory requests to then capture the index creation body, and compare that to one we have stored in the index metadata. Unfortunately, updating from version 8.11.0 of the NuGet package to 8.15.10, we lose InMemoryTransportClient
, so I have changed this to now use InMemoryRequestInvoker
, but instead of working as expected, I get an exception:
Elastic.Clients.Elasticsearch.UnsupportedProductException: The client noticed that the server is not a supported distribution of Elasticsearch or an unknown product.
This is my code using 8.11.0:
var clientSettings = new ElasticsearchClientSettings(new InMemoryTransportClient());
var client = new ElasticsearchClient(clientSettings);
And using 8.15.10:
var clientSettings = new ElasticsearchClientSettings(new InMemoryRequestInvoker());
var client = new ElasticsearchClient(clientSettings);
And the common test code in both examples:
var request = new CreateIndexRequest("dummy")
{
RequestConfiguration = new RequestConfiguration()
{
DisableDirectStreaming = true
}
};
var result = await client
.Indices
.CreateAsync(request); // with 8.15.10, the exception is thrown here
var requestBody = Encoding.UTF8.GetString(result.ApiCallDetails.RequestBodyInBytes);
// with 8.11.0, requestBody now contains "{}", as expected.
How can I make this work with 8.15.10?
1
After reading through the code for the ES library, it seems that it’s specifically checking for the header x-elastic-product: Elasticsearch
.
I’ve added set that in the InMemoryRequestInvoker
constructor, and it now runs without issue as it did with 8.11.0.
var esHeaders = new Dictionary<string, IEnumerable<string>>()
{
{ "x-elastic-product", new [] { "Elasticsearch" } }
};
var memoryRequestInvoker = new InMemoryRequestInvoker(Array.Empty<byte>(), headers: esHeaders);
var clientSettings = new ElasticsearchClientSettings(memoryRequestInvoker);
var client = new ElasticsearchClient(clientSettings);
var request = new CreateIndexRequest("dummy")
{
RequestConfiguration = new RequestConfiguration()
{
DisableDirectStreaming = true
}
};
var result = await client
.Indices
.CreateAsync(request);
var requestBody = Encoding.UTF8.GetString(result.ApiCallDetails.RequestBodyInBytes);
// requestBody now contains "{}" as expected.