我目前正在考虑编写一个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在非类型化版本中检索结果,但在类型化版本中无法进行序列化。
2条答案
按热度按时间bfhwhh0e1#
SearchResponse<T>
有一个Hits属性(如果我没记错的话,类型是IEnumerable<IHit<T>>
)。然后,每个命中都有一个Source属性,该属性的类型为T。
所以呢
返回您的
IEnumerable<T>
。zyfwsgd62#
查询的响应将是
Nest.ISearchResponse<T>
。通过访问响应中的Documents属性,您将获得IEnumerable<T>
,这是与搜索匹配的结果列表。更新为了回答更新后的问题:
var result = client.Search<T>("index", "type", query, null);
中的T
应该是您在命令中包含的确切的"type"
,而不是Generic T。