Module: RailsPanda::Search

Defined in:
lib/rails_panda_search.rb,
lib/rails_panda_search/config.rb,
lib/rails_panda_search/engine.rb,
lib/rails_panda_search/search.rb,
lib/rails_panda_search/version.rb,
lib/rails_panda_search/searchable.rb,
lib/rails_panda_search/strategies/base.rb,
lib/rails_panda_search/strategies/ngram/ngram_entry.rb,
lib/rails_panda_search/strategies/ngram/ngram_indexer.rb,
lib/rails_panda_search/strategies/ngram/ngram_strategy.rb

Defined Under Namespace

Modules: Searchable, Strategies Classes: Config, Engine, Error

Constant Summary collapse

VERSION =
"1.0.3"

Class Method Summary collapse

Class Method Details

.configObject



72
73
74
# File 'lib/rails_panda_search/config.rb', line 72

def config
  @config ||= Config.new
end

.configure {|config| ... } ⇒ Object

Yields:



68
69
70
# File 'lib/rails_panda_search/config.rb', line 68

def configure
  yield config
end

.reindex_search_all!Object

Reindex all records across ALL searchable models.



39
40
41
# File 'lib/rails_panda_search/search.rb', line 39

def reindex_search_all!
  searchable_models.each(&:reindex_search_all!)
end

.search_for(query) ⇒ Object

Search across ALL models that use can_search_in. Returns a Hash of { "ModelName" => ActiveRecord::Relation, ... }

Dispatches to all active strategies and merges results.

RailsPanda::Search.search_for("alice")
# => { "User" => #<ActiveRecord::Relation [...]>, "Contact" => #<ActiveRecord::Relation [...]> }


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rails_panda_search/search.rb', line 18

def search_for(query)
  all_matches = config.active_strategy_classes.flat_map do |strategy|
    strategy.search(query)
  end

  return {} if all_matches.empty?

  models_by_name = searchable_models.index_by(&:name)

  all_matches
    .group_by(&:first)
    .each_with_object({}) do |(source_type, pairs), results|
      model = models_by_name[source_type]
      next unless model

      pk_hashes = pairs.map(&:last).uniq
      results[source_type] = model.send(:_relation_from_pk_hashes, pk_hashes)
    end
end