Skip to content

Add username search endpoint with trigram similarity#171

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/add-searchusername-method
Draft

Add username search endpoint with trigram similarity#171
Copilot wants to merge 5 commits intomainfrom
copilot/add-searchusername-method

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 15, 2025

Implements fuzzy username search using PostgreSQL trigram similarity against the full username history in username_queries table. Returns unique UUIDs ranked by match quality and recency.

Changes

Repository Layer

  • Added SearchUsername(ctx, search, top) method to Postgres account repository
  • Query uses CTE with trigram similarity filtering (similarity(username, $1) > 0)
  • Groups by player_uuid to return unique UUIDs with best match score
  • Sorts by MAX(similarity(...)) DESC, MAX(last_queried_at) DESC
  • Validates top parameter (1-100)

Application Layer

  • Added BuildSearchUsername wrapper with 5s timeout
  • Simple pass-through to repository layer

API Layer

  • GET /v1/username/search?search={term}&top={limit}
  • Returns {"success": true, "uuids": ["uuid1", "uuid2", ...]}
  • Default top=10, max 100
  • Standard rate limiting and CORS

Database

  • Migration to enable pg_trgm extension
  • Updated CI to create extension before tests
  • GIN index creation skipped in migration (schema context issues), recommend manual creation in production:
    CREATE INDEX idx_username_queries_username_trgm 
      ON username_queries USING gin (username gin_trgm_ops);

Example Usage

# Search for usernames similar to "Techno"
GET /v1/username/search?search=Techno&top=5

# Response
{
  "success": true,
  "uuids": [
    "a937646b-f115-44c3-8dbf-9ae4a65669a0",
    "b123456c-d234-4567-8901-234567890abc"
  ]
}

Matches "Technoblade", "TechnoFan", etc. with fuzzy matching, returning most similar results first, then most recently seen.

Original prompt

Create a method on the Postgres player repository called searchusername. This should take a search string and a top int and return a slice of up to top unique uuids that have been registered as having names matching the search term. The implementation should use trigram similarity to sort the matches with a secondary sort on the last time the user was recorded with that username.
Add table tests for this method that set up a schema with a given list of db entries and then queries with a search term and top value and asserts the result.

Create an app method that simply wraps this method, and create very simple tests for it. Refer to e.g. the app method for get tags for inspiration.

Then create a get endpoint that serves this app method. Add simple tests for the endpoint. Refer to the get tags endpoint for inspiration.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 4 commits December 15, 2025 16:01
Co-authored-by: Amund211 <14028449+Amund211@users.noreply.github.com>
Co-authored-by: Amund211 <14028449+Amund211@users.noreply.github.com>
Co-authored-by: Amund211 <14028449+Amund211@users.noreply.github.com>
Co-authored-by: Amund211 <14028449+Amund211@users.noreply.github.com>
Copilot AI changed the title [WIP] Add searchusername method to player repository Add username search endpoint with trigram similarity Dec 15, 2025
Copilot AI requested a review from Amund211 December 15, 2025 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants