Class: RailsPanda::Search::Strategies::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_panda_search/strategies/base.rb

Overview

Abstract base class that all search strategies must implement.

A strategy is responsible for:

  • Indexing record data (creating entries in whatever backend)
  • Removing indexed data when records change or are destroyed
  • Searching for records matching a query string

Subclasses must implement: index_record!, remove_record!, clear_for_source_type!, and search.

sync_record! has a default implementation (remove + index) that can be overridden for more efficient incremental updates.

See Strategies::Ngram::NgramStrategy for a reference implementation.

Direct Known Subclasses

Ngram::NgramStrategy

Class Method Summary collapse

Class Method Details

.clear_for_source_type!(source_type) ⇒ Object

Remove ALL index entries for a given source type. Called before a full reindex to clear orphaned entries.

Parameters:

  • source_type (String)

    the model name to clear entries for

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/rails_panda_search/strategies/base.rb', line 44

def clear_for_source_type!(source_type)
  raise NotImplementedError, "#{name} must implement .clear_for_source_type!"
end

.index_record!(record, columns) ⇒ Object

Create index entries for a record's searchable columns. This is a pure insert — it does NOT delete old entries.

Parameters:

  • record (ActiveRecord::Base)

    the record to index

  • columns (Array<Symbol>)

    columns to index

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/rails_panda_search/strategies/base.rb', line 28

def index_record!(record, columns)
  raise NotImplementedError, "#{name} must implement .index_record!"
end

.remove_record!(record) ⇒ Object

Remove all index entries for a record. Called after a record is destroyed.

Parameters:

  • record (ActiveRecord::Base)

    the record to remove

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/rails_panda_search/strategies/base.rb', line 36

def remove_record!(record)
  raise NotImplementedError, "#{name} must implement .remove_record!"
end

.search(query, source_type: nil, columns: nil) ⇒ Array<Array(String, Hash)>

Search for records matching the given query. Must return an array of [source_type, source_id] pairs, where source_id is a Hash of primary key column(s) to value(s).

Parameters:

  • query (String)

    the search query

  • source_type (String, nil) (defaults to: nil)

    model name to scope to (nil = global)

  • columns (Array<Symbol>, nil) (defaults to: nil)

    columns to scope to (nil = all)

Returns:

  • (Array<Array(String, Hash)>)

    matching [source_type, source_id] pairs

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/rails_panda_search/strategies/base.rb', line 69

def search(query, source_type: nil, columns: nil)
  raise NotImplementedError, "#{name} must implement .search"
end

.sync_record!(record, columns) ⇒ Object

Sync a record's index: delete old entries, then insert new ones. Called on after_save and manual reindex.

Default: remove_record! + index_record!. Override for more efficient incremental updates.

Parameters:

  • record (ActiveRecord::Base)

    the record to sync

  • columns (Array<Symbol>)

    columns to sync



56
57
58
59
# File 'lib/rails_panda_search/strategies/base.rb', line 56

def sync_record!(record, columns)
  remove_record!(record)
  index_record!(record, columns)
end