ElasticSearch NEST客户端不返回结果

我正在通过ElasticSearch NEST C#客户端运行一个简单的查询。 当我通过http运行相同的查询时,我收到结果,但是我从客户端返回零文档。

这是我填充数据集的方式:

curl -X POST "http://localhost:9200/blog/posts" -d @blog.json

此POST请求返回JSON结果:

http://localhost:9200/_search?q=adipiscing

这是我没有返回任何内容的代码。

 public class Connector { private readonly ConnectionSettings _settings; private readonly ElasticClient _client; public Connector() { _settings = new ConnectionSettings("localhost", 9200); _settings.SetDefaultIndex("blog"); _client = new ElasticClient(_settings); } public IEnumerable Search(string q) { var result = _client.Search(s => s.QueryString(q)); return result.Documents.ToList(); } } 

我错过了什么? 提前致谢 ..

NEST尝试猜测类型和索引名称,在您的情况下,它将使用/ blog / blogentries

blog因为你告诉默认索引的是和blogentries因为它会小写并复数你传递给Search的类型名称。

你可以像这样控制哪种类型和索引:

  .Search(s=>s.AllIndices().Query(...)); 

这将让NEST知道您实际上想要搜索所有索引,因此嵌套会将其转换为根目录上的/_search ,等于您在curl上发出的命令。

你最想要的是:

  .Search(s=>s.Type("posts").Query(...)); 

所以NEST在/blog/posts/_search