Class: RailsPanda::Search::Strategies::Base
- Inherits:
-
Object
- Object
- RailsPanda::Search::Strategies::Base
- 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
Class Method Summary collapse
-
.clear_for_source_type!(source_type) ⇒ Object
Remove ALL index entries for a given source type.
-
.index_record!(record, columns) ⇒ Object
Create index entries for a record's searchable columns.
-
.remove_record!(record) ⇒ Object
Remove all index entries for a record.
-
.search(query, source_type: nil, columns: nil) ⇒ Array<Array(String, Hash)>
Search for records matching the given query.
-
.sync_record!(record, columns) ⇒ Object
Sync a record's index: delete old entries, then insert new ones.
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.
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.
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.
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).
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.
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 |