Module: Runestone

Defined in:
lib/runestone.rb,
lib/runestone/version.rb

Defined Under Namespace

Modules: ActiveRecord, Corpus, PsqlSchemaDumper Classes: Engine, IndexingJob, Model, Settings, WebSearch

Constant Summary collapse

VERSION =
'2.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_synonym(word, *replacements) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/runestone.rb', line 40

def self.add_synonym(word, *replacements)
  word = normalize(word)
  replacements.map! { |r| normalize(r) }

  word = word.split(/\s+/)
  last = word.pop

  syn = synonyms
  word.each do |w|
    syn = if syn.has_key?(w) && h = syn[w].find { |i| i.is_a?(Hash) }
      h
    else
      h = {}
      syn[w] ||= []
      syn[w] << h
      h
    end
  end

  syn[last] ||= []
  syn[last] += replacements
  syn[last].uniq!
end

.add_synonyms(dictionary) ⇒ Object



34
35
36
37
38
# File 'lib/runestone.rb', line 34

def self.add_synonyms(dictionary)
  dictionary.each do |k, v|
    add_synonym(k, *v)
  end
end

.normalize(string) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/runestone.rb', line 20

def self.normalize(string)
  string = string.downcase
  string = string.unicode_normalize!
  string
rescue Encoding::CompatibilityError
  string
end

.normalize!(string) ⇒ Object



28
29
30
31
32
# File 'lib/runestone.rb', line 28

def self.normalize!(string)
  string.downcase!
  string.unicode_normalize!
rescue Encoding::CompatibilityError
end

Instance Method Details

#search(query, dictionary: nil, prefix: :last, normalization: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/runestone.rb', line 64

def search(query, dictionary: nil, prefix: :last, normalization: nil)
  exact_search = Runestone::WebSearch.parse(query, prefix: prefix)
  typo_search = exact_search.typos
  syn_search = typo_search.synonymize
  
  tsqueries = [exact_search, typo_search, syn_search].map(&:to_s).uniq.map do |q|
    ts_query(q, dictionary: dictionary)
  end
  
  q = if select_values.empty?
    select(
      klass.arel_table[Arel.star],
      *tsqueries.each_with_index.map { |q, i| Arel::Nodes::As.new(ts_rank_cd(:vector, q, dictionary: dictionary, normalization: normalization), Arel::Nodes::SqlLiteral.new("rank#{i}")) }
    )
  else
    select(
      *tsqueries.each_with_index.map { |q, i| Arel::Nodes::As.new(ts_rank_cd(:vector, q, dictionary: dictionary, normalization: normalization), Arel::Nodes::SqlLiteral.new("rank#{i}")) }
    )
  end

  q = if klass == Runestone::Model
    q.where(ts_match(:vector, tsqueries.last, dictionary: dictionary))
  else
    q.joins(:runestones).where(ts_match(TS::Model.arel_table['vector'], tsqueries.last, dictionary: dictionary))
  end

  q = q.where(dictionary: dictionary) if dictionary
    
  q.order(
    *tsqueries.each_with_index.map { |q, i| Arel::Nodes::Descending.new(Arel::Nodes::SqlLiteral.new("rank#{i}")) }
  )
end