Module: Runestone::ActiveRecord::RelationMethods

Defined in:
lib/runestone/active_record/relation_methods.rb

Instance Method Summary collapse

Instance Method Details

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



52
53
54
55
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
# File 'lib/runestone/active_record/relation_methods.rb', line 52

def search(query, dictionary: nil, prefix: nil, 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(Runestone::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

#ts_match(vector, query, dictionary: nil) ⇒ Object



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

def ts_match(vector, query, dictionary: nil)
  Arel::Nodes::TSMatch.new(
    ts_vector(vector, dictionary: dictionary),
    ts_query(query, dictionary: dictionary)
  )
end

#ts_query(query, dictionary: nil) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/runestone/active_record/relation_methods.rb', line 4

def ts_query(query, dictionary: nil)
  dictionary ||= Runestone.dictionary

  if query.is_a?(Arel::Nodes::TSQuery)
    query
  else
    Arel::Nodes::TSQuery.new(query, language: dictionary)
  end
end

#ts_rank(vector, query, dictionary: nil) ⇒ Object



35
36
37
38
39
40
# File 'lib/runestone/active_record/relation_methods.rb', line 35

def ts_rank(vector, query, dictionary: nil)
  Arel::Nodes::TSRank.new(
    ts_vector(vector, dictionary: dictionary),
    ts_query(query, dictionary: dictionary)
  )
end

#ts_rank_cd(vector, query, dictionary: nil, normalization: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/runestone/active_record/relation_methods.rb', line 42

def ts_rank_cd(vector, query, dictionary: nil, normalization: nil)
  normalization ||= Runestone.normalization
  
  Arel::Nodes::TSRankCD.new(
    ts_vector(vector, dictionary: dictionary),
    ts_query(query, dictionary: dictionary),
    normalization
  )
end

#ts_vector(column_name, dictionary: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/runestone/active_record/relation_methods.rb', line 14

def ts_vector(column_name, dictionary: nil)
  # if column_name.is_a?(String) || column_name.is_a?(Symbol)
  #   column = columns_hash[column_name.to_s]
  #   if column.type == :tsvector
  #     arel_table[column.name]
  #   else
  #     Arel::Nodes::TSVector.new(arel_table[column.name], language)
  #   end
  # else
  #   column_name
  # end
  Runestone::Model.arel_table[:vector]
end