我目前正在考虑编写一个 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 是否提供这种方式?
2015 年 8 月 5 日:对措辞不当的问题表示歉意。我最初尝试使用 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 确实在无类型版本中检索结果但未能在类型版本中进行序列化。