# Search UI: Aggs in UI state and request?

**URL:** <https://discuss.elastic.co/t/search-ui-aggs-in-ui-state-and-request/308364>\
**Category:** Elastic Search\
**Tags:** elastic-app-search\
**Created:** [June 28, 2022, 1:28pm UTC](https://discuss.elastic.co/t/search-ui-aggs-in-ui-state-and-request/308364 "2022-06-28T13:28:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![esTest](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/estest/32/107680_2.png) [@esTest](https://discuss.elastic.co/u/esTest)\
**Post date:** [June 28, 2022, 1:28pm UTC](https://discuss.elastic.co/t/search-ui-aggs-in-ui-state-and-request/308364/1 "2022-06-28T13:28:59Z")

</div>

**Versions:**

- @elastic/search-ui-elasticsearch-connector: “1.13.0”
- @elastic/react-search-ui: “1.13.0”

**Backend:**

- ES

Hello, thank you for your work on all of these packages!

Wondering if there is more recommended way to work with Aggs than my current approach below. I’m using the example repo [app-search-reference-ui](https://github.com/elastic/app-search-reference-ui-react) to try out these packages.

# Aggs in API Requests

I see that a top level `aggs:{}` prop is included in the request payload. But from looking at [how the request is built in Request.ts](https://github.com/elastic/search-ui/blob/38f111e336dd6715da3eb263c13f8132b04d188e/packages/search-ui-elasticsearch-connector/src/handlers/search/Request.ts), it seems `aggs` is not one of the props plucked from queryConfig or requestState.

# Aggs in UI State

In the API docs I see there are functions exposed for updating filters, searchTerm, sort, etc but I’m not seeing any docs about maintaining and updating state for Aggs.

# Current Workaround

I’m keeping the queryConfig in my app state. Using a postProcessFn (passed as 2nd arg to my ES connector instance), I read from that queryConfig and modify the request body to include aggs.

```auto
const customizeESRequestBody = (requestBody, requestState, queryConfig) => {
requestBody.aggs = {
  ...queryConfig.aggs
};
return requestBody;
}

```

---

<div class="post-metadata">

**Author:** ![joemcelroy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joemcelroy/32/103757_2.png) [@joemcelroy](https://discuss.elastic.co/u/joemcelroy)\
**Post date:** [June 29, 2022, 1:01am UTC](https://discuss.elastic.co/t/search-ui-aggs-in-ui-state-and-request/308364/2 "2022-06-29T01:01:32Z")

</div>

hey @esTest

Could you share what type of aggregations you are hoping to add into the requestBody / maintain and share a typical Elasticsearch request with the aggregations you want? Could you also share what use-case you're hoping to achieve with controlling aggregations?

Behind the scenes, the elasticsearch-connector will use the facet configuration to generate the aggregations needed to power those facets. See here for more information [Configuration | Elastic Documentation](https://docs.elastic.co/search-ui/api/core/configuration#facets)

Joe

---

<div class="post-metadata">

**Author:** ![esTest](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/estest/32/107680_2.png) [@esTest](https://discuss.elastic.co/u/esTest)\
**Post date:** [June 29, 2022, 1:35pm UTC](https://discuss.elastic.co/t/search-ui-aggs-in-ui-state-and-request/308364/3 "2022-06-29T13:35:53Z")

</div>

Thanks for explaining @joemcelroy, I didn't realize the connector would translate facets to `aggs`.

Here are some example aggs I wanted to apply to most result sets:

- sum a field
- avg a field

Is it possible to express those ^ using the facet API? From checking the code I see the 3 supported facet types are: value, range, location.

Thanks again, cheers!

---

<div class="post-metadata">

**Author:** ![joemcelroy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joemcelroy/32/103757_2.png) [@joemcelroy](https://discuss.elastic.co/u/joemcelroy)\
**Post date:** [June 30, 2022, 4:03am UTC](https://discuss.elastic.co/t/search-ui-aggs-in-ui-state-and-request/308364/4 "2022-06-30T04:03:33Z")

</div>

hey @esTest, from what you said, my understanding is that you want to display a value which uses sums / avg field values in the result set? Its an interesting use-case but not something search-ui supports unfortunately.

The only thing I can suggest would be to query Elasticsearch separately for these field aggregation values and display them separately, leaving search-ui to handle the rest.

---

<div class="post-metadata">

**Author:** ![esTest](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/estest/32/107680_2.png) [@esTest](https://discuss.elastic.co/u/esTest)\
**Post date:** [July 13, 2022, 1:29pm UTC](https://discuss.elastic.co/t/search-ui-aggs-in-ui-state-and-request/308364/5 "2022-07-13T13:29:56Z")

</div>

@joemcelroy Thanks again for the replies. I'm planning to use `react-search-ui` with a custom connector. Cheers!

---

<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:** [August 10, 2022, 1:30pm UTC](https://discuss.elastic.co/t/search-ui-aggs-in-ui-state-and-request/308364/6 "2022-08-10T13:30:24Z")

</div>

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