# 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:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![JonasLevin](https://avatars.discourse-cdn.com/v4/letter/j/3da27b/32.png) [@JonasLevin](https://discuss.elastic.co/u/JonasLevin)
#### Post date: [January 27, 2022, 9:00pm UTC](https://discuss.elastic.co/t/include-friend-relations-in-user-search/295632/1 "2022-01-27T21:00:42Z")

</div>

I'm working on creating a search feature for a social network like Mobile App.  
Users can follow each other and search by username, lastname or firstname. Currently we are just indexing username, lastname and firstname into Elasticsearch and store the full user information in a seperate primary Database, referenced by an id stored in Elasticsearch and the primary DB.

Our friend relations are stored in the following schema in Google Bigtable:

```auto
requesterID:requestedID:isAccepted

```

The `requesterID` is the id of the person who requested to follow the other person. The `requestedID` is the id of the person who can accept the request.

So we have a bunch of rows referencing the relations between two users but we currently can't include friend relations in our user search. We would like to boost the rank of search results if the person who is searching is following the other person in the search result.  
So it would list people who your following first in a search result.

Is there a way to model relations in Elasticsearch so that they can be included in a search query?

My first guess would be to store the id's of friends in an array, like: [102,106,120]. But I don't think that this is scalable if a user has thousand of friends.

---

<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).

---

<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: [February 26, 2022, 3:18am UTC](https://discuss.elastic.co/t/include-friend-relations-in-user-search/295632/3 "2022-02-26T03:18:20Z")

</div>

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