Search code examples
c#elasticsearchnest

Elastic search (using NEST) returns 0 results on MatchAll()


I am using following code to index document and test results using NEST in .net core application. But it is not returning any record.

So either I am doing wrong indexing or I have query problem.

Very new to elastic search. So don't know what is wrong with following code as I am trying to index text of text file and searching it for testing.

private static void Index()
        {
            var settings = new ConnectionSettings().DefaultIndex("ProjectDocuments");

            var client = new ElasticClient(settings);

            //First, you need to make the routing required when you are creating your index, like this:
            client.CreateIndex("ProjectDocuments", d => d 
                .Mappings(mapping => mapping
                .Map<Document>(map => map
                .RoutingField(routing => routing
                .Required(true))
                .AutoMap())
            ));

            Routing routingFromInt = 1;

            Document document = new Document()
            {
                Id = 1,
                Content = "Some Text File Text"
            };

            IIndexResponse result = client.Index<Document>(document, selector => selector
                .Id(1)
                .Routing(routingFromInt));

            //TODO: Following returns 0. so might be issue with indexing itself.
            ISearchResponse<Document> searchResponse = client.Search<Document>(query => query.Query(q => q.MatchAll()).Routing(routingFromInt));

            var documents = searchResponse.Documents;
        }

Solution

  • Issue was with default index name. Index name with Uppercase is not supported in elastic search.

    so "ProjectDocuments" was causing issue. Changed it to "project_documents" and started working.