我目前正在考虑将ElasticSearch.Net/NEST客户端编写为PlainElastic.Net的可能替代品(因为ElasticSearch.Net具有我想要使用的故障转移功能)
在PlainElastic中,我可以编写如下内容:
var command = new SearchCommand(index, type);
var result = Connection.Post(command, query);
var searchResult = Serializer.ToSearchResult<T>(result);
return searchResult.Documents;
这将返回一个IEnumerable,其中每个结果都是T类型。 我希望在NEST中有类似的东西,我作为初步测试开始的是:
var result = client.Search<T>("index", "type", query, null);
但我似乎无法像PlainElastic.Net使用ToSearchResult方法那样找到反序列化结果的方法。 Elasticsearch.Net/NEST是否提供这种方式?
08/05/2015:道歉措辞严厉。我最初尝试使用NEST的是无类型版本:
var result = client.Search("index", "type", query, null);
这导致了我试图单独反序列化的结果,因为原始尝试使用了类型化版本:
var result = client.Search<T>("index", "type", query, null);
抛出异常:
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Elasticsearch.Net
StackTrace:
at Elasticsearch.Net.Serialization.PocoJsonSerializerStrategy.DeserializeObject( Object value, Type type) in C:\code\elasticsearch- net\src\Elasticsearch.Net\Serialization\SimpleJson.cs:line 1370
at Elasticsearch.Net.Serialization.SimpleJson.DeserializeObject(String json, Type type, IJsonSerializerStrategy jsonSerializerStrategy) in C:\code\elasticsearch-net\src\Elasticsearch.Net\Serialization\SimpleJson.cs:line 553
at Elasticsearch.Net.Serialization.SimpleJson.DeserializeObject[T](String json) in C:\code\elasticsearch-net\src\Elasticsearch.Net\Serialization\SimpleJson.cs:line 570
at Elasticsearch.Net.Serialization.ElasticsearchDefaultSerializer.Deserialize[T](Stream stream) in C:\code\elasticsearch-net\src\Elasticsearch.Net\Serialization\ElasticsearchDefaultSerializer.cs:line 27
at Elasticsearch.Net.Connection.RequestHandlers.RequestHandler.StreamToTypedResponse[T](ElasticsearchResponse`1 streamResponse, ITransportRequestState requestState, Byte[] readBytes) in C:\code\elasticsearch-net\src\Elasticsearch.Net\Connection\RequestHandlers\RequestHandler.cs:line 254
at Elasticsearch.Net.Connection.RequestHandlers.RequestHandler.ReturnTypedResponse[T](TransportRequestState`1 requestState, ElasticsearchResponse`1 streamResponse, ElasticsearchServerError& error) in C:\code\elasticsearch-net\src\Elasticsearch.Net\Connection\RequestHandlers\RequestHandler.cs:line 106
at Elasticsearch.Net.Connection.RequestHandlers.RequestHandler.CoordinateRequest[T](TransportRequestState`1 requestState, Int32 maxRetries, Int32 retried, Boolean& aliveResponse) in C:\code\elasticsearch-net\src\Elasticsearch.Net\Connection\RequestHandlers\RequestHandler.cs:line 131
at Elasticsearch.Net.Connection.RequestHandlers.RequestHandler.DoRequest[T](TransportRequestState`1 requestState) in C:\code\elasticsearch-net\src\Elasticsearch.Net\Connection\RequestHandlers\RequestHandler.cs:line 177
at Elasticsearch.Net.Connection.RequestHandlers.RequestHandler.Request[T](TransportRequestState`1 requestState, Object data) in C:\code\elasticsearch-net\src\Elasticsearch.Net\Connection\RequestHandlers\RequestHandler.cs:line 34
at Elasticsearch.Net.Connection.Transport.DoRequest[T](String method, String path, Object data, IRequestParameters requestParameters) in C:\code\elasticsearch-net\src\Elasticsearch.Net\Connection\Transport.cs:line 343
at Elasticsearch.Net.ElasticsearchClient.DoRequest[T](String method, String path, Object data, IRequestParameters requestParameters) in C:\code\elasticsearch-net\src\Elasticsearch.Net\ElasticsearchClient.cs:line 65
at Elasticsearch.Net.ElasticsearchClient.Search[T](String index, String type, Object body, Func`2 requestParameters) in C:\code\elasticsearch-net\src\Elasticsearch.Net\ElasticsearchClient.Generated.cs:line 33515
因此,我认为我的原始问题仍然相同,但更多的是尝试理解为什么NEST确实在非类型化版本中检索结果但未能在类型版本中进行序列化。
答案 0 :(得分:5)
如果我没记错的话,SearchResponse<T>
有一个Hits属性(IEnumberable<IHit<T>>
类型。)。
然后每个匹配都有一个Source属性,类型为T。
所以
var enumerable = results.Hits.Select(h => h.Source);
为您提供IEnumerable<T>
。
答案 1 :(得分:1)
您的查询回复将为Nest.ISearchResponse<T>
。通过访问响应中的Documents属性,您将获得IEnumerable<T>
,这是与您的搜索匹配的结果列表。
<强>更新强>
为了回复更新后的问题:T
中的var result = client.Search<T>("index", "type", query, null);
应该是您在命令中包含的确切"type"
,而不是通用T.