Module: RSolr::Ext::Request::QueryHelpers

Included in:
RSolr::Ext::Request
Defined in:
lib/rsolr-ext/request.rb

Instance Method Summary collapse

Instance Method Details

#append_to_param(existing_value, new_value, auto_join = true) ⇒ Object

creates an array where the “existing_value” param is first and the “new_value” is the last. All empty/nil items are removed. the return result is either the result of the array being joined on a space, or the array itself. “auto_join” should be true or false.



99
100
101
102
103
# File 'lib/rsolr-ext/request.rb', line 99

def append_to_param(existing_value, new_value, auto_join=true)
  values = [existing_value, new_value]
  values.delete_if{|v|v.nil?}
  auto_join ? values.join(' ') : values.flatten
end

#build_query(value, quote_string = false) ⇒ Object

builds a solr query fragment if “quote_string” is true, the values will be quoted. if “value” is a string/symbol, the #to_s method is called if the “value” is an array, each item in the array is send to build_query (recursive) if the “value” is a Hash, a fielded query is built where the keys are used as the field names and the values are either processed as a Range or passed back into build_query (recursive)



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
# File 'lib/rsolr-ext/request.rb', line 67

def build_query(value, quote_string=false)
  case value
  when String,Symbol,Numeric
    quote_string ? quote(value.to_s) : value.to_s
  when Array
    value.collect do |v|
      build_query(v, quote_string)
    end.flatten
  when Range
    build_range(value)
  when Hash
    return value.collect do |(k,v)|
      if v.is_a?(Range)
        "#{k}:#{build_range(v)}"
      # If the value is an array, we want the same param, multiple times (not a query join)
      elsif v.is_a?(Array)
        v.collect do |vv|
          "#{k}:#{build_query(vv, quote_string)}"
        end
      else
        "#{k}:#{build_query(v, quote_string)}"
      end
    end.flatten
  end
end

#build_range(r) ⇒ Object

builds a solr range query from a Range object



54
55
56
# File 'lib/rsolr-ext/request.rb', line 54

def build_range(r)
  "[#{r.min} TO #{r.max}]"
end

#quote(value) ⇒ Object

Wraps a string around double quotes



49
50
51
# File 'lib/rsolr-ext/request.rb', line 49

def quote(value)
  %("#{value}")
end