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, server, 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.
-
#get_mapping(types) ⇒ Object
Fetch the mappings defined for this index.
-
#initialize(name, server) ⇒ Index
constructor
A new instance of Index.
-
#mget(type, ids) ⇒ Object
Grab a bunch of items from this index.
-
#put_mapping(type, mapping) ⇒ Object
Adds mappings to the 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.
38 39 40 41 |
# File 'lib/elasticsearch.rb', line 38 def initialize(name, server) @name = name @conn = ElasticSearch.get_connection(server) end |
Class Method Details
.create(name, server, create_options = {}) ⇒ Object
Create a new index in elasticsearch
name - the name of the index to be created
server - URL of the server to create the index on
create_options - a hash of index creation options
Returns a new ElasticSearch::Index instance
216 217 218 219 220 221 222 223 224 |
# File 'lib/elasticsearch.rb', line 216 def self.create(name, server, ={}) conn = ElasticSearch.get_connection(server) conn.put do |req| req.url "/#{name}" req.body = end new(name, server) 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
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/elasticsearch.rb', line 137 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
68 69 70 71 72 |
# File 'lib/elasticsearch.rb', line 68 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
122 123 124 125 126 127 128 |
# File 'lib/elasticsearch.rb', line 122 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 |
#get_mapping(types) ⇒ Object
Fetch the mappings defined for this index
types - the type or types to query
Returns a hash, the parsed response body from elasticsearch
190 191 192 193 194 |
# File 'lib/elasticsearch.rb', line 190 def get_mapping(types) get do |req| req.url "#{@name}/#{types}/_mapping" 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
80 81 82 83 84 85 |
# File 'lib/elasticsearch.rb', line 80 def mget(type, ids) get do |req| req.url "#{@name}/#{type}/_mget" req.body = {'ids' => ids} end end |
#put_mapping(type, mapping) ⇒ Object
Adds mappings to the index
type - the type we're modifying
mapping - the new mapping to merge into the index
Returns a hash, the parsed response body from elasticsearch
202 203 204 205 206 207 |
# File 'lib/elasticsearch.rb', line 202 def put_mapping(type, mapping) put do |req| req.url "#{@name}/#{type}/_mapping" req.body = mapping 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
107 108 109 110 111 112 113 |
# File 'lib/elasticsearch.rb', line 107 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
64 65 66 |
# File 'lib/elasticsearch.rb', line 64 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
155 156 157 158 159 |
# File 'lib/elasticsearch.rb', line 155 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
166 167 168 169 170 |
# File 'lib/elasticsearch.rb', line 166 def remove_all(type) delete do |req| req.url "#{@name}/#{type}/_query", :q => '*' 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
178 179 180 181 182 183 |
# File 'lib/elasticsearch.rb', line 178 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
93 94 95 96 97 98 |
# File 'lib/elasticsearch.rb', line 93 def search(types, ) get do |req| req.url "#{@name}/#{types}/_search" req.body = end end |