Method: IndexTank::Index#search

Defined in:
lib/indextank/index.rb

#search(query, options = {}) ⇒ Object

the options argument may contain an :index_code definition to override this instance’s default index_code it can also contain any of the following:

:start => an int with the number of results to skip
:len => an int with the number of results to return
:snippet => a comma separated list of field names for which a snippet
            should be returned. (requires an index that supports snippets)
:fetch => a comma separated list of field names for which its content
          should be returned. (requires an index that supports storage)
:function => an int with the index of the scoring function to be used
             for this query
:variables => a hash int => float, with variables that can be later
              used in scoring :function
:category_filters => a hash to filter the query based on document categories. Keys represent category names.
                    see http://indextank.com/documentation/ruby-client#faceting

                    Example:
                      category_filters => {:size => "big", :price => "expensive"}
                      means that only documents that have "big" as size category and "expensive" as price category
                      will match the query
:docvar_filters =>  a hash with int keys and Array values to filter the query based on document variables.
                    see http://indextank.com/documentation/ruby-client#range_queries

                    Example: 
                        docvar_filters = { 1 => [ [2, 3], [5, nil] ]} 
                        means that only documents with document variable number 1 between 2 and 3 or bigger than 5
                        will match the query.
:function_filters => a hash with int keys and Array values to filter the query based on scoring functions.
                    see http://indextank.com/documentation/ruby-client#range_queries

                    Example: 
                        function_filters = { 3 => [ [nil, 2], [5, 7], [8,14] ]} 
                        means that only documents whose score calculated by scoring function 3 is lower than 2,
                        between 5 and 7 or between 8 and 14 will match the query.


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
# File 'lib/indextank/index.rb', line 168

def search(query, options = {})
  options = {:start => 0, :len => 10 }.merge(options).merge(:q => query)
  if options[:variables]
    options[:variables].each_pair { |k, v| options.merge!( :"var#{k}" => v ) }
    options.delete :variables
  end

  if options[:docvar_filters]
    # go from { 3 => [ [1, 3], [5, nil] ]} to filter_docvar3 => 1:3,5:*
    options[:docvar_filters].each_pair { |k, v| 
                                          rng = v.map { |val|
                                            raise ArgumentError, "using a range with bound count != 2"  unless val.length == 2
                                            "#{val[0] || '*'}:#{val[1] || '*'}"
                                          }.join ","
                                          options.merge!( :"filter_docvar#{k}" => rng ) 
                                       }
    options.delete :docvar_filters
  end

  if options[:function_filters]
    # go from { 2 => [ [1 , 3],[5,8] ]} to filter_function2 => 1:3,5:8
    options[:function_filters].each_pair { |k, v| 
                                          rng = v.map { |val|
                                            raise ArgumentError, "using a range with bound count != 2"  unless val.length == 2
                                            "#{val[0] || '*'}:#{val[1] || '*'}"
                                          }.join ","
                                          options.merge!( :"filter_function#{k}" => rng ) 
                                       }
    options.delete :function_filters
  end

  if options[:category_filters]
    options[:category_filters] = options[:category_filters].to_json
  end

  response = @conn.get do |req|
    req.url 'search', options
  end  
  case response.status
  when 400
    raise InvalidQuery
  when 404
    raise NonExistentIndex
  when 409
    raise IndexInitializing
  end

  response.body
end