class Stretcher::Index

Represents an Index context in elastic search. Generally should be instantiated via Server#index(name).

Attributes

logger[R]
name[R]
server[R]

Public Class Methods

new(server, name, options={}) click to toggle source
# File lib/stretcher/index.rb, line 8
def initialize(server, name, options={})
  @server = server
  @name = name
  @logger = options[:logger] || server.logger
end

Public Instance Methods

alias(name, &block) click to toggle source

Returns a Stretcher::Alias object for the alias name. Optionally takes a block, which will be passed a single arg with the Alias obj The block syntax returns the evaluated value of the block

my_server.alias('user_1') # Stretcher::Alias
my_server.alias { |alias| 1 } # 1
# File lib/stretcher/index.rb, line 31
def alias(name, &block)
  al = Alias.new(self, name, :logger => logger)
  block ? block.call(al) : al
end
analyze(text, analysis_params) click to toggle source

Implements the Analyze API EX:

index.analyze("Candles", analyzer: :snowball)
# => #<Hashie::Mash tokens=[#<Hashie::Mash end_offset=7 position=1 start_offset=0 token="candl" type="<ALPHANUM>">]>
# File lib/stretcher/index.rb, line 162
def analyze(text, analysis_params)
  request(:get, "_analyze", analysis_params) do |req|
    req.body = text
  end
end
bulk(data, options={}) click to toggle source

Perform a raw bulk operation. You probably want to use Stretcher::Index#bulk_index which properly formats a bulk index request.

# File lib/stretcher/index.rb, line 192
def bulk(data, options={})
  request(:post, "_bulk", options, data)
end
bulk_action(action, documents, options={}) click to toggle source
# File lib/stretcher/index.rb, line 52
def bulk_action(action, documents, options={})
  action=action.to_sym
  
  body = documents.reduce("") {|post_data, d_raw|
    d = Hashie::Mash.new(d_raw)
    index_meta = { :_id => (d[:id] || d.delete(:_id)) }

    system_fields = %w{_type _parent _routing}
    d.keys.reduce(index_meta) do |memo, key|
      index_meta[key] = d.delete(key) if system_fields.include?(key.to_s)
    end

    post_data << (MultiJson.dump({action => index_meta}) << "\n")
    post_data << (MultiJson.dump(d) << "\n") unless action == :delete
    post_data
  }
  bulk body, options
end
bulk_delete(documents, options={}) click to toggle source

Given a hash of documents, will bulk delete

docs = [{"_type" => "tweet", "_id" => 91011}]
server.index(:foo).bulk_delete(docs)
# File lib/stretcher/index.rb, line 48
def bulk_delete(documents, options={})
  bulk_action(:delete, documents, options)
end
bulk_index(documents, options={}) click to toggle source

Given a hash of documents, will bulk index

docs = [{"_type" => "tweet", "_id" => 91011, "text" => "Bulked"}]
server.index(:foo).bulk_index(docs)
# File lib/stretcher/index.rb, line 40
def bulk_index(documents, options={})
  bulk_action(:index, documents, options)
end
create(options=nil) click to toggle source

Creates the index, with the supplied hash as the options body (usually mappings: and settings:))

# File lib/stretcher/index.rb, line 72
def create(options=nil)
  request(:put, nil, nil, options)
end
delete() click to toggle source

Deletes the index

# File lib/stretcher/index.rb, line 77
def delete
  request :delete
end
delete_percolator_query(query_name) click to toggle source

Deletes a percolate query from the index www.elasticsearch.org/guide/reference/api/percolate/

# File lib/stretcher/index.rb, line 186
def delete_percolator_query(query_name)
  server.request(:delete, percolator_query_path(query_name))
end
delete_query(query) click to toggle source

Delete documents by a given query. Per: www.elasticsearch.org/guide/reference/api/delete-by-query.html

# File lib/stretcher/index.rb, line 124
def delete_query(query)
  do_delete_query(query)
end
exists?() click to toggle source

Check if the index has been created on the remote server

