Class: Solr::Request::Standard

Inherits:
Select show all
Defined in:
lib/solr/request/standard.rb

Overview

The ASF licenses this file to You under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Direct Known Subclasses

Dismax

Constant Summary collapse

VALID_PARAMS =
[:query, :sort, :default_field, :operator, :start, :rows, :shards, :date_facets,
:filter_queries, :field_list, :debug_query, :explain_other, :facets, :highlighting, :mlt]

Instance Attribute Summary

Attributes inherited from Select

#query_type

Instance Method Summary collapse

Methods inherited from Select

#content_type, #handler, #response_format, #to_s

Methods inherited from Base

#content_type, #handler, #response_format

Constructor Details

#initialize(params) ⇒ Standard

Returns a new instance of Standard.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/solr/request/standard.rb', line 18

def initialize(params)
  super('standard')
  
  raise "Invalid parameters: #{(params.keys - VALID_PARAMS).join(',')}" unless 
    (params.keys - VALID_PARAMS).empty?
  
  raise ":query parameter required" unless params[:query]
  
  @params = params.dup
  
  # Validate operator
  if params[:operator]
    raise "Only :and/:or operators allowed" unless 
      [:and, :or].include?(params[:operator])
      
    @params[:operator] = params[:operator].to_s.upcase
  end

  # Validate start, rows can be transformed to ints
  @params[:start] = params[:start].to_i if params[:start]
  @params[:rows] = params[:rows].to_i if params[:rows]
  
  @params[:field_list] ||= ["*","score"]
  
  @params[:shards] ||= []
end

Instance Method Details

#to_hashObject



45
46
47
48
49
50
51
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/solr/request/standard.rb', line 45

def to_hash
  hash = {}
  
  # standard request param processing
  sort = @params[:sort].collect do |sort|
    key = sort.keys[0]
    "#{key.to_s} #{sort[key] == :descending ? 'desc' : 'asc'}"
  end.join(',') if @params[:sort]
  hash[:q] = @params[:query]
  hash[:sort] = sort if sort
  hash["q.op"] = @params[:operator]
  hash[:df] = @params[:default_field]

  # common parameter processing
  hash[:start] = @params[:start]
  hash[:rows] = @params[:rows]
  hash[:fq] = @params[:filter_queries]
  hash[:fl] = @params[:field_list].join(',')
  hash[:debugQuery] = @params[:debug_query]
  hash[:explainOther] = @params[:explain_other]
  hash[:shards] = @params[:shards].join(',') unless @params[:shards].empty?
  
  # facet parameter processing
  if @params[:facets]
    # TODO need validation of all that is under the :facets Hash too
    hash[:facet] = true
    hash["facet.field"] = []
    hash["facet.query"] = @params[:facets][:queries]
    hash["facet.sort"] = (@params[:facets][:sort] == :count) if @params[:facets][:sort]
    hash["facet.limit"] = @params[:facets][:limit]
    hash["facet.missing"] = @params[:facets][:missing]
    hash["facet.mincount"] = @params[:facets][:mincount]
    hash["facet.prefix"] = @params[:facets][:prefix]
    hash["facet.offset"] = @params[:facets][:offset]
    if @params[:facets][:fields]  # facet fields are optional (could be facet.query only)
      @params[:facets][:fields].each do |f|
        if f.kind_of? Hash
          key = f.keys[0]
          value = f[key]
          hash["facet.field"] << key
          hash["f.#{key}.facet.sort"] = (value[:sort] == :count) if value[:sort]
          hash["f.#{key}.facet.limit"] = value[:limit]
          hash["f.#{key}.facet.missing"] = value[:missing]
          hash["f.#{key}.facet.mincount"] = value[:mincount]
          hash["f.#{key}.facet.prefix"] = value[:prefix]
          hash["f.#{key}.facet.offset"] = value[:offset]
        else
          hash["facet.field"] << f
        end
      end
    end
    
    if @params[:date_facets]
      hash["facet.date"] = []
      if @params[:date_facets][:fields]
        @params[:date_facets][:fields].each do |f|
          if f.kind_of? Hash
            key = f.keys[0]
            hash["facet.date"] << key
            f[key].each { |k, v|
              hash["f.#{key}.facet.date.#{k}"] = v
            }
          else
            hash["facet.date"] << f
          end
        end
      end
      hash["facet.date.start"] = @params[:date_facets][:start]
      hash["facet.date.end"] = @params[:date_facets][:end]
      hash["facet.date.gap"] = @params[:date_facets][:gap]
      hash["facet.date.other"] = @params[:date_facets][:other]
      hash["facet.date.hardend"] = @params[:date_facets][:hardend]
      if @params[:date_facets][:filter]
        if hash[:fq]
          hash[:fq] << @params[:date_facets][:filter]
        else
          hash[:fq] = @params[:date_facets][:filter]
        end
      end
    end
  end
  
  # highlighting parameter processing - http://wiki.apache.org/solr/HighlightingParameters
  if @params[:highlighting]
    hash[:hl] = true
    hash["hl.fl"] = @params[:highlighting][:field_list].join(',') if @params[:highlighting][:field_list]

    snippets = @params[:highlighting][:max_snippets]
    if snippets
      if snippets.kind_of? Hash
        if snippets[:default]
          hash["hl.snippets"] = snippets[:default]
        end
        if snippets[:fields]
          snippets[:fields].each do |k,v|
            hash["f.#{k}.hl.snippets"] = v
          end
        end
      else
        hash["hl.snippets"] = snippets
      end
    end

    fragsize = @params[:highlighting][:fragment_size]
    if fragsize
      if fragsize.kind_of? Hash
        if fragsize[:default]
          hash["hl.fragsize"] = fragsize[:default]
        end
        if fragsize[:fields]
          fragsize[:fields].each do |k,v|
            hash["f.#{k}.hl.fragsize"] = v
          end
        end
      else
        hash["hl.fragsize"] = fragsize
      end
    end

    rfm = @params[:highlighting][:require_field_match]
    if nil != rfm
      if rfm.kind_of? Hash
        if nil != rfm[:default]
          hash["hl.requireFieldMatch"] = rfm[:default]
        end
        if rfm[:fields]
          rfm[:fields].each do |k,v|
            hash["f.#{k}.hl.requireFieldMatch"] = v
          end
        end
      else
        hash["hl.requireFieldMatch"] = rfm
      end
    end

    mac = @params[:highlighting][:max_analyzed_chars]
    if mac
      if mac.kind_of? Hash
        if mac[:default]
          hash["hl.maxAnalyzedChars"] = mac[:default]
        end
        if mac[:fields]
          mac[:fields].each do |k,v|
            hash["f.#{k}.hl.maxAnalyzedChars"] = v
          end
        end
      else
        hash["hl.maxAnalyzedChars"] = mac
      end
    end

    prefix = @params[:highlighting][:prefix]
    if prefix
      if prefix.kind_of? Hash
        if prefix[:default]
          hash["hl.simple.pre"] = prefix[:default]
        end
        if prefix[:fields]
          prefix[:fields].each do |k,v|
            hash["f.#{k}.hl.simple.pre"] = v
          end
        end
      else
        hash["hl.simple.pre"] = prefix
      end
    end

    suffix = @params[:highlighting][:suffix]
    if suffix
      if suffix.kind_of? Hash
        if suffix[:default]
          hash["hl.simple.post"] = suffix[:default]
        end
        if suffix[:fields]
          suffix[:fields].each do |k,v|
            hash["f.#{k}.hl.simple.post"] = v
          end
        end
      else
        hash["hl.simple.post"] = suffix
      end
    end

    formatter = @params[:highlighting][:formatter]
    if formatter
      if formatter.kind_of? Hash
        if formatter[:default]
          hash["hl.formatter"] = formatter[:default]
        end
        if formatter[:fields]
          formatter[:fields].each do |k,v|
            hash["f.#{k}.hl.formatter"] = v
          end
        end
      else
        hash["hl.formatter"] = formatter
      end
    end

    fragmenter = @params[:highlighting][:fragmenter]
    if fragmenter
      if fragmenter.kind_of? Hash
        if fragmenter[:default]
          hash["hl.fragmenter"] = fragmenter[:default]
        end
        if fragmenter[:fields]
          fragmenter[:fields].each do |k,v|
            hash["f.#{k}.hl.fragmenter"] = v
          end
        end
      else
        hash["hl.fragmenter"] = fragmenter
      end
    end

    merge_contiguous = @params[:highlighting][:merge_contiguous]
    if nil != merge_contiguous
      if merge_contiguous.kind_of? Hash
        if nil != merge_contiguous[:default]
          hash["hl.mergeContiguous"] = merge_contiguous[:default]
        end
        if merge_contiguous[:fields]
          merge_contiguous[:fields].each do |k,v|
            hash["f.#{k}.hl.mergeContiguous"] = v
          end
        end
      else
        hash["hl.mergeContiguous"] = merge_contiguous
      end
    end

    increment = @params[:highlighting][:increment]
    if increment
      if increment.kind_of? Hash
        if increment[:default]
          hash["hl.increment"] = increment[:default]
        end
        if increment[:fields]
          increment[:fields].each do |k,v|
            hash["f.#{k}.hl.increment"] = v
          end
        end
      else
        hash["hl.increment"] = increment
      end
    end

    # support "old style"
    alternate_fields = @params[:highlighting][:alternate_fields]
    if alternate_fields
      alternate_fields.each do |f,v|
        hash["f.#{f}.hl.alternateField"] = v
      end
    end

    alternate_field = @params[:highlighting][:alternate_field]
    if alternate_field
      if alternate_field.kind_of? Hash
        if alternate_field[:default]
          hash["hl.alternateField"] = alternate_field[:default]
        end
        if alternate_field[:fields]
          alternate_field[:fields].each do |k,v|
            hash["f.#{k}.hl.alternateField"] = v
          end
        end
      else
        hash["hl.alternateField"] = alternate_field
      end
    end

    mafl = @params[:highlighting][:max_alternate_field_length]
    if mafl
      if mafl.kind_of? Hash
        if mafl[:default]
          hash["hl.maxAlternateFieldLength"] = mafl[:default]
        end
        if mafl[:fields]
          mafl[:fields].each do |k,v|
            hash["f.#{k}.hl.maxAlternateFieldLength"] = v
          end
        else
          # support "old style"
          mafl.each do |k,v|
            hash["f.#{k}.hl.maxAlternateFieldLength"] = v
          end
        end
      else
        hash["hl.maxAlternateFieldLength"] = mafl
      end
    end

    hash["hl.usePhraseHighlighter"] = @params[:highlighting][:use_phrase_highlighter]

    regex = @params[:highlighting][:regex]
    if regex
      if regex[:slop]
        if regex[:slop].kind_of? Hash
          if regex[:slop][:default]
            hash["hl.regex.slop"] = regex[:slop][:default]
          end
          if regex[:slop][:fields]
            regex[:slop][:fields].each do |k,v|
              hash["f.#{k}.hl.regex.slop"] = v
            end
          end
        else
          hash["hl.regex.slop"] = regex[:slop]
        end
      end
      if regex[:pattern]
        if regex[:pattern].kind_of? Hash
          if regex[:pattern][:default]
            hash["hl.regex.pattern"] = regex[:pattern][:default]
          end
          if regex[:pattern][:fields]
            regex[:pattern][:fields].each do |k,v|
              hash["f.#{k}.hl.regex.pattern"] = v
            end
          end
        else
          hash["hl.regex.pattern"] = regex[:pattern]
        end
      end
      if regex[:max_analyzed_chars]
        if regex[:max_analyzed_chars].kind_of? Hash
          if regex[:max_analyzed_chars][:default]
            hash["hl.regex.maxAnalyzedChars"] = regex[:max_analyzed_chars][:default]
          end
          if regex[:max_analyzed_chars][:fields]
            regex[:max_analyzed_chars][:fields].each do |k,v|
              hash["f.#{k}.hl.regex.maxAnalyzedChars"] = v
            end
          end
        else
          hash["hl.regex.maxAnalyzedChars"] = regex[:max_analyzed_chars]
        end
      end
    end

  end
  
  if @params[:mlt]
    hash[:mlt] = true
    hash["mlt.count"] = @params[:mlt][:count]
    hash["mlt.fl"] = @params[:mlt][:field_list].join(',')
    hash["mlt.mintf"] = @params[:mlt][:min_term_freq]
    hash["mlt.mindf"] = @params[:mlt][:min_doc_freq]
    hash["mlt.minwl"] = @params[:mlt][:min_word_length]
    hash["mlt.maxwl"] = @params[:mlt][:max_word_length]
    hash["mlt.maxqt"] = @params[:mlt][:max_query_terms]
    hash["mlt.maxntp"] = @params[:mlt][:max_tokens_parsed]
    hash["mlt.boost"] = @params[:mlt][:boost]
  end
  
  hash.merge(super.to_hash)
end