duc*_*kus 9 c# 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<BlogEntry> Search(string q)
{
var result =
_client.Search<BlogEntry>(s => s.QueryString(q));
return result.Documents.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么?提前致谢 ..
Mar*_*man 11
NEST尝试猜测类型和索引名称,在您的情况下,它将使用/ blog/blogentries
blog因为你告诉默认索引是什么,blogentries因为它会小写并复数你传递给的类型名称Search<T>.
你可以像这样控制哪种类型和索引:
.Search<BlogEntry>(s=>s.AllIndices().Query(...));
Run Code Online (Sandbox Code Playgroud)
这将让NEST知道您实际上想要搜索所有索引,因此嵌套会将其转换/_search为根,等于您在curl上发出的命令.
你最想要的是:
.Search<BlogEntry>(s=>s.Type("posts").Query(...));
Run Code Online (Sandbox Code Playgroud)
所以NEST搜索 /blog/posts/_search