Class: Stretcher::IndexType

Inherits:
EsComponent show all
Defined in:
lib/stretcher/index_type.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from EsComponent

#do_alias, #do_delete_query, #do_refresh, #do_search, #request

Constructor Details

#initialize(index, name, options = {}) ⇒ IndexType

Returns a new instance of IndexType


7
8
9
10
11
12
# 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

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index


5
6
7
# File 'lib/stretcher/index_type.rb', line 5

def index
  @index
end

#loggerObject (readonly)

Returns the value of attribute logger


5
6
7
# File 'lib/stretcher/index_type.rb', line 5

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name


5
6
7
# File 'lib/stretcher/index_type.rb', line 5

def name
  @name
end

#serverObject (readonly)

Returns the value of attribute server


5
6
7
# File 'lib/stretcher/index_type.rb', line 5

def server
  @server
end

Instance Method Details

#delete(id, options = {}) ⇒ Object

Deletes the document with the given ID


69
70
71
72
73
74
# 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_mappingObject

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


99
100
101
# File 'lib/stretcher/index_type.rb', line 99

def delete_mapping
  request :delete, "_mapping"
end

#delete_query(query) ⇒ Object


117
118
119
# File 'lib/stretcher/index_type.rb', line 117

def delete_query(query)
  do_delete_query(query)
end

#exists?(id = nil) ⇒ Boolean

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

Returns:

  • (Boolean)

110
111
112
113
114
115
# 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 = {}) ⇒ Object

Explains a query for a specific document


44
45
46
# 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) ⇒ Object

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


23
24
25
26
27
28
29
30
31
# 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_mappingObject

Retrieve the mapping for this type


92
93
94
# File 'lib/stretcher/index_type.rb', line 92

def get_mapping
  request :get, "_mapping"
end

#mget(ids, options = {}) ⇒ Object

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


35
36
37
# File 'lib/stretcher/index_type.rb', line 35

def mget(ids, options={})
  request(:get, '_mget', options, :ids => ids)
end

#mget_existing(ids, options = {}) ⇒ Object


39
40
41
# File 'lib/stretcher/index_type.rb', line 39

def mget_existing(ids,options={})
  mget(ids, options)
end

#mlt(id, options = {}) ⇒ Object

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=


87
88
89
# 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) ⇒ Object

Full path to this index type


129
130
131
132
# 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 = {}) ⇒ Object

Takes a document and percolates it


77
78
79
# File 'lib/stretcher/index_type.rb', line 77

def percolate(document = {})
  request :get, '_percolate', nil, {:doc => document}
end

#post(source, options = {}) ⇒ Object

Index an item with automatic ID generation


54
55
56
# File 'lib/stretcher/index_type.rb', line 54

def post(source, options={})
  request(:post, nil, options, source)
end

#put(id, source, options = {}) ⇒ Object

Index an item with a specific ID


49
50
51
# File 'lib/stretcher/index_type.rb', line 49

def put(id, source, options={})
  request(:put, id, options, source)
end

#put_mapping(body) ⇒ Object

Alter the mapping for this type


104
105
106
# File 'lib/stretcher/index_type.rb', line 104

def put_mapping(body)
  request(:put, "_mapping", {}, body)
end

#search(generic_opts = {}, explicit_body = nil) ⇒ Object

Issues an Index#search scoped to this type See Index#search for more details


123
124
125
126
# File 'lib/stretcher/index_type.rb', line 123

def search(generic_opts={}, explicit_body=nil)
  # Written this way to be more RDoc friendly
  do_search(generic_opts, explicit_body)
end

#update(id, body, options = {}) ⇒ Object

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


64
65
66
# File 'lib/stretcher/index_type.rb', line 64

def update(id, body, options={})
  request(:post, "#{id}/_update", options, body)
end