ElasticSearch NEST客户端不返回结果

ElasticSearch NEST客户端不返回结果

问题描述:

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

这是我如何填充数据集:

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<BlogEntry> Search(string q) 
    { 
     var result = 
      _client.Search<BlogEntry>(s => s.QueryString(q)); 

     return result.Documents.ToList(); 
    } 
} 

我错过了什么?在此先感谢..

NEST尝试猜测类型和索引的名字和你的情况下,它会使用/博客/ blogentries

blog因为你告诉一下默认的指数和blogentries因为它会和小写复制您传递给Search<T>的类型名称。

您可以控制哪些类型和索引像这样:

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

这将让NEST知道你真正想要搜索的所有索引,因此巢将其翻译为/_search根,等于命令你发卷。

你最有可能想要的是:

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

这样NEST搜索在/blog/posts/_search

+0

非常感谢!现在我必须做出正确的映射 – 2013-12-26 13:18:21

+0

谢谢,我一直在努力,因为我的模型名称与索引名称不匹配。此外,如果使用Object Initializer语法(我猜它默认为所有索引),则不会推断索引名称。有办法让它更加明显会很好。 (我会说,“显式比隐式更好”原理可以在这里工作) – Giovanni 2015-04-05 16:27:39

+0

即使使用对象初始值设置语法,它也不应该默认为所有的索引,请介意在github上打开一个包含代码长度的票据? – 2015-04-09 20:22:01