You*_*jae 6 c# elasticsearch nest
我的正确索引路径是POST: /foo/_search低于代码命中率POST: /foo/bar/_search.
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 fields
public class Bar
{
public int userId { get; set; }
public string userName { get; set; }
public DateTime createdTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
以上代码response返回以下消息;
在POST上成功进行低级别调用构建的有效NEST响应:/ foo/bar/_search
如何正确设置搜索路径?
试验1
当我省略了settings.DefaultIndex("foo");行时,它会抛出ArgumentException如下,但是当我设置时DefaultIndex(),Search<T>使用Tname作为第二个路径.
ArgumentException:给定类型的索引名称为null,并且未设置默认索引.使用ConnectionSettings.MapDefaultTypeIndices()映射索引名称,或使用ConnectionSettings.DefaultIndex()设置默认索引.
试用2 请参阅文档,
var settings = new ConnectionSettings(node)
.MapDefaultTypeIndices(m => m.Add(typeof(Bar), "foo"));
Run Code Online (Sandbox Code Playgroud)
以上代码在响应中返回相同的结果.
在POST上成功进行低级别调用构建的有效NEST响应:/ foo/bar/_search
Rus*_*Cam 10
通过NEST公开的大部分Elasticsearch API都采用强类型方式,包括.Search<T>():与此终结点,无论是"index"和"type"将被推断T,但有时你可能需要设置不同的值到这是推断出来的.在这些情况下,您可以在搜索流畅的API(或搜索对象,如果使用对象初始化程序语法)上调用其他方法来覆盖推断的值
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; }
}
Run Code Online (Sandbox Code Playgroud)
您可以在搜索 lambda 表达式中添加其他参数var response = client.Search<Bar>(s => s.Index("indexName").Query(q => q.Term(o => o.userName, "test")));
| 归档时间: |
|
| 查看次数: |
4408 次 |
| 最近记录: |