Class: RailsPanda::Search::Strategies::Ngram::NgramStrategy

Inherits:
Base
  • Object
show all
Defined in:
lib/rails_panda_search/strategies/ngram/ngram_strategy.rb

Overview

N-gram search strategy implementation. Conforms to the Strategies::Base interface.

Breaks text into n-character substrings and stores them in a local ActiveRecord table for fast substring matching.

Uses the default Base#sync_record! (remove + index).

Class Method Summary collapse

Methods inherited from Base

sync_record!

Class Method Details

.clear_for_source_type!(source_type) ⇒ Object



29
30
31
# File 'lib/rails_panda_search/strategies/ngram/ngram_strategy.rb', line 29

def clear_for_source_type!(source_type)
  NgramEntry.for_source_type(source_type).delete_all
end

.index_record!(record, columns) ⇒ Object



21
22
23
# File 'lib/rails_panda_search/strategies/ngram/ngram_strategy.rb', line 21

def index_record!(record, columns)
  NgramIndexer.index_record!(record, columns)
end

.remove_record!(record) ⇒ Object



25
26
27
# File 'lib/rails_panda_search/strategies/ngram/ngram_strategy.rb', line 25

def remove_record!(record)
  NgramIndexer.remove_record!(record)
end

.search(query, source_type: nil, columns: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/rails_panda_search/strategies/ngram/ngram_strategy.rb', line 33

def search(query, source_type: nil, columns: nil)
  ngrams = NgramIndexer.ngrams_for(query)
  return [] if ngrams.empty?

  NgramEntry.matching_source_ids(
    ngrams,
    source_type: source_type,
    columns: columns
  )
end