Build "User Search" using Neynar and OpenRanks' Global Ranking API
Surface better search results for your client.
In your client, whether it's a global user search or a search based on @mentions in a cast, both scenarios necessitate returning results that prioritize non-spam accounts at the top.
Using the combination of these APIs will lead to a latency time of around 500ms OpenRank API - 300ms and Neynar APIs - 200ms
Search functionality will be one of the pivotal features of your client. In this guide, we'll utilize Neynar's API to initially retrieve all user results based on a search query and subsequently sort these results according to the global rank of each user.
This guide expects you to have already have signed up for a Neynar account and have the basics set up ready to be able to consume Neynars' APIs. If you haven't, you can go through the getting started guide here.
Step 1: Getting the search query
This aspect is particularly tailored to your client's specifications, particularly in how it manages text inputs. For the purpose of this guide, we will utilize a static string as the search query and we also need the users FID, so we can pass it as the viewer_fid
to Neynars API as a parameter in the next step, for relevant results.
Step 2: Using Neynar API to get a list of responses for the search query
In this step, we utilize Neynar's user search API endpoint to retrieve results for all users based on the username search query. The search query is set with a hardcoded limit
of 10 results, which can be adjusted according to your specific requirements. Additionally, we enhance the quality of responses by passing the viewer_fid
parameter as the User FID while using the Neynar API.
By the end of this step, you'll have an array of user objects that match the relevant search query.
Step 3: Using OpenRanks' Global Ranking APIs to sort the result based on global rank
From the previous step, we obtained an array of various users based on the search query. Now, we'll filter out FIDs for all the returned users and pass them as the body to OpenRanks Global Ranking API to obtain the global ranking for each of those users.
Step 4: Creating a new array merging the userwith rank and score key values
At the end of the previous step, we now have two arrays: one from Neynar, usernameResponsesArray
, containing user objects based on the search query, and another from OpenRank, usersGlobalRankResponseArray
, containing ranks of all those users' FIDs. Now, we create a new array, userResponseArrayWithRank
, which merges the two based on FIDs and helps create a new array with all the user information we have from Neynar, along with each user's rank and score.
The code below accomplishes the following: it compares user FIDs from both arrays, and if the FID matches, it adds the rank and score key-value pairs from the usersGlobalRankResponse
to the usernameResponsesArray
.
Now, we have an array of objects for the users along with their global rank and score included.
Step 5: Sorting the array based on global rank
In this step, we pass the previous array through the function sortByRankAscending
to sort the array based on the ascending list of rankings. This ensures that highly globally ranked users appear first. This helps significantly in sorting out spam accounts, pushing them down the list rather than displaying them at the top.
Future Improvements:
In the future, once personalized rankings APIs are made available for each user, which can be queried based on an input list of FIDs, providing rankings based on the viewer's FID, we can utilize this instead of the global rankings API to generate or even sort the list.
Last updated