lsolr 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lsolr.rb +46 -30
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e10967d4b5a602672070691c144b7471cd2b085ae08737a457101333540d82e2
|
4
|
+
data.tar.gz: d58e9b7d1975f3d6b5d799bb26985974ab134f3edb4794354cbb52ba2f7625f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 722047aaeded40aeafeac2f614c8eb7f2add58a8a51963a3a7ef9d2f32a4c0a3643daf7f2f7f42283a041cec18d58cdf1e5b6d7a5c66d071bf9ac2a602d590ff
|
7
|
+
data.tar.gz: 2cc6913b18142b476cf1edc47fbf7dc8ce15dcc674c838ea07045c93cbc2b13d8dab7f37ec2c5277878f64620c5352fc6311d1010f0535e8c12b956af50c2e2f
|
data/lib/lsolr.rb
CHANGED
@@ -62,7 +62,7 @@ require 'date'
|
|
62
62
|
# #=> 'NOT (field:a* OR field:b* OR field:c*)'
|
63
63
|
#
|
64
64
|
# @example How to use. Part 5:
|
65
|
-
# LSolr.build('a:1').and(
|
65
|
+
# LSolr.build('a:1').and(b: 2).to_s
|
66
66
|
# #=> 'a:1 AND b:2'
|
67
67
|
class LSolr
|
68
68
|
ArgumentError = Class.new(::ArgumentError)
|
@@ -110,7 +110,7 @@ class LSolr
|
|
110
110
|
def build(params)
|
111
111
|
case params
|
112
112
|
when Hash then params.map { |f, v| build_query(f, v) }.reduce { |a, e| a.and(e) }
|
113
|
-
when String then
|
113
|
+
when String then build_raw_query(params)
|
114
114
|
else raise TypeError, "Could not build solr query. Please specify a Hash or String value. #{params.inspect} given."
|
115
115
|
end
|
116
116
|
end
|
@@ -148,6 +148,10 @@ class LSolr
|
|
148
148
|
values.each { |v| last = v }
|
149
149
|
new(field).greater_than_or_equal_to(values.first).less_than_or_equal_to(last)
|
150
150
|
end
|
151
|
+
|
152
|
+
def build_raw_query(query)
|
153
|
+
query.empty? ? new : new.raw(query)
|
154
|
+
end
|
151
155
|
end
|
152
156
|
|
153
157
|
# Create a new query builder instance.
|
@@ -172,7 +176,7 @@ class LSolr
|
|
172
176
|
def to_s
|
173
177
|
raise IncompleteQueryError, 'Please specify a term of search.' if blank?
|
174
178
|
|
175
|
-
|
179
|
+
decorate_linked_expressions_if_needed(build_expression)
|
176
180
|
end
|
177
181
|
|
178
182
|
alias to_str to_s
|
@@ -206,19 +210,19 @@ class LSolr
|
|
206
210
|
|
207
211
|
# Sets a raw query.
|
208
212
|
#
|
209
|
-
# @param
|
213
|
+
# @param query [String] a raw query string
|
210
214
|
#
|
211
215
|
# @return [LSolr] self instance
|
212
|
-
def raw(
|
213
|
-
raise ArgumentError, "The raw query must be a not empty string value. #{
|
216
|
+
def raw(query)
|
217
|
+
raise ArgumentError, "The raw query must be a not empty string value. #{query.inspect} given." unless present_string?(query)
|
214
218
|
|
215
|
-
@raw =
|
219
|
+
@raw = query.to_s
|
216
220
|
self
|
217
221
|
end
|
218
222
|
|
219
223
|
# Adds parentheses to query expression.
|
220
224
|
#
|
221
|
-
# @see https://lucene.apache.org/solr/guide/
|
225
|
+
# @see https://lucene.apache.org/solr/guide/7_2/the-standard-query-parser.html#grouping-terms-to-form-sub-queries Grouping Terms to Form Sub-Queries
|
222
226
|
#
|
223
227
|
# @return [LSolr] copied self instance
|
224
228
|
def wrap
|
@@ -230,7 +234,7 @@ class LSolr
|
|
230
234
|
|
231
235
|
# Adds the boolean operator `NOT` to query expression.
|
232
236
|
#
|
233
|
-
# @see https://lucene.apache.org/solr/guide/
|
237
|
+
# @see https://lucene.apache.org/solr/guide/7_2/the-standard-query-parser.html#the-boolean-operator-not The Boolean Operator NOT ("!")
|
234
238
|
#
|
235
239
|
# @return [LSolr] self instance
|
236
240
|
def not
|
@@ -241,7 +245,7 @@ class LSolr
|
|
241
245
|
|
242
246
|
# Boosts a query expression.
|
243
247
|
#
|
244
|
-
# @see https://lucene.apache.org/solr/guide/
|
248
|
+
# @see https://lucene.apache.org/solr/guide/7_2/the-standard-query-parser.html#boosting-a-term-with Boosting a Term with "^"
|
245
249
|
#
|
246
250
|
# @param factor [Float] a boost factor number
|
247
251
|
#
|
@@ -255,7 +259,7 @@ class LSolr
|
|
255
259
|
|
256
260
|
# Specifies scoring result in expression.
|
257
261
|
#
|
258
|
-
# @see https://lucene.apache.org/solr/guide/
|
262
|
+
# @see https://lucene.apache.org/solr/guide/7_2/the-standard-query-parser.html#constant-score-with Constant Score with "^="
|
259
263
|
#
|
260
264
|
# @param score [Float] a constant score
|
261
265
|
#
|
@@ -298,7 +302,7 @@ class LSolr
|
|
298
302
|
|
299
303
|
# Builds a normal query expression with dates and times.
|
300
304
|
#
|
301
|
-
# @see https://lucene.apache.org/solr/guide/
|
305
|
+
# @see https://lucene.apache.org/solr/guide/7_2/working-with-dates.html Working with Dates
|
302
306
|
#
|
303
307
|
# @param value [String, Date, Time] a filter value
|
304
308
|
#
|
@@ -311,7 +315,7 @@ class LSolr
|
|
311
315
|
|
312
316
|
# Builds a prefix search query expression.
|
313
317
|
#
|
314
|
-
# @see https://lucene.apache.org/solr/guide/
|
318
|
+
# @see https://lucene.apache.org/solr/guide/7_2/the-standard-query-parser.html#wildcard-searches Wildcard Searches
|
315
319
|
#
|
316
320
|
# @param value [String] a search word
|
317
321
|
#
|
@@ -323,8 +327,8 @@ class LSolr
|
|
323
327
|
|
324
328
|
# Builds a phrase or proximity search query expression.
|
325
329
|
#
|
326
|
-
# @see https://lucene.apache.org/solr/guide/
|
327
|
-
# @see https://lucene.apache.org/solr/guide/
|
330
|
+
# @see https://lucene.apache.org/solr/guide/7_2/the-standard-query-parser.html#grouping-clauses-within-a-field Grouping Clauses within a Field
|
331
|
+
# @see https://lucene.apache.org/solr/guide/7_2/the-standard-query-parser.html#proximity-searches Proximity Searches
|
328
332
|
#
|
329
333
|
# @param values [Array<String>] search words
|
330
334
|
# @param distance [Integer] proximity distance
|
@@ -339,7 +343,7 @@ class LSolr
|
|
339
343
|
|
340
344
|
# Builds a fuzzy search query expression.
|
341
345
|
#
|
342
|
-
# @see https://lucene.apache.org/solr/guide/
|
346
|
+
# @see https://lucene.apache.org/solr/guide/7_2/the-standard-query-parser.html#fuzzy-searches Fuzzy Searches
|
343
347
|
#
|
344
348
|
# @param value [String] a search word
|
345
349
|
# @param distance [Float] a proximity distance
|
@@ -353,7 +357,7 @@ class LSolr
|
|
353
357
|
|
354
358
|
# Builds a range search query expression.
|
355
359
|
#
|
356
|
-
# @see https://lucene.apache.org/solr/guide/
|
360
|
+
# @see https://lucene.apache.org/solr/guide/7_2/the-standard-query-parser.html#range-searches Range Searches
|
357
361
|
#
|
358
362
|
# @param value [String, Integer, Date, Time] a filter value
|
359
363
|
#
|
@@ -365,7 +369,7 @@ class LSolr
|
|
365
369
|
|
366
370
|
# Builds a range search query expression.
|
367
371
|
#
|
368
|
-
# @see https://lucene.apache.org/solr/guide/
|
372
|
+
# @see https://lucene.apache.org/solr/guide/7_2/the-standard-query-parser.html#range-searches Range Searches
|
369
373
|
#
|
370
374
|
# @param value [String, Integer, Date, Time] a filter value
|
371
375
|
#
|
@@ -377,7 +381,7 @@ class LSolr
|
|
377
381
|
|
378
382
|
# Builds a range search query expression.
|
379
383
|
#
|
380
|
-
# @see https://lucene.apache.org/solr/guide/
|
384
|
+
# @see https://lucene.apache.org/solr/guide/7_2/the-standard-query-parser.html#range-searches Range Searches
|
381
385
|
#
|
382
386
|
# @param value [String, Integer, Date, Time] a filter value
|
383
387
|
#
|
@@ -389,7 +393,7 @@ class LSolr
|
|
389
393
|
|
390
394
|
# Builds a range search query expression.
|
391
395
|
#
|
392
|
-
# @see https://lucene.apache.org/solr/guide/
|
396
|
+
# @see https://lucene.apache.org/solr/guide/7_2/the-standard-query-parser.html#range-searches Range Searches
|
393
397
|
#
|
394
398
|
# @param value [String, Integer, Date, Time] a filter value
|
395
399
|
#
|
@@ -401,9 +405,9 @@ class LSolr
|
|
401
405
|
|
402
406
|
# Builds a composite query expression.
|
403
407
|
#
|
404
|
-
# @see https://lucene.apache.org/solr/guide/
|
408
|
+
# @see https://lucene.apache.org/solr/guide/7_2/the-standard-query-parser.html#the-boolean-operator-and The Boolean Operator AND ("&&")
|
405
409
|
#
|
406
|
-
# @param another [LSolr] another query builder instance
|
410
|
+
# @param another [LSolr, Hash, String] another query builder instance or query params or raw query string
|
407
411
|
#
|
408
412
|
# @return [LSolr] copied another query builder instance
|
409
413
|
def and(another)
|
@@ -412,9 +416,9 @@ class LSolr
|
|
412
416
|
|
413
417
|
# Builds a composite query expression.
|
414
418
|
#
|
415
|
-
# @see https://lucene.apache.org/solr/guide/
|
419
|
+
# @see https://lucene.apache.org/solr/guide/7_2/the-standard-query-parser.html#boolean-operators-supported-by-the-standard-query-parser Boolean Operators Supported by the Standard Query Parser
|
416
420
|
#
|
417
|
-
# @param another [LSolr] another query builder instance
|
421
|
+
# @param another [LSolr, Hash, String] another query builder instance or query params or raw query string
|
418
422
|
#
|
419
423
|
# @return [LSolr] copied another query builder instance
|
420
424
|
def or(another)
|
@@ -425,7 +429,7 @@ class LSolr
|
|
425
429
|
#
|
426
430
|
# @return [LSolr] a first term of query.
|
427
431
|
def head
|
428
|
-
if
|
432
|
+
if present_query?(prev)
|
429
433
|
prev.head
|
430
434
|
else
|
431
435
|
self
|
@@ -435,7 +439,7 @@ class LSolr
|
|
435
439
|
private
|
436
440
|
|
437
441
|
def initialize_copy(obj)
|
438
|
-
obj.prev = obj.prev.dup if
|
442
|
+
obj.prev = obj.prev.dup if present_query?(obj.prev)
|
439
443
|
obj.left_parentheses = obj.left_parentheses.dup
|
440
444
|
obj.right_parentheses = obj.right_parentheses.dup
|
441
445
|
end
|
@@ -456,6 +460,10 @@ class LSolr
|
|
456
460
|
!v.nil? && v.is_a?(Array) && !v.compact.empty? && v.map(&:to_s).map(&:empty?).none?
|
457
461
|
end
|
458
462
|
|
463
|
+
def present_query?(v)
|
464
|
+
!v.nil? && v.present?
|
465
|
+
end
|
466
|
+
|
459
467
|
def valid_boost_factor?(v)
|
460
468
|
(v.is_a?(Float) || v.is_a?(Integer)) && v > 0
|
461
469
|
end
|
@@ -491,7 +499,8 @@ class LSolr
|
|
491
499
|
end
|
492
500
|
|
493
501
|
def link(another, operator)
|
494
|
-
|
502
|
+
another = build_instance_if_needed(another)
|
503
|
+
return self unless present_query?(another)
|
495
504
|
|
496
505
|
another = another.dup
|
497
506
|
head = another.head
|
@@ -500,7 +509,14 @@ class LSolr
|
|
500
509
|
another
|
501
510
|
end
|
502
511
|
|
503
|
-
def
|
512
|
+
def build_instance_if_needed(another)
|
513
|
+
case another
|
514
|
+
when self.class then another
|
515
|
+
when Hash, String then self.class.build(another)
|
516
|
+
end
|
517
|
+
end
|
518
|
+
|
519
|
+
def build_expression
|
504
520
|
if raw?
|
505
521
|
@raw
|
506
522
|
elsif range_search?
|
@@ -510,9 +526,9 @@ class LSolr
|
|
510
526
|
end
|
511
527
|
end
|
512
528
|
|
513
|
-
def
|
529
|
+
def decorate_linked_expressions_if_needed(expr)
|
514
530
|
expr = "#{expr_not}#{left_parentheses.join}#{expr}#{right_parentheses.join}"
|
515
|
-
expr = "#{prev} #{operator} #{expr}" if
|
531
|
+
expr = "#{prev} #{operator} #{expr}" if present_query?(prev)
|
516
532
|
scoring = present_string?(@constant_score) ? @constant_score : @boost
|
517
533
|
"#{expr}#{scoring}"
|
518
534
|
end
|