11

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!

pedrommuller
  • 15,741
  • 10
  • 76
  • 126

2 Answers2

12

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

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Martijn Laarman
  • 13,476
  • 44
  • 63
  • 1
    thanks is there any way to replace the json.net serializer impl with a custom one? (let's say servicestack) – pedrommuller Dec 27 '13 at 01:50
  • @Martijn Is there a way to visualize the query being sent before it is sent? – Dema Jun 11 '14 at 17:11
  • result.Result does not seem to exist in current API. How is this now achieved? I want to pass through the elasticsearch server response directly to my client, as the client uses a javascript library to work with standard ES responses for faceting etc. thanks. – richardwhatever Nov 01 '14 at 14:42
  • I have errors on ConnectionStatus and SearchPost. Am I missing a namespace? – beruic May 02 '16 at 12:30
  • 1
    This code seems completely broken when using a newer version (2.4) of ElasticClient. – Luca Cremonesi Aug 04 '16 at 14:55
  • If you're returning the raw response of ES, NEST doesn't serialize it or need to. It is in the JSON format the Java based ES server serialized it in. – wilsotc Jan 11 '17 at 16:32
0

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);