[英]ElasticSearch NEST client not returning results
I am running a simple query through the ElasticSearch NEST C# client. 我正在通过ElasticSearch NEST C#客户端运行一个简单的查询。 I receive results when I run the same query through http, but I get zero documents returned from the client. 当我通过http运行相同的查询时,我收到结果,但是我从客户端返回零文档。
This is how I populated the data set: 这是我填充数据集的方式:
curl -X POST "http://localhost:9200/blog/posts" -d @blog.json
This POST request returns a JSON result: 此POST请求返回JSON结果:
http://localhost:9200/_search?q=adipiscing
This is the code I have that is not returning anything. 这是我没有返回任何内容的代码。
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();
}
}
What am I missing? 我错过了什么? Thanks in advance .. 提前致谢 ..
NEST tries to guess the type and index name and in your case it will use /blog/blogentries NEST尝试猜测类型和索引名称,在您的情况下,它将使用/ blog / blogentries
blog
because that what you told the default index is and blogentries
because it will lowercase and pluralize the type name you pass to Search<T>
. blog
因为你告诉默认索引的是和blogentries
因为它会小写并复数你传递给Search<T>
的类型名称。
You can control which type and index like so: 你可以像这样控制哪种类型和索引:
.Search<BlogEntry>(s=>s.AllIndices().Query(...));
This will let NEST know you actually want to search on all indices and so nest will translate it to /_search
on the root, equal to the command you issued on curl. 这将让NEST知道您实际上想要搜索所有索引,因此嵌套会将其转换为根目录上的/_search
,等于您在curl上发出的命令。
What you most likely want is: 你最想要的是:
.Search<BlogEntry>(s=>s.Type("posts").Query(...));
So that NEST searches in /blog/posts/_search
所以NEST在/blog/posts/_search
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.