Module: ParadeDB::Arel::Predications

Defined in:
lib/parade_db/arel/predications.rb

Constant Summary collapse

TOKENIZER_EXPRESSION =
/\A[a-zA-Z_][a-zA-Z0-9_]*(?:(?:::|\.)[a-zA-Z_][a-zA-Z0-9_]*)*(?:\(\s*[a-zA-Z0-9_'".,=\s:-]*\s*\))?\z/.freeze
BUILDER =
Builder.new.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.install!Object



230
231
232
233
234
# File 'lib/parade_db/arel/predications.rb', line 230

def install!
  return if ::Arel::Predications.ancestors.include?(ParadeDB::Arel::Predications)

  ::Arel::Predications.include(ParadeDB::Arel::Predications)
end

Instance Method Details

#pdb_allObject



82
83
84
85
# File 'lib/parade_db/arel/predications.rb', line 82

def pdb_all
  rhs = ::Arel::Nodes::NamedFunction.new("pdb.all", [])
  ::Arel::Nodes::InfixOperation.new("@@@", self, rhs)
end

#pdb_existsObject



87
88
89
90
# File 'lib/parade_db/arel/predications.rb', line 87

def pdb_exists
  rhs = ::Arel::Nodes::NamedFunction.new("pdb.exists", [])
  ::Arel::Nodes::InfixOperation.new("@@@", self, rhs)
end

#pdb_full_text(expression) ⇒ Object



26
27
28
29
# File 'lib/parade_db/arel/predications.rb', line 26

def pdb_full_text(expression)
  rhs = expression.is_a?(::Arel::Nodes::Node) ? expression : ::Arel.sql(expression.to_s)
  ::Arel::Nodes::InfixOperation.new("@@@", self, rhs)
end

#pdb_match(*terms, tokenizer: nil, distance: nil, prefix: nil, transposition_cost_one: nil, boost: nil) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/parade_db/arel/predications.rb', line 10

def pdb_match(*terms, tokenizer: nil, distance: nil, prefix: nil, transposition_cost_one: nil, boost: nil)
  rhs = pdb_quoted(pdb_join_terms(terms))
  rhs = pdb_apply_fuzzy(rhs, distance: distance, prefix: prefix, transposition_cost_one: transposition_cost_one)
  rhs = pdb_apply_tokenizer(rhs, tokenizer)
  rhs = pdb_apply_boost(rhs, boost)
  ::Arel::Nodes::InfixOperation.new("&&&", self, rhs)
end

#pdb_match_any(*terms, tokenizer: nil, distance: nil, prefix: nil, transposition_cost_one: nil, boost: nil) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/parade_db/arel/predications.rb', line 18

def pdb_match_any(*terms, tokenizer: nil, distance: nil, prefix: nil, transposition_cost_one: nil, boost: nil)
  rhs = pdb_quoted(pdb_join_terms(terms))
  rhs = pdb_apply_fuzzy(rhs, distance: distance, prefix: prefix, transposition_cost_one: transposition_cost_one)
  rhs = pdb_apply_tokenizer(rhs, tokenizer)
  rhs = pdb_apply_boost(rhs, boost)
  ::Arel::Nodes::InfixOperation.new("|||", self, rhs)
end

#pdb_more_like_this(key, fields: nil, options: {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/parade_db/arel/predications.rb', line 100

def pdb_more_like_this(key, fields: nil, options: {})
  args = [pdb_quoted(key)]

  unless fields.nil?
    field_values = Array(fields).map { |field| pdb_quoted(field.to_s) }
    args << Nodes::ArrayLiteral.new(field_values)
  end

  options.each do |name, value|
    key_node = ::Arel::Nodes::SqlLiteral.new(name.to_s)
    rendered_value =
      if value.is_a?(Array)
        Nodes::ArrayLiteral.new(Array(value).map { |term| pdb_quoted(term.to_s) })
      else
        pdb_quoted(value)
      end
    args << ::Arel::Nodes::InfixOperation.new("=>", key_node, rendered_value)
  end

  rhs = ::Arel::Nodes::NamedFunction.new("pdb.more_like_this", args)
  ::Arel::Nodes::InfixOperation.new("@@@", self, rhs)
end

#pdb_near(proximity, boost: nil, const: nil) ⇒ Object



55
56
57
# File 'lib/parade_db/arel/predications.rb', line 55

def pdb_near(proximity, boost: nil, const: nil)
  BUILDER.near(self, proximity, boost: boost, const: const)
end

#pdb_parse(query, lenient: nil, conjunction_mode: nil) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/parade_db/arel/predications.rb', line 73

def pdb_parse(query, lenient: nil, conjunction_mode: nil)
  rhs = Nodes::ParseNode.new(
    pdb_quoted(query),
    lenient: lenient,
    conjunction_mode: conjunction_mode
  )
  ::Arel::Nodes::InfixOperation.new("@@@", self, rhs)
end

#pdb_phrase(text, slop: nil, tokenizer: nil) ⇒ Object



31
32
33
# File 'lib/parade_db/arel/predications.rb', line 31

def pdb_phrase(text, slop: nil, tokenizer: nil)
  BUILDER.phrase(self, text, slop: slop, tokenizer: tokenizer)
end

#pdb_phrase_prefix(*terms, max_expansion: nil) ⇒ Object

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/parade_db/arel/predications.rb', line 59

def pdb_phrase_prefix(*terms, max_expansion: nil)
  flat = terms.flatten.compact
  raise ArgumentError, "phrase_prefix requires at least one term" if flat.empty?

  array = Nodes::ArrayLiteral.new(flat.map { |term| pdb_quoted(term) })
  args = [array]
  unless max_expansion.nil?
    pdb_validate_integer!(max_expansion, :max_expansion)
    args << pdb_quoted(max_expansion)
  end
  rhs = ::Arel::Nodes::NamedFunction.new("pdb.phrase_prefix", args)
  ::Arel::Nodes::InfixOperation.new("@@@", self, rhs)
end

#pdb_range(value = nil, gte: nil, gt: nil, lte: nil, lt: nil, type: nil) ⇒ Object



92
93
94
# File 'lib/parade_db/arel/predications.rb', line 92

def pdb_range(value = nil, gte: nil, gt: nil, lte: nil, lt: nil, type: nil)
  BUILDER.range(self, value, gte: gte, gt: gt, lte: lte, lt: lt, type: type)
end

#pdb_range_term(value, relation: nil, range_type: nil) ⇒ Object



96
97
98
# File 'lib/parade_db/arel/predications.rb', line 96

def pdb_range_term(value, relation: nil, range_type: nil)
  BUILDER.range_term(self, value, relation: relation, range_type: range_type)
end

#pdb_regex(pattern) ⇒ Object



46
47
48
49
# File 'lib/parade_db/arel/predications.rb', line 46

def pdb_regex(pattern)
  rhs = ::Arel::Nodes::NamedFunction.new("pdb.regex", [pdb_quoted(pattern)])
  ::Arel::Nodes::InfixOperation.new("@@@", self, rhs)
end

#pdb_regex_phrase(*patterns, slop: nil, max_expansions: nil) ⇒ Object



51
52
53
# File 'lib/parade_db/arel/predications.rb', line 51

def pdb_regex_phrase(*patterns, slop: nil, max_expansions: nil)
  BUILDER.regex_phrase(self, *patterns, slop: slop, max_expansions: max_expansions)
end

#pdb_scoreObject



123
124
125
# File 'lib/parade_db/arel/predications.rb', line 123

def pdb_score
  ::Arel::Nodes::NamedFunction.new("pdb.score", [self])
end

#pdb_snippet(*args) ⇒ Object



127
128
129
# File 'lib/parade_db/arel/predications.rb', line 127

def pdb_snippet(*args)
  ::Arel::Nodes::NamedFunction.new("pdb.snippet", [self] + args.map { |arg| pdb_quoted(arg) })
end

#pdb_snippet_positionsObject



150
151
152
# File 'lib/parade_db/arel/predications.rb', line 150

def pdb_snippet_positions
  ::Arel::Nodes::NamedFunction.new("pdb.snippet_positions", [self])
end

#pdb_snippets(start_tag: nil, end_tag: nil, max_num_chars: nil, limit: nil, offset: nil, sort_by: nil) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/parade_db/arel/predications.rb', line 131

def pdb_snippets(
  start_tag: nil,
  end_tag: nil,
  max_num_chars: nil,
  limit: nil,
  offset: nil,
  sort_by: nil
)
  BUILDER.snippets(
    self,
    start_tag: start_tag,
    end_tag: end_tag,
    max_num_chars: max_num_chars,
    limit: limit,
    offset: offset,
    sort_by: sort_by
  )
end

#pdb_term(term, distance: nil, prefix: nil, transposition_cost_one: nil, boost: nil) ⇒ Object



35
36
37
38
39
40
# File 'lib/parade_db/arel/predications.rb', line 35

def pdb_term(term, distance: nil, prefix: nil, transposition_cost_one: nil, boost: nil)
  rhs = pdb_quoted(term)
  rhs = pdb_apply_fuzzy(rhs, distance: distance, prefix: prefix, transposition_cost_one: transposition_cost_one)
  rhs = pdb_apply_boost(rhs, boost)
  ::Arel::Nodes::InfixOperation.new("===", self, rhs)
end

#pdb_term_set(*terms) ⇒ Object



42
43
44
# File 'lib/parade_db/arel/predications.rb', line 42

def pdb_term_set(*terms)
  BUILDER.term_set(self, *terms)
end