Returning Raw Json in ElasticSearch NEST query

不羁岁月 提交于 2019-12-19 02:26:34

问题


I'm doing a small research about a client for elastic search in .net and I found that NEST is one of the most supported solutions for this matter.

I was looking at Nest's docummentation and I couldn´t find a way to output a raw json from a query and avoid the serialization into an object, because I'm using angularJs in the front end I don´t want to overload the process of sending the information to the client with some unnecessary steps.

......and also I'd like to know how can I overrdide the serialization process?

I found that NEST uses Json.NET which I would like to change for the servicestack json serielizer.

thanks!


回答1:


Hi Pedro you can do this with NEST

var searchDescriptor = new SearchDescriptor<ElasticSearchProject>()
    .Query(q=>q.MatchAll());
var request = this._client.Serializer.Serialize(searchDescriptor);
ConnectionStatus result = this._client.Raw.SearchPost(request);
Assert.NotNull(result);
Assert.True(result.Success);
Assert.IsNotEmpty(result.Result);

This allows you to strongly type your queries, but return the string .Result which is the raw response from elasticsearch as string to your

request can be an object or the string so if you are OK with the internal json serialize just pass searchDescriptor directly




回答2:


Use RequestResponseSerializer instead of Serializer.

var searchDescriptor = ...;
...
byte[] b = new byte[60000];
using (MemoryStream ms = new MemoryStream(b))
{
    this._client.RequestResponseSerializer.Serialize(searchDescriptor , ms);
}
var rawJson = System.Text.Encoding.Default.GetString(b);


来源:https://stackoverflow.com/questions/20751970/returning-raw-json-in-elasticsearch-nest-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!