Module: RailsPanda::Search::Searchable::SearchClassMethods

Defined in:
lib/rails_panda_search/searchable.rb

Overview

Class methods added only to models that call can_search_in

Instance Method Summary collapse

Instance Method Details

#reindex_search_all!Object

Reindex all records in this model across all active strategies. Clears all existing entries first, then inserts fresh.



62
63
64
65
66
67
68
69
70
# File 'lib/rails_panda_search/searchable.rb', line 62

def reindex_search_all!
  strategies = RailsPanda::Search.config.active_strategy_classes

  strategies.each { |strategy| strategy.clear_for_source_type!(name) }

  find_each do |record|
    strategies.each { |strategy| strategy.index_record!(record, record.searchable_columns) }
  end
end

#search_for(query, columns: nil) ⇒ Object

Search for records matching the given query string. Returns an ActiveRecord::Relation.

Dispatches to all active strategies and merges results.

User.search_for("alice")
User.search_for("alice", columns: [:email])
User.where(active: true).search_for("alice")


49
50
51
52
53
54
55
56
57
58
# File 'lib/rails_panda_search/searchable.rb', line 49

def search_for(query, columns: nil)
  all_matches = RailsPanda::Search.config.active_strategy_classes.flat_map do |strategy|
    strategy.search(query, source_type: name, columns: columns)
  end

  pk_hashes = all_matches.map(&:last).uniq
  return none if pk_hashes.empty?

  _relation_from_pk_hashes(pk_hashes)
end