var node = new Uri("http://elasticsearch-server.com:9200");var settings = new ConnectionSettings(node);settings.DefaultIndex("foo");var clIEnt = new ElasticclIEnt(settings);var response = clIEnt.Search<bar>(s => s.query(q => q.Term(o => o.username,"test")));// POCO for response fIEldspublic class bar{ public int userID { get; set; } public string username { get; set; } public DateTime createdTime { get; set; }}
上面的代码响应返回以下消息;
ValID nesT response built from a successful low level call on POST: /foo/bar/_search
如何正确设置搜索路径?
试验1
当我省略settings.DefaultIndex(“foo”);如下所示,它会抛出ArgumentException,但是当我设置DefaultIndex()时,Search< T>使用T名称作为第二个路径.
ArgumentException: Index name is null for the given type and no default index is set. Map an index name using ConnectionSettings.MapDefaultTypeIndices() or set a default index using ConnectionSettings.DefaultIndex().
试用2
参考documentation,
var settings = new ConnectionSettings(node).MapDefaultTypeIndices(m => m.Add(typeof(bar),"foo"));
以上代码在响应中返回相同的结果.
解决方法 通过nesT公开的大部分Elasticsearch API都是强类型的,包括.Search< T>();使用此端点,“index”和“type”都将从T推断,但有时您可能希望将不同的值设置为推断的值.在这些情况下,您可以在搜索流畅的API(或搜索对象,如果使用对象初始化程序语法)上调用其他方法来覆盖推断的值ValID nesT response built from a successful low level call on POST: /foo/bar/_search
voID Main(){ var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); var connectionSettings = new ConnectionSettings(pool) .DefaultIndex("foo"); var clIEnt = new ElasticclIEnt(connectionSettings); // POST http://localhost:9200/foo/bar/_search // Will try to deserialize all _source to instances of bar clIEnt.Search<bar>(s => s .MatchAll() ); // POST http://localhost:9200/foo/_search // Will try to deserialize all _source to instances of bar clIEnt.Search<bar>(s => s .AllTypes() .MatchAll() ); // POST http://localhost:9200/_search // Will try to deserialize all _source to instances of bar clIEnt.Search<bar>(s => s .AllTypes() .Allindices() .MatchAll() ); connectionSettings = new ConnectionSettings(pool) .InferMapPingFor<bar>(m => m .Indexname("bars") .Typename("barbar") ); clIEnt = new ElasticclIEnt(connectionSettings); // POST http://localhost:9200/bars/barbar/_search // Will try to deserialize all _source to instances of bar clIEnt.Search<bar>(s => s .MatchAll() ); // POST http://localhost:9200/bars/_search // Will try to deserialize all _source to instances of bar clIEnt.Search<bar>(s => s .AllTypes() .MatchAll() ); // POST http://localhost:9200/_all/barbar/_search // Will try to deserialize all _source to instances of bar clIEnt.Search<bar>(s => s .Allindices() .MatchAll() ); // POST http://localhost:9200/_search // Will try to deserialize all _source to instances of bar clIEnt.Search<bar>(s => s .Allindices() .AllTypes() .MatchAll() );}public class bar{ public int userID { get; set; } public string username { get; set; } public DateTime createdTime { get; set; }}总结
以上是内存溢出为你收集整理的c# – ElasticSearch.NET NEST搜索网址全部内容,希望文章能够帮你解决c# – ElasticSearch.NET NEST搜索网址所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)