# NEST result loses aggregates

**URL:** <https://discuss.elastic.co/t/nest-result-loses-aggregates/193589>\
**Category:** Elasticsearch\
**Created:** [August 2, 2019, 3:56pm UTC](https://discuss.elastic.co/t/nest-result-loses-aggregates/193589 "2019-08-02T15:56:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rwb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rwb/32/51546_2.png) [@rwb](https://discuss.elastic.co/u/rwb)\
**Post date:** [August 2, 2019, 3:56pm UTC](https://discuss.elastic.co/t/nest-result-loses-aggregates/193589/1 "2019-08-02T15:56:52Z")

</div>

This is my query

```auto
{
   "query":{
      "match_all":{

      }
   },
   "size":20,
   "aggs":{
      "CompanyName.raw":{
         "terms":{
            "field":"CompanyName.raw",
            "size":20,
            "order":{
               "_count":"desc"
            }
         }
      }
   }
}

```

When I send it directly to ElasticSearch the result contains an array `aggregations['CompanyName.raw']['buckets']`.  
However, when I issue the same request with NEST this array is empty.

```auto
                using (MemoryStream ms = new MemoryStream())
                {
                    Client.RequestResponseSerializer.Serialize<ISearchResponse<ProductPurchasing>>(r, ms);
                    ms.Position = 0;
                    using (StreamReader sr = new StreamReader(ms))
                    {
                        string responseJson = sr.ReadToEnd();
                    }
                }

```

Where has this gone? Surely this is a severe bug?

---

<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:** [August 4, 2019, 11:19pm UTC](https://discuss.elastic.co/t/nest-result-loses-aggregates/193589/2 "2019-08-04T23:19:50Z")

</div>

Can you show the response JSON?

> [@rwb](#):
>
> ```auto
> Client.RequestResponseSerializer.Serialize<ISearchResponse<ProductPurchasing>>(r, ms);
> 
> ```

Responses are not guaranteed to be _re-serializable_ back to a `MemoryStream` or byte array. If you want to have both the response bytes and an instance of a response type to work with, you can either

1. Set `.DisableDirectStreaming()` either on `ConnectionSettings` to globally apply, or on an individual request, to buffer the request/response bytes on the response instance, in the `. ApiCall.RequestBodyBytes` or `.ApiCall.ResponseBodyBytes`, respectively.

or

1. Use the low level client to make the request, and return a `StringResponse` or `BytesResponse`. Then deserialize this response to a `SearchResponse<ProductPurchasing>`. If you use the low level client exposed on NEST through the `.LowLevel` property, you can pass a high level request like `SearchRequest<ProductPurchasing>` to the low level client using `PostData.Serializable(request)`

---

<div class="post-metadata">

**Author:** ![rwb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rwb/32/51546_2.png) [@rwb](https://discuss.elastic.co/u/rwb)\
**Post date:** [August 5, 2019, 9:15am UTC](https://discuss.elastic.co/t/nest-result-loses-aggregates/193589/3 "2019-08-05T09:15:52Z")

</div>

Thank you.  
Using the low level client has got me what I need.

(We run a multi-tenant sytem so I'm intercepting queries written by ReactiveSearch to add various restrictions and exclude other clients' data such as prices. Using NEST to handle the queies ought to be easier than manipulating raw JSON and NDJSON.)

---

<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:** [September 2, 2019, 9:15am UTC](https://discuss.elastic.co/t/nest-result-loses-aggregates/193589/4 "2019-09-02T09:15:56Z")

</div>

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