indextank 1.0.11 → 1.0.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/indextank.rb +1 -1
  2. data/lib/indextank/index.rb +108 -5
  3. metadata +3 -3
data/lib/indextank.rb CHANGED
@@ -5,7 +5,7 @@ directory = File.expand_path(File.dirname(__FILE__))
5
5
  require File.join(directory, 'indextank', 'client')
6
6
 
7
7
  module IndexTank
8
- VERSION = "1.0.11"
8
+ VERSION = "1.0.12"
9
9
 
10
10
  def self.setup_connection(url)
11
11
  @conn = Faraday::Connection.new(:url => url) do |builder|
@@ -12,17 +12,18 @@ module IndexTank
12
12
  end
13
13
 
14
14
  def add( options = {} )
15
- options = {:public_search => false}.merge(options)
16
-
15
+
16
+ if self.exists?
17
+ raise IndexAlreadyExists
18
+ end
19
+
17
20
  response = @conn.put do |req|
18
21
  req.url ""
19
- req.body = options.to_json
22
+ req.body = options.to_json unless options.length == 0
20
23
  end
21
24
  case response.status
22
25
  when 201
23
26
  true
24
- when 204
25
- raise IndexAlreadyExists
26
27
  when 409
27
28
  raise TooManyIndexes
28
29
  when 401
@@ -30,6 +31,24 @@ module IndexTank
30
31
  end
31
32
  end
32
33
 
34
+ def update( options )
35
+
36
+ if not self.exists?
37
+ raise NonExistentIndex
38
+ end
39
+
40
+ response = @conn.put do |req|
41
+ req.url ""
42
+ req.body = options.to_json
43
+ end
44
+ case response.status
45
+ when 204
46
+ true
47
+ when 401
48
+ raise InvalidApiKey
49
+ end
50
+ end
51
+
33
52
  def refresh
34
53
  response = @conn.get('')
35
54
  if response.status == 200
@@ -61,6 +80,11 @@ module IndexTank
61
80
  refresh.status != 404
62
81
  end
63
82
 
83
+ def public_search_enabled?
84
+ refresh
85
+ @metadata['public_search']
86
+ end
87
+
64
88
  def batch_insert(documents)
65
89
  resp = @conn.put do |req|
66
90
  req.url "docs"
@@ -191,6 +215,85 @@ module IndexTank
191
215
  response.body
192
216
  end
193
217
 
218
+ # the options argument may contain an :index_code definition to override
219
+ # this instance's default index_code
220
+ # it can also contain any of the following:
221
+ # :start => an int with the number of results to skip
222
+ # :function => an int with the index of the scoring function to be used
223
+ # for this query
224
+ # :variables => a hash int => float, with variables that can be later
225
+ # used in scoring :function
226
+ # :category_filters => a hash to filter the query based on document categories. Keys represent category names.
227
+ # see http://indextank.com/documentation/ruby-client#faceting
228
+ #
229
+ # Example:
230
+ # category_filters => {:size => "big", :price => "expensive"}
231
+ # means that only documents that have "big" as size category and "expensive" as price category
232
+ # will match the query
233
+ # :docvar_filters => a hash with int keys and Array values to filter the query based on document variables.
234
+ # see http://indextank.com/documentation/ruby-client#range_queries
235
+ #
236
+ # Example:
237
+ # docvar_filters = { 1 => [ [2, 3], [5, nil] ]}
238
+ # means that only documents with document variable number 1 between 2 and 3 or bigger than 5
239
+ # will match the query.
240
+ # :function_filters => a hash with int keys and Array values to filter the query based on scoring functions.
241
+ # see http://indextank.com/documentation/ruby-client#range_queries
242
+ #
243
+ # Example:
244
+ # function_filters = { 3 => [ [nil, 2], [5, 7], [8,14] ]}
245
+ # means that only documents whose score calculated by scoring function 3 is lower than 2,
246
+ # between 5 and 7 or between 8 and 14 will match the query.
247
+ def delete_by_search(query, options = {})
248
+ options = {:start => 0}.merge(options).merge(:q => query)
249
+ if options[:variables]
250
+ options[:variables].each_pair { |k, v| options.merge!( :"var#{k}" => v ) }
251
+ options.delete :variables
252
+ end
253
+
254
+ if options[:docvar_filters]
255
+ # go from { 3 => [ [1, 3], [5, nil] ]} to filter_docvar3 => 1:3,5:*
256
+ options[:docvar_filters].each_pair { |k, v|
257
+ rng = v.map { |val|
258
+ raise ArgumentError, "using a range with bound count != 2" unless val.length == 2
259
+ "#{val[0] || '*'}:#{val[1] || '*'}"
260
+ }.join ","
261
+ options.merge!( :"filter_docvar#{k}" => rng )
262
+ }
263
+ options.delete :docvar_filters
264
+ end
265
+
266
+ if options[:function_filters]
267
+ # go from { 2 => [ [1 , 3],[5,8] ]} to filter_function2 => 1:3,5:8
268
+ options[:function_filters].each_pair { |k, v|
269
+ rng = v.map { |val|
270
+ raise ArgumentError, "using a range with bound count != 2" unless val.length == 2
271
+ "#{val[0] || '*'}:#{val[1] || '*'}"
272
+ }.join ","
273
+ options.merge!( :"filter_function#{k}" => rng )
274
+ }
275
+ options.delete :function_filters
276
+ end
277
+
278
+ if options[:category_filters]
279
+ options[:category_filters] = options[:category_filters].to_json
280
+ end
281
+
282
+ response = @conn.delete do |req|
283
+ req.url 'search', options
284
+ end
285
+ case response.status
286
+ when 400
287
+ raise InvalidQuery
288
+ when 404
289
+ raise NonExistentIndex
290
+ when 409
291
+ raise IndexInitializing
292
+ end
293
+
294
+ response.body
295
+ end
296
+
194
297
  def suggest(query, options = {})
195
298
  options.merge!({:query => query})
196
299
  @conn.get do |req|
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: indextank
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 11
10
- version: 1.0.11
9
+ - 12
10
+ version: 1.0.12
11
11
  platform: ruby
12
12
  authors:
13
13
  - Santiago Perez