Class: LSolr
- Inherits:
-
Object
- Object
- LSolr
- Defined in:
- lib/lsolr.rb
Overview
A query builder of Apache Solr standard Lucene type query for Ruby.
Constant Summary collapse
- ArgumentError =
Class.new(::ArgumentError)
- TypeError =
Class.new(::TypeError)
- IncompleteQueryError =
Class.new(StandardError)
- NOT =
'NOT'
- AND =
'AND'
- OR =
'OR'
- TO =
'TO'
- GREATER_THAN =
'{'
- LESS_THAN =
'}'
- GREATER_THAN_OR_EQUAL_TO =
'['
- LESS_THAN_OR_EQUAL_TO =
']'
- WILD_CARD =
'*'
- PROXIMITY =
'~'
- BOOST =
'^'
- CONSTANT_SCORE =
'^='
- DELIMITER_SPACE =
' '
- RANGE_FUZZY_MATCH_DISTANCE =
(0.0..2.0).freeze
- FORMAT_DATE_TIME =
'%Y-%m-%dT%H:%M:%SZ'
- FORMAT_MILLISECOND_FOR_DATE_TYPE =
'%Q'
- FORMAT_MILLISECOND_FOR_TIME_TYPE =
'%L'
- FORMAT_SECOND =
'%s'
- FORMAT_INSPECT =
'#<%<class>s:%<object>#018x `%<query>s`>'
- PARENTHESIS_LEFT =
'('
- PARENTHESIS_RIGHT =
')'
- RESERVED_SYMBOLS =
%w(- + & | ! ( ) { } [ ] ^ " ~ * ? : \\\\ /).freeze
- RESERVED_WORDS =
/(AND|OR|NOT)/.freeze
- REPLACEMENT_CHAR =
' '
Instance Attribute Summary collapse
-
#expr_not ⇒ Object
Returns the value of attribute expr_not.
-
#left_parentheses ⇒ Object
Returns the value of attribute left_parentheses.
-
#operator ⇒ Object
Returns the value of attribute operator.
-
#prev ⇒ Object
Returns the value of attribute prev.
-
#right_parentheses ⇒ Object
Returns the value of attribute right_parentheses.
Class Method Summary collapse
-
.build(params) ⇒ LSolr
Builds composite query and returns builder instance.
Instance Method Summary collapse
-
#and(another) ⇒ LSolr
Builds a composite query expression.
-
#blank? ⇒ true, false
A query is blank if term is incomplete in expression.
-
#boost(factor) ⇒ LSolr
Boosts a query expression.
-
#constant_score(score) ⇒ LSolr
Specifies scoring result in expression.
-
#date_time_match(value) ⇒ LSolr
Builds a normal query expression with dates and times.
-
#field(name) ⇒ LSolr
Sets a field name.
-
#fuzzy_match(value, distance: 2.0) ⇒ LSolr
Builds a fuzzy search query expression.
-
#greater_than(value) ⇒ LSolr
Builds a range search query expression.
-
#greater_than_or_equal_to(value) ⇒ LSolr
Builds a range search query expression.
-
#head ⇒ LSolr
Returns a first term of query.
-
#initialize(field_name = nil) ⇒ LSolr
constructor
Create a new query builder instance.
-
#inspect ⇒ String
Returns instance information.
-
#less_than(value) ⇒ LSolr
Builds a range search query expression.
-
#less_than_or_equal_to(value) ⇒ LSolr
Builds a range search query expression.
-
#match(value) ⇒ LSolr
Builds a normal query expression.
-
#match_in(values) ⇒ LSolr
Builds a normal multi value query expression.
-
#not ⇒ LSolr
Adds the boolean operator ‘NOT` to query expression.
-
#or(another) ⇒ LSolr
Builds a composite query expression.
-
#phrase_match(values, distance: 0) ⇒ LSolr
Builds a phrase or proximity search query expression.
-
#prefix_match(value) ⇒ LSolr
Builds a prefix search query expression.
-
#present? ⇒ true, false
A query is present if it’s not blank.
-
#raw(query) ⇒ LSolr
Sets a raw query.
-
#to_s ⇒ String
(also: #to_str)
Returns Apache Solr standard lucene type query string.
-
#wrap ⇒ LSolr
Adds parentheses to query expression.
Constructor Details
#initialize(field_name = nil) ⇒ LSolr
Create a new query builder instance.
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/lsolr.rb', line 165 def initialize(field_name = nil) if field_name.nil? @field = '' else field(field_name) end @expr_not = @value = @range_first = @range_last = @boost = @constant_score = @raw = '' @left_parentheses = [] @right_parentheses = [] end |
Instance Attribute Details
#expr_not ⇒ Object
Returns the value of attribute expr_not.
104 105 106 |
# File 'lib/lsolr.rb', line 104 def expr_not @expr_not end |
#left_parentheses ⇒ Object
Returns the value of attribute left_parentheses.
104 105 106 |
# File 'lib/lsolr.rb', line 104 def left_parentheses @left_parentheses end |
#operator ⇒ Object
Returns the value of attribute operator.
104 105 106 |
# File 'lib/lsolr.rb', line 104 def operator @operator end |
#prev ⇒ Object
Returns the value of attribute prev.
104 105 106 |
# File 'lib/lsolr.rb', line 104 def prev @prev end |
#right_parentheses ⇒ Object
Returns the value of attribute right_parentheses.
104 105 106 |
# File 'lib/lsolr.rb', line 104 def right_parentheses @right_parentheses end |
Class Method Details
.build(params) ⇒ LSolr
Builds composite query and returns builder instance.
114 115 116 117 118 119 120 121 122 |
# File 'lib/lsolr.rb', line 114 def build(params) case params when Hash then params.map { |f, v| build_query(f, v) }.reduce { |a, e| a.and(e) } when String then build_raw_query(params) else raise TypeError, "Could not build solr query. Please specify a Hash or String value. `#{params}` given." end rescue TypeError => e raise ArgumentError, "#{e.} It is not a supported type." end |
Instance Method Details
#and(another) ⇒ LSolr
Builds a composite query expression.
448 449 450 |
# File 'lib/lsolr.rb', line 448 def and(another) link(another, AND) end |
#blank? ⇒ true, false
A query is blank if term is incomplete in expression.
202 203 204 205 |
# File 'lib/lsolr.rb', line 202 def blank? managed_query_absence = @field.empty? || (@value.empty? && (@range_first.empty? || @range_last.empty?)) managed_query_absence && @raw.empty? end |
#boost(factor) ⇒ LSolr
Boosts a query expression.
274 275 276 277 278 279 |
# File 'lib/lsolr.rb', line 274 def boost(factor) raise ArgumentError, "The boost factor must be a positive number. `#{factor}` given." unless valid_boost_factor?(factor) @boost = "#{BOOST}#{factor}" self end |
#constant_score(score) ⇒ LSolr
Specifies scoring result in expression.
290 291 292 293 294 295 |
# File 'lib/lsolr.rb', line 290 def constant_score(score) raise ArgumentError, "The constant score must be a number. `#{score}` given." unless valid_score?(score) @constant_score = "#{CONSTANT_SCORE}#{score}" self end |
#date_time_match(value) ⇒ LSolr
Builds a normal query expression with dates and times.
342 343 344 345 346 |
# File 'lib/lsolr.rb', line 342 def date_time_match(value) value = stringify(value, symbols: RESERVED_SYMBOLS - %w[- : . / +]) @value = %("#{value}") self end |
#field(name) ⇒ LSolr
Sets a field name.
221 222 223 224 225 226 |
# File 'lib/lsolr.rb', line 221 def field(name) raise ArgumentError, "The field name must be a not empty string value. `#{name}` given." unless present_string?(name) @field = name.to_s self end |
#fuzzy_match(value, distance: 2.0) ⇒ LSolr
Builds a fuzzy search query expression.
386 387 388 389 390 391 |
# File 'lib/lsolr.rb', line 386 def fuzzy_match(value, distance: 2.0) raise ArgumentError, "Out of #{RANGE_FUZZY_MATCH_DISTANCE}. `#{distance}` given." unless valid_fuzzy_match_distance?(distance) @value = "#{clean(value).split.join}#{PROXIMITY}#{distance}" self end |
#greater_than(value) ⇒ LSolr
Builds a range search query expression.
400 401 402 403 |
# File 'lib/lsolr.rb', line 400 def greater_than(value) @range_first = "#{GREATER_THAN}#{stringify(value)}" self end |
#greater_than_or_equal_to(value) ⇒ LSolr
Builds a range search query expression.
424 425 426 427 |
# File 'lib/lsolr.rb', line 424 def greater_than_or_equal_to(value) @range_first = "#{GREATER_THAN_OR_EQUAL_TO}#{stringify(value)}" self end |
#head ⇒ LSolr
Returns a first term of query.
466 467 468 469 470 471 472 |
# File 'lib/lsolr.rb', line 466 def head if present_query?(prev) prev.head else self end end |
#inspect ⇒ String
Returns instance information.
193 194 195 196 197 |
# File 'lib/lsolr.rb', line 193 def inspect format(FORMAT_INSPECT, class: self.class.name, object: object_id << 1, query: present? ? to_s : '') end |
#less_than(value) ⇒ LSolr
Builds a range search query expression.
412 413 414 415 |
# File 'lib/lsolr.rb', line 412 def less_than(value) @range_last = "#{stringify(value)}#{LESS_THAN}" self end |
#less_than_or_equal_to(value) ⇒ LSolr
Builds a range search query expression.
436 437 438 439 |
# File 'lib/lsolr.rb', line 436 def less_than_or_equal_to(value) @range_last = "#{stringify(value)}#{LESS_THAN_OR_EQUAL_TO}" self end |
#match(value) ⇒ LSolr
Builds a normal query expression.
304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/lsolr.rb', line 304 def match(value) raise ArgumentError, "`#{value}` given. It must be a not empty value." unless present_string?(value.to_s) return match_in(value) if value.is_a?(Array) return date_time_match(value) if value.is_a?(Date) || value.is_a?(Time) values = clean(value).split if values.size > 1 phrase_match(values) else @value = values.join self end end |
#match_in(values) ⇒ LSolr
Builds a normal multi value query expression.
326 327 328 329 330 331 332 333 |
# File 'lib/lsolr.rb', line 326 def match_in(values) raise ArgumentError, "`#{values}` given. It must be a not empty array." unless present_array?(values) return match(values.first) if values.size == 1 values = values.map { |v| clean(v) } @value = "(#{values.join(DELIMITER_SPACE)})" self end |
#not ⇒ LSolr
Adds the boolean operator ‘NOT` to query expression.
259 260 261 262 263 |
# File 'lib/lsolr.rb', line 259 def not this = dup this.head.expr_not = "#{NOT} " this end |
#or(another) ⇒ LSolr
Builds a composite query expression.
459 460 461 |
# File 'lib/lsolr.rb', line 459 def or(another) link(another, OR) end |
#phrase_match(values, distance: 0) ⇒ LSolr
Builds a phrase or proximity search query expression.
369 370 371 372 373 374 |
# File 'lib/lsolr.rb', line 369 def phrase_match(values, distance: 0) value = values.map { |v| clean(v).split }.flatten.join(DELIMITER_SPACE) proximity_match = distance.to_s.to_i > 0 ? "#{PROXIMITY}#{distance}" : '' @value = %("#{value}"#{proximity_match}) self end |
#prefix_match(value) ⇒ LSolr
Builds a prefix search query expression.
355 356 357 358 |
# File 'lib/lsolr.rb', line 355 def prefix_match(value) @value = clean(value, symbols: RESERVED_SYMBOLS - %w[* ?]).split.join(WILD_CARD) self end |
#present? ⇒ true, false
A query is present if it’s not blank.
210 211 212 |
# File 'lib/lsolr.rb', line 210 def present? !blank? end |
#raw(query) ⇒ LSolr
Sets a raw query.
235 236 237 238 239 240 |
# File 'lib/lsolr.rb', line 235 def raw(query) raise ArgumentError, "The raw query must be a not empty string value. `#{query}` given." unless present_string?(query) @raw = query.to_s self end |
#to_s ⇒ String Also known as: to_str
Returns Apache Solr standard lucene type query string.
182 183 184 185 186 |
# File 'lib/lsolr.rb', line 182 def to_s raise IncompleteQueryError, 'Please specify a term of search.' if blank? decorate_linked_expressions_if_needed(build_expression) end |
#wrap ⇒ LSolr
Adds parentheses to query expression.
247 248 249 250 251 252 |
# File 'lib/lsolr.rb', line 247 def wrap this = dup this.head.left_parentheses << PARENTHESIS_LEFT this.right_parentheses << PARENTHESIS_RIGHT this end |