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.
2 Likes

Greetings,

Sorry to resurrect an old thread but I recently implemented something similar for my game. I want to allow display names to be searched. I have switched to postgres and I added the required index. But, my query has gotten gnarly. Can you please verify that this query is sane:

SELECT id FROM (
      SELECT id FROM users WHERE display_name ILIKE CONCAT('%', $1::text, '%') AND (id != $2) 
UNION ALL
      SELECT source_id as id FROM user_edge INNER JOIN users u ON u.id = id 
UNION ALL
      SELECT destination_id as id FROM user_edge INNER JOIN users u ON u.id = id) temp 
GROUP BY id 
HAVING (count(*) = 1) 
LIMIT 20

Here, the $1 is the text to search for the display_name. The $2 is the user_id of the current user. The two unions are needed to filter out any existing relationships so that the user does not get their current friends or people they have blocked. I don’t have much data in the tables yet, so I can really test the efficiency. EXPLAIN is currently not using indexes, which I assume is due to the fact the tables only have 1-2 rows…

Before I commit to this course, is this sane :slight_smile: