lsolr 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/lsolr.rb +38 -12
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b2f767515aadc312c4eb075eade0bec49647961b
4
- data.tar.gz: 5783b321efaf0866d3fe2561734adb41be08631e
3
+ metadata.gz: 05c41bbf755a942358b8103e341c059d3a062da1
4
+ data.tar.gz: 0a74f0bb97acf6dcc385433964d90cba915b84c0
5
5
  SHA512:
6
- metadata.gz: 58056c0bd27ea5974d9d8ba219312e896ca7e18cd844c61b4e189bc9ccb09cf404781f0d337f88a3a2c484b1b5ae26111c0dd863e66e2ba2c327beca54aa0a97
7
- data.tar.gz: 81a26a8ee3957c6cc2b45b1e153350407ce2079ef08dfbfe99acf3ce807fd21534478a32ab750e443eb645106b7ee63e0a7d2e9b7751ad10b66bf48d7180d2ba
6
+ metadata.gz: b97854d45e33b3f03dc64f1a985a892d3a94169d7a2a14d0970d9adb926094dbcc21cda9e878f4f5f7cd40678ddf2071bd8d5624312981f73926260b882eeb2a
7
+ data.tar.gz: 6e18db2a61a038353498e3cd4c1992a51e510d517aeecb448c05a4cb96321f08e046839ff58c48909fb4f338bfc9c4deaece64bdfd5f9fc73d5bb99749b0d4ae
@@ -84,11 +84,15 @@ class LSolr
84
84
  class << self
85
85
  # Builds composite query and returns builder instance.
86
86
  #
87
- # @param params [Hash{Symbol => String, Integer, Float, true, false, Range, Date, Time}] query terms
87
+ # @param params [Hash{Symbol => String, Integer, Float, true, false, Range, Date, Time}, String] query terms or a raw query
88
88
  #
89
89
  # @return [LSolr] a instance
90
90
  def build(params)
91
- params.map { |f, v| build_query(f, v) }.reduce { |a, e| a.and(e) }
91
+ case params
92
+ when Hash then params.map { |f, v| build_query(f, v) }.reduce { |a, e| a.and(e) }
93
+ when String then new.raw(params)
94
+ else raise TypeError, 'Could not build solr query. Please specify a Hash or String value.'
95
+ end
92
96
  end
93
97
 
94
98
  private
@@ -106,7 +110,7 @@ class LSolr
106
110
  end
107
111
 
108
112
  def build_array_query(field, values)
109
- return LSolr.new(field) if values.empty?
113
+ return new(field) if values.empty?
110
114
 
111
115
  values.map { |v| build_query(field, v) }.reduce { |a, e| a.or(e) }.wrap
112
116
  end
@@ -128,28 +132,29 @@ class LSolr
128
132
 
129
133
  # Create a new query builder instance.
130
134
  #
131
- # @param field [String] a field name
135
+ # @param field [String, Symbol] a field name
132
136
  # @return [LSolr] a instance
133
- def initialize(field)
134
- raise ArgumentError, 'Please specify a field name.' if field.nil? || field.empty?
135
-
137
+ def initialize(field = '')
136
138
  @expr_not = ''
137
- @field = field
139
+ @field = field.to_s
138
140
  @value = ''
139
141
  @range_first = ''
140
142
  @range_last = ''
141
143
  @boost = ''
142
144
  @left_parentheses = []
143
145
  @right_parentheses = []
146
+ @raw = ''
144
147
  end
145
148
 
146
149
  # Returns Apache Solr standard lucene type query string.
147
150
  #
148
151
  # @return [String] a stringified query
149
152
  def to_s # rubocop:disable Metrics/AbcSize
150
- @value = "#{@range_first} #{TO} #{@range_last}" if range_search?
151
- raise IncompleteQueryError, 'Please specify a search value.' if blank?
153
+ raise IncompleteQueryError, 'Please specify search field and value.' if blank?
152
154
 
155
+ return @raw unless @raw.empty?
156
+
157
+ @value = "#{@range_first} #{TO} #{@range_last}" if range_search?
153
158
  expr = "#{expr_not}#{left_parentheses.join}#{@field}:#{@value}#{right_parentheses.join}"
154
159
  expr = "#{prev} #{operator} #{expr}" if !prev.nil? && prev.present?
155
160
  "#{expr}#{@boost}"
@@ -157,11 +162,12 @@ class LSolr
157
162
 
158
163
  alias to_str to_s
159
164
 
160
- # A query is blank if value is empty in expression.
165
+ # A query is blank if term is incomplete in expression.
161
166
  #
162
167
  # @return [true, false]
163
168
  def blank?
164
- @value.empty? && (@range_first.empty? || @range_last.empty?)
169
+ managed_query_absence = @field.empty? || (@value.empty? && (@range_first.empty? || @range_last.empty?))
170
+ managed_query_absence && @raw.empty?
165
171
  end
166
172
 
167
173
  # A query is present if it's not blank.
@@ -171,6 +177,26 @@ class LSolr
171
177
  !blank?
172
178
  end
173
179
 
180
+ # Sets a field name.
181
+ #
182
+ # @param f [String, Symbol] a field name
183
+ #
184
+ # @return [LSolr] self instance
185
+ def field(f)
186
+ @field = f.to_s
187
+ self
188
+ end
189
+
190
+ # Sets a raw query.
191
+ #
192
+ # @param q [String] raw query
193
+ #
194
+ # @return [LSolr] self instance
195
+ def raw(q)
196
+ @raw = q.to_s
197
+ self
198
+ end
199
+
174
200
  # Adds parentheses to query expression.
175
201
  #
176
202
  # @see https://lucene.apache.org/solr/guide/7_1/the-standard-query-parser.html#grouping-terms-to-form-sub-queries Grouping Terms to Form Sub-Queries
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lsolr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taishi Kasuga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-18 00:00:00.000000000 Z
11
+ date: 2018-02-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: LSolr is a query builder of Apache Solr standard Lucene type query for
14
14
  Ruby.