Class: Stretcher::IndexType
- Inherits:
-
EsComponent
- Object
- EsComponent
- Stretcher::IndexType
- 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
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
-
#delete(id, options = {}) ⇒ Object
Deletes the document with the given ID.
-
#delete_mapping ⇒ Object
Delete the mapping for this type.
- #delete_query(query) ⇒ Object
-
#exists?(id = nil) ⇒ Boolean
Check if this index type is defined, if passed an id this will check if the given document exists.
-
#explain(id, query, options = {}) ⇒ Object
Explains a query for a specific document.
-
#get(id, options = {}, raw = false) ⇒ Object
Retrieves the document by ID.
-
#get_mapping ⇒ Object
Retrieve the mapping for this type.
-
#initialize(index, name, options = {}) ⇒ IndexType
constructor
A new instance of IndexType.
-
#mget(ids, options = {}) ⇒ Object
Retrieves multiple documents of the index type by ID www.elasticsearch.org/guide/reference/api/multi-get/.
- #mget_existing(ids, options = {}) ⇒ Object
-
#mlt(id, options = {}) ⇒ Object
Runs an MLT query based on the document’s content.
-
#path_uri(path = nil) ⇒ Object
Full path to this index type.
-
#percolate(document = {}) ⇒ Object
Takes a document and percolates it.
-
#post(source, options = {}) ⇒ Object
Index an item with automatic ID generation.
-
#put(id, source, options = {}) ⇒ Object
Index an item with a specific ID.
-
#put_mapping(body) ⇒ Object
Alter the mapping for this type.
-
#search(generic_opts = {}, explicit_body = nil) ⇒ Object
Issues an Index#search scoped to this type See Index#search for more details.
-
#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
androuting
.
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, ={}) @index = index @server = index.server @name = name @logger = [:logger] || index.logger end |
Instance Attribute Details
#index ⇒ Object (readonly)
Returns the value of attribute index.
5 6 7 |
# File 'lib/stretcher/index_type.rb', line 5 def index @index end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
5 6 7 |
# File 'lib/stretcher/index_type.rb', line 5 def logger @logger end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/stretcher/index_type.rb', line 5 def name @name end |
#server ⇒ Object (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, ={}) request :delete, id, rescue Stretcher::RequestError => e raise e if e.http_response.status != 404 false end |
#delete_mapping ⇒ Object
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
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, ={}) request(:get, "#{id}/_explain", , 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, ={}, raw=false) if == true || == false # Support raw as second argument, legacy API raw = true = {} end res = request(:get, id, ) raw ? res : (res["_source"] || res["fields"]) end |
#get_mapping ⇒ Object
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, ={}) request(:get, '_mget', , :ids => ids) end |
#mget_existing(ids, options = {}) ⇒ Object
39 40 41 |
# File 'lib/stretcher/index_type.rb', line 39 def mget_existing(ids,={}) mget(ids, ) 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, ={}) SearchResults.new(request(:get, "#{id}/_mlt", , 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, ={}) request(:post, nil, , 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, ={}) request(:put, id, , 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, ={}) request(:post, "#{id}/_update", , body) end |