# File lib/stretcher/index.rb, line 114
def exists?
  # Unless the exception is hit we know its a 2xx response
  request(:head)
  true
rescue Stretcher::RequestError::NotFound
  false
end
get_mapping() click to toggle source

Retrieve the mapping for this index

# File lib/stretcher/index.rb, line 99
def get_mapping
  request :get, "_mapping"
end
get_settings() click to toggle source

Retrieve settings for this index

# File lib/stretcher/index.rb, line 104
def get_settings
  request :get, "_settings"
end
mget(meta_docs) click to toggle source

Takes a collection of hashes of the form {:_type => ‘foo’, :_id => 123} And issues an mget for them within the current index

# File lib/stretcher/index.rb, line 83
def mget(meta_docs)
  merge_data = {:_index => name}
  @server.mget(meta_docs.map {|d| d.merge(merge_data) })
end
msearch(queries=[]) click to toggle source

Searches a list of queries against only this index This deviates slightly from the official API in that ONLY queries are requried, the empty {} preceding them are not See: www.elasticsearch.org/guide/reference/api/multi-search.html

server.index(:foo).msearch([{query: {match_all: {}}}])
# => Returns an array of Stretcher::SearchResults
# File lib/stretcher/index.rb, line 148
def msearch(queries=[])
  raise ArgumentError, "msearch takes an array!" unless queries.is_a?(Array)
  req_body = queries.reduce([]) {|acc,q|
    acc << {:index => name}
    acc << q
    acc
  }
  @server.msearch req_body
end
optimize(options=nil) click to toggle source

Perform an optimize on the index to merge and reduce the number of segments

# File lib/stretcher/index.rb, line 174
def optimize(options=nil)
  request(:post, "_optimize", options)
end
path_uri(path="/") click to toggle source

Full path to this index

# File lib/stretcher/index.rb, line 204
def path_uri(path="/")
  p = @server.path_uri("/#{name}")
  path ? p << "/#{path}" : p
end
refresh() click to toggle source

Perform a refresh making all items in this index available instantly

# File lib/stretcher/index.rb, line 169
def refresh
  do_refresh
end
register_percolator_query(query_name, options = {}) click to toggle source

Registers a percolate query against the index www.elasticsearch.org/guide/reference/api/percolate/

# File lib/stretcher/index.rb, line 180
def register_percolator_query(query_name, options = {})
  server.request(:put, percolator_query_path(query_name), nil, options)
end
stats() click to toggle source

Retrieves stats for this index

# File lib/stretcher/index.rb, line 89
def stats
  request :get, "_stats"
end
status() click to toggle source

Retrieves status for this index

# File lib/stretcher/index.rb, line 94
def status
  request :get, "_status"
end
suggest(name, text, completion={}) click to toggle source

Takes the name, text, and completion options to craft a completion query. suggest(“band_complete”, “a”, field: :suggest) Use the new completion suggest API per www.elasticsearch.org/guide/reference/api/search/completion-suggest/

# File lib/stretcher/index.rb, line 199
def suggest(name, text, completion={})
  request(:post, "_suggest", nil, {name => {:text => text, :completion => completion}})
end
type(name, &block) click to toggle source

Returns a Stretcher::IndexType object for the type name. Optionally takes a block, which will be passed a single arg with the Index obj The block syntax returns the evaluated value of the block

my_index.index(:foo) # => #<Stretcher::Index ...>
my_index.index(:foo) {|idx| 1+1} # => 2
# File lib/stretcher/index.rb, line 20
def type(name, &block)
  t = IndexType.new(self, name)
  block ? block.call(t) : t
end
update_settings(settings) click to toggle source

Update settings for this index

# File lib/stretcher/index.rb, line 109
def update_settings(settings)
  request :put, "_settings", nil, settings
end

Private Instance Methods

percolator_query_path(query_name) click to toggle source
# File lib/stretcher/index.rb, line 211
def percolator_query_path(query_name)
  server.path_uri("/_percolator/#{name}/#{query_name}")
end