Skip to content

Use FT._LIST in CreateAndAliasIndes#970

Open
yelly wants to merge 2 commits intoredis:mainfrom
yelly:list-create-alias-index
Open

Use FT._LIST in CreateAndAliasIndes#970
yelly wants to merge 2 commits intoredis:mainfrom
yelly:list-create-alias-index

Conversation

@yelly
Copy link
Copy Markdown

@yelly yelly commented Mar 15, 2026

Fixes #969.
The current implementation fails tests when running against Redis 8 (presumably the version in which the error message from FT.INFO changed).

Introduced a new implementation for CreateAndAliasIndex which does the following:

  1. Query FT._LIST.
  2. Finds all existing indexes which match the versioned index pattern (index + "_v" + version).
  3. Creates a new index with the latest version + 1 (or 1 if no existing index).
  4. Updates the alias to point to the new index (works whether or not the alias already exists).
  5. Drops all the old indexes.

The implementation is shared between Hash and JSON repositories to avoid duplication.
The new implementation passes the tests against Redis 8 and redis-stack.


Note

Medium Risk
Touches RediSearch index lifecycle management (create/alias/drop), so mistakes could break search availability or delete the wrong indexes; scope is limited to CreateAndAliasIndex logic.

Overview
Fixes CreateAndAliasIndex for both hash and JSON repositories by switching from FT.INFO error-string parsing to FT._LIST-based discovery of existing *_vN indexes.

The new shared helper (om/indexes.go) computes the next versioned index name, creates it, repoints the alias with FT.ALIASUPDATE, and then drops all previously versioned indexes, reducing duplication between HashRepository and JSONRepository.

Reviewed by Cursor Bugbot for commit f749add. Bugbot is set up for automated code reviews on this repo. Configure here.

@jit-ci
Copy link
Copy Markdown

jit-ci bot commented Mar 15, 2026

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates RediSearch index rotation in the om repositories to avoid fragile FT.INFO error-string parsing (which broke on Redis 8), by switching discovery to FT._LIST and centralizing the logic in a shared helper.

Changes:

  • Replaced per-repository CreateAndAliasIndex logic in HashRepository and JSONRepository with a shared createAndAliasIndex helper.
  • Implemented version discovery via FT._LIST, selecting the next idx_vN based on the max existing version and then dropping all prior versioned indexes.
  • Updated repositories to call the new helper and adjusted imports accordingly.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
om/indexes.go New shared helper implementing FT._LIST-based version selection, alias update, and cleanup of old versioned indexes.
om/hash.go Switched HashRepository.CreateAndAliasIndex to use the shared helper.
om/json.go Switched JSONRepository.CreateAndAliasIndex to use the shared helper.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread om/indexes.go
Comment on lines +44 to +63
newIndex := idx + "_v1"
if len(currVers) > 0 {
newIndex = fmt.Sprintf("%s_v%d", idx, slices.Max(currVers)+1)
}

// Create the new index
if err := client.Do(ctx, cmdFn(createCmd(newIndex).Schema())).Error(); err != nil {
return err
}

if err := client.Do(ctx, client.B().FtAliasupdate().Alias(idx).Index(newIndex).Build()).Error(); err != nil {
return fmt.Errorf("failed to update alias: %w", err)
}

for _, ver := range currVers {
currIdx := fmt.Sprintf("%s_v%d", idx, ver)
if err := client.Do(ctx, client.B().FtDropindex().Index(currIdx).Build()).Error(); err != nil {
return fmt.Errorf("failed to drop old index %q: %w", currIdx, err)
}
}
Comment thread om/indexes.go Outdated
Comment thread om/indexes.go
for _, message := range listResp {
n, err := message.ToString()
if err != nil {
return fmt.Errorf("FT._LIST retured non-string response: %w", err)
Comment thread om/indexes.go

// Create the new index
if err := client.Do(ctx, cmdFn(createCmd(newIndex).Schema())).Error(); err != nil {
return err
@rueian
Copy link
Copy Markdown
Collaborator

rueian commented Mar 17, 2026

Hi @yelly, could you also add tests for Redis 8?

@rueian
Copy link
Copy Markdown
Collaborator

rueian commented Apr 7, 2026

ping @yelly, could you also add tests for Redis 8?

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.

Repository CreateAndAliasIndex matches missing index error too specifically

3 participants