How can I "pass" the original json response from a NEST Elasticsearch request?

Our client-side code works directly with elasticsearch answers, but I want to put NEST in the middle to do some security and filtering. The easiest way to create a request using NEST (or elasticsearch.net) and then just pass the original json response to my client with the least amount of processing. By the way, I am using ServiceStack.

The previous similar question now has an outdated answer - Return Raw Json to ElasticSearch NEST request

thanks

+5
source share
2 answers

This is beneficial for readers who want to do the same in the new versions of NEST, v2.3 at the time of this writing. If you just need an answer, all you have to do is use ElasticLowLevelClient , according to the doc :

 var responseJson = client.Search<string>(...); 

But if you want to get typed results, then this is a bit more connected. You need to call DisableDirectStreaming() in the settings object and then extract the original json from response.ApiCall.ResponseBodyInBytes , as shown here .

 var settings = new ConnectionSettings(new Uri("http://localhost:9200")) .DefaultIndex("index1") .DisableDirectStreaming(); var response = new ElasticClient(settings) .Search<object>(s => s.AllIndices().AllTypes().MatchAll()); if (response.ApiCall.ResponseBodyInBytes != null) { var responseJson = System.Text.Encoding.UTF8.GetString(response.ApiCall.ResponseBodyInBytes); } 
+4
source

Elasticsearch.Net allows Elasticsearch.Net to directly return a response stream,

 var search = client.Search<Stream>(new { size = 10 }); 

.Search() has many overloads to limit its scope by index and type.

This will return an IElasticsearchResponse<Stream> where you can pass the response stream directly to your choide deserializer (SS.Text in your case) without buffering the client between them.

+3
source

Source: https://habr.com/ru/post/1206232/


All Articles