Module: Runestone
- Defined in:
- lib/runestone.rb,
lib/runestone/version.rb
Defined Under Namespace
Modules: ActiveRecord, Corpus Classes: Engine, IndexingJob, Model, Settings, WebSearch
Constant Summary collapse
- VERSION =
'1.0'
Class Method Summary collapse
- .add_synonym(word, *replacements) ⇒ Object
- .add_synonyms(dictionary) ⇒ Object
- .normalize(string) ⇒ Object
Instance Method Summary collapse
Class Method Details
.add_synonym(word, *replacements) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/runestone.rb', line 32 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
26 27 28 29 30 |
# File 'lib/runestone.rb', line 26 def self.add_synonyms(dictionary) dictionary.each do |k, v| add_synonym(k, *v) end end |
.normalize(string) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/runestone.rb', line 18 def self.normalize(string) string = string.downcase string = string.unicode_normalize! string rescue Encoding::CompatibilityError string end |
Instance Method Details
#search(query, dictionary: nil, prefix: :last) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/runestone.rb', line 56 def search(query, dictionary: nil, prefix: :last) 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), 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), 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 |