element-hq / element-hq/synapse
Local users don't always appear first even with `prefer_local_users: true`
- Dominant language
- Python
- Stars
- 4.6k
- Forks
- 600
- Avg merge
- 5d 22h
- Merged PRs (30d)
- 51
Description
This issue has been migrated from [#15521](https://github.com/matrix-org/synapse/issues/15521).
---
### Description
This might be somewhere between bug and feature request, since the description of the configuration setting says it *prefers* them but does not put them first. Depending on the use case (i.e. schools and other organizations), it is ideal to show the local users first and foremost.
> prefer_local_users: Defines whether to prefer local users in search query results. If set to true, local users are **more likely** to appear above remote users when searching the user directory. Defaults to false.
### Steps to reproduce
1. In `homeserver.yaml` config, set `prefer_local_users: true`
1. Search for a user by localpart using `/user_directory/search`
1. Notice that results from my homeserver (`matrix.mit.edu`) appears in fourth place whereas I expected it to be above the other remove homeservers.


### Homeserver
matrix.mit.edu
### Synapse Version
1.81.0+jammy1
### Installation Method
Debian packages from packages.matrix.org
### Database
Single PostgreSQL 14+238 server, no ports or restores
### Workers
Single process
### Platform
Ubuntu 20.04
### Configuration
```
user_directory:
enabled: true
search_all_users: true
prefer_local_users: true
```
### Relevant log output
```shell
{@rgabriel:matrix.mit.edu} Processed request: 0.027sec/0.000sec (0.000sec, 0.000sec) (0.000sec/0.017sec/1) 554B 200 "POST /_matrix/client/r0/user_directory/search HTTP/1.1" "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/112.0" [0 dbevts]
```
### Anything else that would be useful to know?
_No response_
Related: #12958 (asks for ability to only show local users, this is an in-between)
Contributor guide
Research direction
Start by tracing the /user_directory/search endpoint and the prefer_local_users setting described in homeserver.yaml, then inspect how search results are ordered. Compare the current behavior with the documented preference and the reproduction; done means local users consistently appear before remote users when the setting is enabled.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgresql, python
- Domain
- backend, search
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100