简体   繁体   中英

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;
        }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM