Indexing data for Elasticsearch NEST in a .net unit test

Viewed 27

I have implemented Elasticsearch using NEST v7.17.4 in one of the .NET6 WebApi's. All works well. If I run the project, I am able to:

  • Seed the database.
  • Create a valid index.
  • Populate the data into the index.
  • Use postman/swagger to query the search endpoint and get the expected results.

I am now trying to write a simple unit test to compare the search results. I cannot seem to populate the index with data.

TestClass.cs

[TestMethod]
public async Task Search_Test() 
{
    // Arrange
    var articles = new List<Articles>()
    {
        new Article () 
        { 
            Name = "",
            Id = Guid.NewGuid(), 
            Number = "", 
            ParentId = parent1Guid
        }
        // The rest has been cut for brevity. Total 4 articles divided between 2 parents.
    };

    string defaultIndex = "articles";
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var connectionSettings = new ConnectionSettings(pool, new InMemoryConnection())
        .DefaultIndex(defaultIndex).DisableDirectStreaming()
        .EnableDebugMode();

    var elasticClient = new ElasticClient(connectionSettings);
    
    // At this point createIndexResponse.IsValid() returns false
    var createIndexResponse = elasticClient.Indices.Create(defaultIndex, index => index.Map<Article>(m => m.AutoMap()));
    
    var tasks = articles.Select(article => elasticClient.IndexDocumentAsync(article));
    var result = await Task.WhenAll(tasks);

    ...
}

The DebugInformation for each of the results, i.e. result[x].DebugInformation has the following message:

DebugInformation = "Invalid NEST response built from a unsuccessful (200) low level call on PUT: /articles/_doc/189ab620-c85b-4548-8f2e-74297996881e?pretty=true&error_trace=true\r\n# Audit trail of this API call:\r\n - [1] BadResponse: Node: http://localhost:9200/ To...

Is this because of the use of InMemoryConnection()? Should I be doing anything different?

I would appreciate any advice.

Thanks

0 Answers
Related