Deleted documents reappearing in searches, how to write unit tests for this?

Hello I have a simple CRUD app that reads and writes to Elasticsearch, I am up to writing some unit tests for the app, which involves creating dummy data, checking I can search for the data and then deleting the data.

I have noticed that sometimes my tests are failing, as data which was deleted in a previous test sometimes reappears.

I am able to replicate the issue querying Elasticsearch directly:

  1. run my tests (created,search,delete)
  2. in the browser go to http://localhost:9200/MYINDEXNAME/_search and refresh the page over and over
  3. at this stage I am expecting the _search to return zero results, which it usually does, but sometimes it will return 1 or 2 results which are previously deleted dummy data

I am running Elasticsearch locally on Windows and just used the stock standard ZIP package, no modifications made to the configuration

It'd be useful if you shared your code. Once a document has been deleted (and has been ack'd), then it's not available for Elasticsearch to retrieve. So something else is happening.

Set up:

            var settings = new ConnectionSettings(new Uri(Configuration["ElasticsearchConfiguration:Uri"]))
                .BasicAuthentication(Configuration["ElasticsearchConfiguration:Username"], Configuration["ElasticsearchConfiguration:Password"])
                .DefaultMappingFor<ElasticDocument>(m => m.IndexName(documentIndexName));

            client = new ElasticClient(settings);

            client.Indices.Create(documentIndexName, c => c
                .Map<ElasticDocument>(m => m.AutoMap()));

Here is how I add to my index:

            var elasticDocument = new ElasticDocument()
                Uid = document.Id

            client.Index(elasticDocument, o => o);

and delete:


the actual model:

        [ElasticsearchType(IdProperty = nameof(Uid))]
        private class ElasticDocument
            [Keyword(Name = "uid")]
            public string Uid { get; set; }

Oh, and the search thats returning unexpected results sometimes:

            var searchResponse = client.Search<ElasticDocument>(q => q
                .Query(rq => rq
                    .Bool(b => b
                        .Must(m => m.MatchAll())));

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.