How can I search users by display name (not username)?

It is possible to search users by display name? nakama has search by username or userid but if player want add some user to his friends list and he know only display name of user it is possible? also search by username is case-sensitive it is possible to do it case-insensitive?

3 Likes

We are looking for similar API where if player search the keywords. It should give list of username as well as display name based on keyword.
If anyone come across any workaround then the please let us know here.

Thanks

@RustamMG Display names are not unique so there’s no index on the column to optimize a search over the data. There’s a way to perform a search efficiently if you use Postgres as your database engine with Nakama. You could set a custom index with the pg_trgm extension and then use a raw SQL query to perform the search. You should keep in mind though that the index will only be used when at least 3 characters are included within the search term.

I’m always curious though why some teams mix and match the use of usernames and display names to search over. Can you share some games in the market that search over both usernames and display names interchangeably?

@novabyte Snapchat uses this method to search username and displayname in the same field.
You can see this feature by checking Add Friends screen on snapchat and then use Search option.

@YoursTruly To achieve that use case you’ll need to follow the steps I mentioned above. Add a custom index to the display name field and then write a small SQL query which allows you to fetch users from the database by the partial matches over the text input by the player.

Your SQL query will look something like:

SELECT
    id, username, display_name
FROM
    users
WHERE
    username ILIKE concat($1, '%')
    OR display_name ILIKE concat($1, '%')
LIMIT
    100;

NOTE:

  1. You should keep your use of custom SQL queries to an absolute minimum because poorly optimized queries will impact the performance of the rest of the game server features.
  2. The specific query above MUST have a custom index added to the display_name field otherwise it will result in a full table scan which will become slower and slower over time as new users are added to the game.
1 Like