Class: IndexTank::Index
- Inherits:
-
Object
- Object
- IndexTank::Index
- Defined in:
- lib/indextank/index.rb
Instance Method Summary collapse
- #add(options = {}) ⇒ Object
- #batch_insert(documents) ⇒ Object
- #bulk_delete(docids) ⇒ Object
- #delete ⇒ Object
-
#delete_by_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 :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.
-
#document(docid) ⇒ Object
creates a new document, identified by :docid :docid => a String or Symbol, with bytesize no longer than 1024 bytes.
- #exists? ⇒ Boolean
- #functions(index = -1,, formula = nil) ⇒ Object
-
#initialize(index_url, metadata = nil) ⇒ Index
constructor
A new instance of Index.
- #method_missing(sym, *args, &block) ⇒ Object
-
#promote(docid, query, options = {}) ⇒ Object
the options argument may contain an :index_code definition to override this instance’s default index_code.
- #public_search_enabled? ⇒ Boolean
- #refresh ⇒ Object
- #running? ⇒ Boolean
-
#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.
- #suggest(query, options = {}) ⇒ Object
- #update(options) ⇒ Object
Constructor Details
#initialize(index_url, metadata = nil) ⇒ Index
Returns a new instance of Index.
8 9 10 11 12 |
# File 'lib/indextank/index.rb', line 8 def initialize(index_url, = nil) @uri = index_url @conn = IndexTank.setup_connection(index_url) @metadata = end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
74 75 76 77 |
# File 'lib/indextank/index.rb', line 74 def method_missing(sym, *args, &block) refresh if @metadata.nil? @metadata[sym.to_s] end |
Instance Method Details
#add(options = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/indextank/index.rb', line 14 def add( = {} ) if self.exists? raise IndexAlreadyExists end response = @conn.put do |req| req.url "" req.body = .to_json unless .length == 0 end case response.status when 201 true when 409 raise TooManyIndexes when 401 raise InvalidApiKey end end |
#batch_insert(documents) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/indextank/index.rb', line 88 def batch_insert(documents) resp = @conn.put do |req| req.url "docs" req.body = documents.to_json end case resp.status when 200 resp.body when 401 raise InvalidApiKey when 409 raise IndexInitializing when 404 raise NonExistentIndex when 400 raise InvalidArgument, resp.body else raise UnexpectedHTTPException, resp.body end end |
#bulk_delete(docids) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/indextank/index.rb', line 109 def bulk_delete(docids) data = [] docids.each do |docid| data << {'docid' => docid} end resp = @conn.delete do |req| req.url "docs" req.body = data.to_json end case resp.status when 200 resp.body when 401 raise InvalidApiKey when 409 raise IndexInitializing when 404 raise NonExistentIndex when 400 raise InvalidArgument, resp.body else raise UnexpectedHTTPException, resp.body end end |
#delete ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/indextank/index.rb', line 61 def delete response = @conn.delete('') case response.status when 204 raise NonExistentIndex end end |
#delete_by_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
: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.
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 |
# File 'lib/indextank/index.rb', line 247 def delete_by_search(query, = {}) = {:start => 0}.merge().merge(:q => query) if [:variables] [:variables].each_pair { |k, v| .merge!( :"var#{k}" => v ) } .delete :variables end if [:docvar_filters] # go from { 3 => [ [1, 3], [5, nil] ]} to filter_docvar3 => 1:3,5:* [: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 "," .merge!( :"filter_docvar#{k}" => rng ) } .delete :docvar_filters end if [:function_filters] # go from { 2 => [ [1 , 3],[5,8] ]} to filter_function2 => 1:3,5:8 [: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 "," .merge!( :"filter_function#{k}" => rng ) } .delete :function_filters end if [:category_filters] [:category_filters] = [:category_filters].to_json end response = @conn.delete do |req| req.url 'search', end case response.status when 400 raise InvalidQuery when 404 raise NonExistentIndex when 409 raise IndexInitializing end response.body end |
#document(docid) ⇒ Object
creates a new document, identified by :docid :docid => a String or Symbol, with bytesize no longer than 1024 bytes
325 326 327 |
# File 'lib/indextank/index.rb', line 325 def document(docid) Document.new("#{@uri}/docs", docid) end |
#exists? ⇒ Boolean
79 80 81 |
# File 'lib/indextank/index.rb', line 79 def exists? refresh.status != 404 end |
#functions(index = -1,, formula = nil) ⇒ Object
329 330 331 332 333 334 335 336 337 |
# File 'lib/indextank/index.rb', line 329 def functions(index = -1, formula = nil) if index == -1 @conn.get("functions").body.sort.collect do |index, formula| Function.new("#{@uri}/functions", index, formula) end else Function.new("#{@uri}/functions", index, formula) end end |
#promote(docid, query, options = {}) ⇒ Object
the options argument may contain an :index_code definition to override this instance’s default index_code
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/indextank/index.rb', line 306 def promote(docid, query, ={}) .merge!( :docid => docid, :query => query ) resp = @conn.put do |req| req.url 'promote' req.body = .to_json end case resp.status when 409 raise IndexInitializing when 404 raise NonExistentIndex when 400 raise InvalidArgument, resp.body end end |
#public_search_enabled? ⇒ Boolean
83 84 85 86 |
# File 'lib/indextank/index.rb', line 83 def public_search_enabled? refresh @metadata['public_search'] end |
#refresh ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/indextank/index.rb', line 52 def refresh response = @conn.get('') if response.status == 200 @metadata = response.body end response end |
#running? ⇒ Boolean
69 70 71 72 |
# File 'lib/indextank/index.rb', line 69 def running? refresh @metadata['started'] end |
#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, = {}) = {:start => 0, :len => 10 }.merge().merge(:q => query) if [:variables] [:variables].each_pair { |k, v| .merge!( :"var#{k}" => v ) } .delete :variables end if [:docvar_filters] # go from { 3 => [ [1, 3], [5, nil] ]} to filter_docvar3 => 1:3,5:* [: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 "," .merge!( :"filter_docvar#{k}" => rng ) } .delete :docvar_filters end if [:function_filters] # go from { 2 => [ [1 , 3],[5,8] ]} to filter_function2 => 1:3,5:8 [: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 "," .merge!( :"filter_function#{k}" => rng ) } .delete :function_filters end if [:category_filters] [:category_filters] = [:category_filters].to_json end response = @conn.get do |req| req.url 'search', end case response.status when 400 raise InvalidQuery when 404 raise NonExistentIndex when 409 raise IndexInitializing end response.body end |
#suggest(query, options = {}) ⇒ Object
297 298 299 300 301 302 |
# File 'lib/indextank/index.rb', line 297 def suggest(query, = {}) .merge!({:query => query}) @conn.get do |req| req.url 'autocomplete', end.body end |
#update(options) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/indextank/index.rb', line 34 def update( ) if not self.exists? raise NonExistentIndex end response = @conn.put do |req| req.url "" req.body = .to_json end case response.status when 204 true when 401 raise InvalidApiKey end end |