弹性搜索(使用NEST)在MatchAll()上返回0个结果

时间:2019-06-26 17:28:07

标签: c# elasticsearch nest

我正在使用以下代码在.net核心应用程序中使用NEST索引文档和测试结果。 但是它没有返回任何记录。

因此我做错了索引编制或查询问题。

是弹性搜索的新手。因此,当我尝试索引文本文件的文本并搜索它进行测试时,不知道以下代码有什么问题。

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

1 个答案:

答案 0 :(得分:0)

问题使用默认的索引名称。弹性搜索中不支持带有大写字母的索引名

因此“ ProjectDocuments ”引起了问题。将其更改为“ project_documents ”并开始工作。

相关问题