# NEST - Option to not deserialize the source of hits

**URL:** <https://discuss.elastic.co/t/nest-option-to-not-deserialize-the-source-of-hits/125472>\
**Category:** Elasticsearch\
**Created:** [March 25, 2018, 9:00am UTC](https://discuss.elastic.co/t/nest-option-to-not-deserialize-the-source-of-hits/125472 "2018-03-25T09:00:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dba](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@dba](https://discuss.elastic.co/u/dba)\
**Post date:** [March 25, 2018, 9:00am UTC](https://discuss.elastic.co/t/nest-option-to-not-deserialize-the-source-of-hits/125472/1 "2018-03-25T09:00:55Z")

</div>

Hi

I am using version 6.0.2 of NEST and [Elasticsearch.Net](http://Elasticsearch.Net).

For performance reasons, and because the source may have unknown fields added dynamically, I would like to not deserialize the sources, when getting my search results. I am only passing the hit sources along to the client, so I don't need the added deserialization and garbage collection overhead.

Currently I have solved this by:  
`var result = await client.SearchAsync<object>(searchRequest);`  
and then ToString() on the hit's source. This, however, feels like a hack and performance could be even better had "Source" been a string already.

An alternative could be if there was a way to get the json (or PostData) from a SearchRequest and then use the low level api, but then I wouldn't have my aggregations deserialized and I would like that.

Am I missing something or is my best bet to use ?

Best regards  
/dba

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [March 26, 2018, 3:26am UTC](https://discuss.elastic.co/t/nest-option-to-not-deserialize-the-source-of-hits/125472/2 "2018-03-26T03:26:50Z")

</div>

There's a few ways you might go about tackling this:

1. If you don't need all fields from `_source`, you can use Source Filtering to return the fields of interest
2. You can use the low level client exposed on NEST to return the response as a byte array or string

```auto
var bytesResponse = await client.LowLevel.SearchAsync<BytesResponse>(
    "index", 
    "type", 
    PostData.Serializable(searchRequest));

// do something with the response bytes
var bytes = bytesResponse.Body

```

1. Use `ILazyDocument` as the document type. This still performs some deserialization into a `JToken` (internalized [Json.NET](http://Json.NET) `JToken` type) but may perform better than deserializing to your own type.

> [@dba](#):
>
> but then I wouldn't have my aggregations deserialized and I would like that.

It's not possible to control deserialization of aggregations independently of `_source`. The nearest built-in way would be with `ILazyDocument`.

---

<div class="post-metadata">

**Author:** ![dba](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@dba](https://discuss.elastic.co/u/dba)\
**Post date:** [March 26, 2018, 6:26am UTC](https://discuss.elastic.co/t/nest-option-to-not-deserialize-the-source-of-hits/125472/3 "2018-03-26T06:26:28Z")

</div>

Hi Russ

Thank you for the suggestions.

I guess I am looking for a combination of high-level and low-level. I want to use the high-level api for creating the query and reading the result in regards to "metadata" (aggregations and so on), I just don't need or want the object (the hit's source) to be deserialized, meaning I just want it is a string (or byte array), so that I can forward it to the client.  
I did notice that when using `SearchAsync<object>` the source object is your internal [json.NET](http://json.NET) object and I am just in luck that I can do a ToString() on it and get the json.  
Would it maybe make sense to provide a non generic version of the search method or a special implementation for `SearchAsync<string>` that will give me a source of type string?

I can see others had the same need and found different solutions to it, but these are "broken" in 6.X, so instead of "hacking" it with `SearchAsync<object>`, it would be nice with an "official" way of doing this.  
When moving large amount of data, the performance benefit would be noticeable.

Best regards  
dba

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [March 27, 2018, 5:50am UTC](https://discuss.elastic.co/t/nest-option-to-not-deserialize-the-source-of-hits/125472/4 "2018-03-27T05:50:42Z")

</div>

> [@dba](#):
>
> Would it maybe make sense to provide a non generic version of the search method or a special implementation for SearchAsync\<string\> that will give me a source of type string?

Having a non-generic version would run counter to the design of the search API within the client, and I'm honestly not sure if the effort of implementing within NEST would be met by its usage. From what I've typically seen, folks tend to either want the whole response deserialized, or to handle the whole response as a string or byte array to pass of to another system. Both cases are covered by the high level client and low level client, respectively. I can be convinced otherwise though 😃

It would be possible to implement your own `IElasticsearchSerializer` to do this though. The interface is pretty straightforward:

> <https://github.com/elastic/elasticsearch-net/blob/cd8ea82730b7d557e6725079da2342f0be6b130b/src/Elasticsearch.Net/Serialization/IElasticsearchSerializer.cs>

And read from the stream up until the closing `}` at the same depth as the opening `{`. Using the `JsonNetSerializer` in [NEST.JsonSerializer nuget package](https://www.nuget.org/packages/NEST.JsonNetSerializer/) might get you closer, although you'll now be working with `JsonReader` to read JSON tokens.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 24, 2018, 5:50am UTC](https://discuss.elastic.co/t/nest-option-to-not-deserialize-the-source-of-hits/125472/5 "2018-04-24T05:50:51Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
