Include Friend relations in user search

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" with filter aggregation on request relation index

you can balance the performance with some transform settings.