Skip to content

Feature: Synonym support for FTS5 searches #1

@mlins

Description

@mlins

Problem

FTS5 has native synonym support, but there's currently no way to configure synonyms through the gem. Apps that need synonym expansion must implement it outside the gem.

Use Case

In a food database, users search for "chicken broth" but USDA data uses "stock":

  • "broth" should match "stock"
  • "unsalted" should match "without salt"

Currently this requires either:

  • Preprocessing queries before calling search()
  • Adding synonyms to the AI/LLM prompt that generates queries

Proposed API

class Food < ApplicationRecord
  include ActiveRecord::Searchable

  searchable do
    field :description, weight: 2.0
    field :category
    
    # Option 1: Simple hash mapping
    synonyms "broth" => "stock", "unsalted" => "without salt"
    
    # Option 2: Bidirectional synonyms
    synonyms "broth" <=> "stock"  # matches in either direction
  end
end

Implementation Notes

SQLite FTS5 supports synonyms via:

  1. Query-time expansion - Expand "broth" to "broth OR stock" before search
  2. Custom tokenizer - More complex but handles index-time synonyms

Query-time expansion is simpler and probably sufficient for most use cases.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions