我的正确索引路径为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; }
}
以上代码response
会返回以下消息;
在POST上成功进行低级别调用构建的有效NEST响应:/ foo / bar / _search
如何正确设置搜索路径?
试用1
当我省略settings.DefaultIndex("foo");
行时,它会抛出ArgumentException
,如下所示,但当我设置DefaultIndex()
时,Search<T>
使用T
名称作为第二条路径。
ArgumentException:给定类型的索引名称为null,并且未设置默认索引。使用ConnectionSettings.MapDefaultTypeIndices()映射索引名称,或使用ConnectionSettings.DefaultIndex()设置默认索引。
试用2 请参阅documentation,
var settings = new ConnectionSettings(node)
.MapDefaultTypeIndices(m => m.Add(typeof(Bar), "foo"));
上面的代码在响应中返回相同的结果。
在POST上成功进行低级别调用构建的有效NEST响应:/ foo / bar / _search
答案 0 :(得分:9)
通过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; }
}
答案 1 :(得分:3)
您可以在搜索lambda表达式中添加其他参数 var response = client.Search<Bar>(s => s.Index("indexName").Query(q => q.Term(o => o.userName, "test")));
答案 2 :(得分:1)
我是ElasticSearch的新手,并不了解_type
。
我将相同的_type
名称设置为POCO类名称,它按预期工作。
所以我们可以说,{index}/{type}
是路径表达式。