Permalink
Browse files

Merge remote-tracking branch 'origin/query-delete'

  • Loading branch information...
2 parents 5838c33 + c52fcf6 commit 0cccaacf2522a43fddb9e23f241d6b76e424e0f6 Leandro Oniszczuk committed Jun 29, 2011
Showing with 81 additions and 2 deletions.
  1. +1 −1 VERSION
  2. +1 −1 lib/indextank.rb
  3. +79 −0 lib/indextank/index.rb
View
@@ -1 +1 @@
-1.0.11
+1.0.12
View
@@ -5,7 +5,7 @@
require File.join(directory, 'indextank', 'client')
module IndexTank
- VERSION = "1.0.11"
+ VERSION = "1.0.12"
def self.setup_connection(url)
@conn = Faraday::Connection.new(:url => url) do |builder|
View
@@ -216,6 +216,85 @@ def search(query, options = {})
response.body
end
+ # 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
+ # :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.
+ def delete_by_search(query, options = {})
+ options = {:start => 0}.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.delete 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
+
def suggest(query, options = {})
options.merge!({:query => query})
@conn.get do |req|

0 comments on commit 0cccaac

Please sign in to comment.