[英]ElasticSearch.NET NEST search<T> url
我正確的索引路徑是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請參閱文檔 ,
var settings = new ConnectionSettings(node)
.MapDefaultTypeIndices(m => m.Add(typeof(Bar), "foo"));
以上代碼在響應中返回相同的結果。
在POST上成功進行低級別調用構建的有效NEST響應:/ foo / bar / _search
通過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; }
}
您可以在搜索lambda表達式中添加其他參數 var response = client.Search<Bar>(s => s.Index("indexName").Query(q => q.Term(o => o.userName, "test")));
我是ElasticSearch的新手,並不知道_type
。
我將相同的_type
名稱設置為POCO類名,它按預期工作。 所以我們可以說, {index}/{type}
是路徑表達式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.