我正在通过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(); } }
我想念什么?提前致谢 ..
NEST尝试猜测类型和索引名称,在您的情况下,它将使用/ blog / blogentries
blog因为您告诉默认索引是什么,并且blogentries因为它会小写并使您传递给的类型名称复数Search<T>。
blog
blogentries
Search<T>
您可以像这样控制类型和索引:
.Search<BlogEntry>(s=>s.AllIndices().Query(...));
这将使NEST知道您实际上要搜索所有索引,因此nest会将其转换/_search为根目录,这与您在curl上发出的命令相同。
/_search
您最可能想要的是:
.Search<BlogEntry>(s=>s.Type("posts").Query(...));
以便NEST搜索 /blog/posts/_search
/blog/posts/_search