class Stretcher::IndexType

Represents an index scoped to a specific type. Generally should be instantiated via Index#type(name).

Attributes

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

Public Class Methods

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

Public Instance Methods

delete(id, options={}) click to toggle source

Deletes the document with the given ID

# File lib/stretcher/index_type.rb, line 69
def delete(id, options={})
  request :delete, id, options
rescue Stretcher::RequestError => e
  raise e if e.http_response.status != 404
  false
end
delete_mapping() click to toggle source

Delete the mapping for this type. Note this will delete All documents of this type as well www.elasticsearch.org/guide/reference/api/admin-indices-delete-mapping.html

# File lib/stretcher/index_type.rb, line 99
def delete_mapping
  request :delete, "_mapping"
end
delete_query(query) click to toggle source
# File lib/stretcher/index_type.rb, line 117
def delete_query(query)
  do_delete_query(query)
end
exists?(id=nil) click to toggle source

Check if this index type is defined, if passed an id this will check if the given document exists

# File lib/stretcher/index_type.rb, line 110
def exists?(id=nil)
  request :head, id
  true
rescue Stretcher::RequestError::NotFound => e
  false
end
explain(id, query, options={}) click to toggle source

Explains a query for a specific document

# File lib/stretcher/index_type.rb, line 44
def explain(id, query, options={})
  request(:get, "#{id}/_explain", options, query)
end
get(id, options={}, raw=false) click to toggle source

Retrieves the document by ID. Normally this returns the contents of _source, however, if the ‘raw’ flag is passed in, it will return the full response hash. Returns nil if the document does not exist.

The :fields argument can either be a csv String or an Array. e.g. [:field1,‘field2] or “field1,field2”. If the fields parameter is passed in those fields are returned instead of _source.

If, you include _source as a field, along with other fields you MUST set the raw flag to true to receive both fields and _source. Otherwise, only _source will be returned

# File lib/stretcher/index_type.rb, line 23
def get(id, options={}, raw=false)
  if options == true || options == false # Support raw as second argument, legacy API
    raw = true
    options = {}
  end
  
  res = request(:get, id, options)
  raw ? res : (res["_source"] || res["fields"])
end
get_mapping() click to toggle source

Retrieve the mapping for this type

# File lib/stretcher/index_type.rb, line 92
def get_mapping
  request :get, "_mapping"
end
mget(ids, options={}) click to toggle source

Retrieves multiple documents of the index type by ID www.elasticsearch.org/guide/reference/api/multi-get/

# File lib/stretcher/index_type.rb, line 35
def mget(ids, options={})
  request(:get, '_mget', options, :ids => ids)
end
mget_existing(ids,options={}) click to toggle source
# File lib/stretcher/index_type.rb, line 39
def mget_existing(ids,options={})
  mget(ids, options)
end
mlt(id, options={}) click to toggle source

Runs an MLT query based on the document’s content. This is actually a search, so a Stretcher::SearchResults object is returned

Equivalent to hitting /index/type/id/_mlt See www.elasticsearch.org/guide/reference/api/more-like-this/ for more Takens an options hash as a second argument, for things like fields=

# File lib/stretcher/index_type.rb, line 87
def mlt(id, options={})
  SearchResults.new(request(:get, "#{id}/_mlt", options, nil, {}, :mashify => false))
end
path_uri(path=nil) click to toggle source

Full path to this index type

# File lib/stretcher/index_type.rb, line 129
def path_uri(path=nil)
  p = index.path_uri(name)
  path ? p << "/#{path}" : p
end
percolate(document = {}) click to toggle source

Takes a document and percolates it

# File lib/stretcher/index_type.rb, line 77
def percolate(document = {})
  request :get, '_percolate', nil, {:doc => document}
end
post(source, options={}) click to toggle source

Index an item with automatic ID generation

# File lib/stretcher/index_type.rb, line 54
def post(source, options={})
  request(:post, nil, options, source)
end
put(id, source, options={}) click to toggle source

Index an item with a specific ID

# File lib/stretcher/index_type.rb, line 49
def put(id, source, options={})
  request(:put, id, options, source)
end
put_mapping(body) click to toggle source

Alter the mapping for this type

# File lib/stretcher/index_type.rb, line 104
def put_mapping(body)
  request(:put, "_mapping", {}, body)
end
update(id, body, options={}) click to toggle source

Uses the update api to modify a document with a script To update a doc with ID 987 for example: type.update(987, script: “ctx._source.message = ‘Updated!’”) See www.elasticsearch.org/guide/reference/api/update.html Takes an optional, third options hash, allowing you to specify Additional query parameters such as fields and routing

# File lib/stretcher/index_type.rb, line 64
def update(id, body, options={})
  request(:post, "#{id}/_update", options, body)
end