Class: ElasticSearch::Index
- Inherits:
-
Object
- Object
- ElasticSearch::Index
- Defined in:
- lib/elasticsearch.rb
Overview
Object to represent an index in elasticsearch
Class Method Summary collapse
-
.create(name, create_options = {}) ⇒ Object
Create a new index in elasticsearch.
Instance Method Summary collapse
-
#add(type, id, doc, params = {}) ⇒ Object
Add a document to this index.
- #bulk(data) ⇒ Object
-
#count(types, query, options = nil) ⇒ Object
Count results using a query string.
-
#initialize(name, server) ⇒ Index
constructor
A new instance of Index.
-
#mget(type, ids) ⇒ Object
Grab a bunch of items from this index.
-
#query(types, query, options = nil) ⇒ Object
Search this index using a query string.
-
#refresh ⇒ Object
Force a refresh of this index.
-
#remove(type, id) ⇒ Object
Remove a document from this index.
-
#remove_all(type) ⇒ Object
Remove all of a type from this index.
-
#remove_by_query(types, options) ⇒ Object
Remove a collection of documents matched by a query.
-
#search(types, options) ⇒ Object
Search this index using a post body.
Constructor Details
#initialize(name, server) ⇒ Index
Returns a new instance of Index.
36 37 38 39 |
# File 'lib/elasticsearch.rb', line 36 def initialize(name, server) @name = name @conn = ElasticSearch.get_connection(server) end |
Class Method Details
.create(name, create_options = {}) ⇒ Object
Create a new index in elasticsearch
name - the name of the index to be created
create_options - a hash of index creation options
Returns a new ElasticSearch::Index instance
189 190 191 192 193 194 195 196 197 |
# File 'lib/elasticsearch.rb', line 189 def self.create(name, ={}) conn = ElasticSearch.get_connection conn.put do |req| req.url "/#{name}" req.body = end new(name) end |
Instance Method Details
#add(type, id, doc, params = {}) ⇒ Object
Add a document to this index
type - the type of this document
id - the unique identifier for this document
doc - the document to be indexed
Returns a hash, the parsed response body from elasticsearch
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/elasticsearch.rb', line 135 def add(type, id, doc, params={}) doc.each do |key, val| # make sure dates are in a consistent format for indexing doc[key] = val.iso8601 if val.respond_to?(:iso8601) end put do |req| req.url "/#{@name}/#{type}/#{id}", params req.body = doc end end |
#bulk(data) ⇒ Object
66 67 68 69 70 |
# File 'lib/elasticsearch.rb', line 66 def bulk(data) return if data.empty? body = post "/#{@name}/_bulk", data raise Error, "bulk import got HTTP #{@last_resp.status} response" if @last_resp.status != 200 end |
#count(types, query, options = nil) ⇒ Object
Count results using a query string
types - the type or types (comma seperated) to search
query - the search query string
options - options hash for this search request (optional)
Returns a hash, the parsed response body from elasticsearch
120 121 122 123 124 125 126 |
# File 'lib/elasticsearch.rb', line 120 def count(types, query, =nil) query = {'q' => query} if query.is_a?(String) get do |req| req.url "#{@name}/#{types}/_count", query req.body = if end end |
#mget(type, ids) ⇒ Object
Grab a bunch of items from this index
type - the type to pull from
ids - an Array of ids to fetch
Returns a hash, the parsed response body from elasticsearch
78 79 80 81 82 83 |
# File 'lib/elasticsearch.rb', line 78 def mget(type, ids) get do |req| req.url "#{@name}/#{type}/_mget" req.body = {'ids' => ids} end end |
#query(types, query, options = nil) ⇒ Object
Search this index using a query string
types - the type or types (comma seperated) to search
query - the search query string
options - options hash for this search request (optional)
Returns a hash, the parsed response body from elasticsearch
105 106 107 108 109 110 111 |
# File 'lib/elasticsearch.rb', line 105 def query(types, query, =nil) query = {'q' => query} if query.is_a?(String) get do |req| req.url "#{@name}/#{types}/_search", query req.body = if end end |
#refresh ⇒ Object
Force a refresh of this index
This basically tells elasticsearch to flush it’s buffers but not clear caches (unlike a commit in Solr) “Commits” happen automatically and are managed by elasticsearch
Returns a hash, the parsed response body from elasticsearch
62 63 64 |
# File 'lib/elasticsearch.rb', line 62 def refresh post "/#{@name}/_refresh" end |
#remove(type, id) ⇒ Object
Remove a document from this index
type - the type of document to be removed
id - the unique identifier of the document to be removed
Returns a hash, the parsed response body from elasticsearch
153 154 155 156 157 |
# File 'lib/elasticsearch.rb', line 153 def remove(type, id) delete do |req| req.url "#{@name}/#{type}/#{id}" end end |
#remove_all(type) ⇒ Object
Remove all of a type from this index
type - the type of document to be removed
Returns a hash, the parsed response body from elasticsearch
164 165 166 167 168 |
# File 'lib/elasticsearch.rb', line 164 def remove_all(type) delete do |req| req.url "#{@name}/#{type}" end end |
#remove_by_query(types, options) ⇒ Object
Remove a collection of documents matched by a query
types - the type or types to query
options - the search options hash
Returns a hash, the parsed response body from elasticsearch
176 177 178 179 180 181 |
# File 'lib/elasticsearch.rb', line 176 def remove_by_query(types, ) delete do |req| req.url "#{@name}/#{types}/_query" req.body = end end |
#search(types, options) ⇒ Object
Search this index using a post body
types - the type or types (comma seperated) to search
options - options hash for this search request
Returns a hash, the parsed response body from elasticsearch
91 92 93 94 95 96 |
# File 'lib/elasticsearch.rb', line 91 def search(types, ) get do |req| req.url "#{@name}/#{types}/_search" req.body = end end |