# Include Friend relations in user search

**URL:** <https://discuss.elastic.co/t/include-friend-relations-in-user-search/295632>\
**Category:** Elasticsearch\
**Created:** [January 27, 2022, 9:00pm UTC](https://discuss.elastic.co/t/include-friend-relations-in-user-search/295632 "2022-01-27T21:00:42Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 29, 2022, 3:17am UTC](https://discuss.elastic.co/t/include-friend-relations-in-user-search/295632/2 "2022-01-29T03:17:45Z")

</div>

To handle relation in Elasticsearch is a difficult problem and there are some limitation and trade-off between strategies.

This is only my suggestion and there could be other ways.

1. create 2 indices
  - user index (the same as now)
  - request relation index (just as in Google Bigtable)

2. use pivot transform function to create an index for search
  - source both indices above
  - group by user\_id
  - one aggregation is top\_hit bucket aggregation with filter aggregation on user index which may hit only one user document per bucket.
  - one aggregation is something like "[unique values aggregation](https://discuss.elastic.co/t/how-to-add-field-that-is-only-present-in-some-documents/295283/6)" with filter aggregation on request relation index

you can [balance the performance with some transform settings](https://www.elastic.co/guide/en/elasticsearch/reference/current/transform-scale.html).

---

_[View the full topic](https://discuss.elastic.co/t/include-friend-relations-in-user-search/295632)._
