Elasticsearch::Resources
For Ruby 2.3+
Elasticsearch::Resources
is a wrapper for the Elasticsearch gem that provides a strongly typed interface for accessing your indexes, types, and documents.
documents = film.search({ query: { match: { title: 'Tron' } } })
documents
document = movies.get(id: 'tron')
document.id
document.attributes
document.title
Installation
If you're not using Bundler...
Install the gem via:
gem install elasticsearch-resources
Then require it into your application with:
require 'elasticsearch/resources'
If you're using Bundler...
Add the gem to your Gemfile:
gem 'elasticsearch/resources'
And then bundle install
to install the gem and its dependencies.
Common use cases
Search all indexes in a cluster
cluster = Elasticsearch::Resources::Cluster.new
documents = cluster.search({ query: { match: { title: 'Tron' } } })
Search all types in an index
cluster = Elasticsearch::Resources::Cluster.new
documents = cluster.search({ index: 'film', query: { match: { title: 'Tron' } } })
index = Elasticsearch::Resources::Index.new(cluster: cluster) do |index|
index.name = 'film'
end
documents = index.search({ query: { match: { title: 'Tron' } } })
Search specific type in an index
cluster = Elasticsearch::Resources::Cluster.new
documents = cluster.search({ index: 'film', type: 'movies', query: { match: { title: 'Tron' } } })
index = Elasticsearch::Resources::Index.new(cluster: cluster) do |index|
index.name = 'film'
end
type = Elasticsearch::Resources::Type.new(index: index) do |type|
type.name = 'movies'
end
documents = type.search({ query: { match: { title: 'Tron' } } })
Creating a document
document = Elasticsearch::Resources::Document.new(type: type, id: 'tron', attributes: { title: 'Tron' })
document.create
Fetch a document
document = type.get(id: 'tron')
Resources
There are four (4) basic resources which you can query with: Cluster
, Index
, Type
, and Document
.
Cluster
An Elasticsearch::Resources::Cluster
represents an ElasticSearch cluster that hosts multiple indexes. You will always needs to create one to run queries.
cluster = Elasticsearch::Resources::Cluster.new
cluster = Elasticsearch::Resources::Cluster.new do |cluster|
cluster.host = 'davesawesomemovies.com:9200'
end
indexes
Returns a Hash
of well known indexes. Empty if no well-known indexes are defined (regardless of whether they actually exist in Elasticsearch.) See "Defining well known resources" and define_indexes
.
cluster.indexes
client
Returns the underlying Elasticsearch::Transport::Client
.
cluster.client
search
Accepts (body, options = {})
and calls search
on the Elasticsearch::Transport::Client
.
Returns Array[Elasticsearch::Resources::Document]
objects.
cluster.search({ query: { match: { title: 'Tron' } } })
count
Accepts (body, options = {})
and calls count
on the Elasticsearch::Transport::Client
.
Returns Hash
response from Elasticsearch::Transport::Client
.
cluster.count({ query: { match: { title: 'Tron' } } })
Index
An Elasticsearch::Resources::Index
represents an ElasticSearch index that contains multiple types. Requires a Cluster
(see "Cluster" above.)
index = Elasticsearch::Resources::Index.new(cluster: cluster) do |index|
index.name = 'film'
end
name
Returns the name of the index, as it exists in Elasticsearch.
index.name
types
Returns a Hash
of well known types. Empty if no well-known types are defined (regardless of whether they actually exist in Elasticsearch.) See "Defining well known resources" and define_types
.
index.types
client
Returns the underlying Elasticsearch::Transport::Client
.
index.client
search
Accepts (body, options = {})
and calls search
on the Elasticsearch::Transport::Client
. Automatically adds the index
option to your queries.
Returns Array[Elasticsearch::Resources::Document]
objects.
index.search({ query: { match: { title: 'Tron' } } })
count
Accepts (body, options = {})
and calls count
on the Elasticsearch::Transport::Client
. Automatically adds the index
option to your queries.
Returns Hash
response from Elasticsearch::Transport::Client
.
index.count({ query: { match: { title: 'Tron' } } })
exists?
Calls exists?
on the Elasticsearch::API::Indices::IndicesClient
.
Returns true
or false
.
index.exists?
create
Calls create
on the Elasticsearch::API::Indices::IndicesClient
.
Throws error if index already exists.
index.create
put_mapping
Accepts (options = {})
and calls put_mapping
on the Elasticsearch::API::Indices::IndicesClient
.
index.put_mapping(type: 'movies', body: { })
delete
Calls delete
on the Elasticsearch::API::Indices::IndicesClient
.
Throws error if index doesn't exist.
index.delete
Type
An Elasticsearch::Resources::Type
represents an ElasticSearch type within an index. Requires a Index
(see "Index" above.)
type = Elasticsearch::Resources::Type.new(index: index) do |type|
type.name = 'movie'
end
name
Returns the name of the type, as it exists in Elasticsearch.
type.name
client
Returns the underlying Elasticsearch::Transport::Client
.
type.client
search
Accepts (body, options = {})
and calls search
on the Elasticsearch::Transport::Client
. Automatically adds the index
and type
option to your queries.
Returns Array[Elasticsearch::Resources::Document]
objects.
type.search({ query: { match: { title: 'Tron' } } })
count
Accepts (body, options = {})
and calls count
on the Elasticsearch::Transport::Client
. Automatically adds the index
and type
option to your queries.
Returns Hash
response from Elasticsearch::Transport::Client
.
type.count({ query: { match: { title: 'Tron' } } })
exists?
Accepts (options = {})
and calls exists?
on the Elasticsearch::Transport::Client
. Automatically adds the index
and type
option to your queries.
Returns true
or false
.
type.exists?(id: 'tron')
create
Accepts (options = {})
and calls create
on the Elasticsearch::Transport::Client
. Automatically adds the index
and type
option to your queries.
Throws error if document already exists.
type.create(id: 'tron', body: { })
update
Accepts (options = {})
and calls update
on the Elasticsearch::Transport::Client
. Automatically adds the index
and type
option to your queries.
type.update(id: 'tron', body: { })
delete
Accepts (options = {})
and calls delete
on the Elasticsearch::Transport::Client
. Automatically adds the index
and type
option to your queries.
Throws error if document doesn't exist.
type.delete(id: 'tron')
get
Accepts (options = {})
and calls get
on the Elasticsearch::Transport::Client
. Automatically adds the index
and type
option to your queries.
Returns Elasticsearch::Resources::Document
.
type.get(id: 'tron')
Document
An Elasticsearch::Resources::Document
represents an ElasticSearch document within an index. Requires an id
and Type
(see "Type" above.)
document = Elasticsearch::Resources::Document.new(
type: type,
id: 'tron',
attributes: { title: 'Tron' }
)
id
Returns a String
of the Document ID.
document.id
attributes
Returns a Hash
of the Document body.
document.attributes
client
Returns the underlying Elasticsearch::Transport::Client
.
document.client
exists?
Accepts (options = {})
and calls exists?
on the Elasticsearch::Transport::Client
. Automatically adds the index
, type
and id
option to your queries.
Returns true
or false
.
document.exists?
create
Accepts (options = {})
and calls create
on the Elasticsearch::Transport::Client
. Automatically adds the index
, type
, id
, and body
option to your queries.
Throws error if document already exists.
document.create
update
Accepts (options = {})
and calls update
on the Elasticsearch::Transport::Client
. Automatically adds the index
, type
, id
, and body
option to your queries.
document.update
delete
Accepts (options = {})
and calls delete
on the Elasticsearch::Transport::Client
. Automatically adds the index
, type
and id
option to your queries.
Throws error if document doesn't exist.
document.delete
get
Accepts (options = {})
and calls get
on the Elasticsearch::Transport::Client
. Automatically adds the index
, type
and id
option to your queries.
Returns Elasticsearch::Resources::Document
.
document.get
Defining well known resources
Cluster
class DavesAwesomeMovies < Elasticsearch::Resources::Cluster
define_configuration defaults: -> { |cluster|
cluster.host = 'davesawesomemovies.com:9200'
}
define_indexes film: 'Film'
end
cluster = DavesAwesomeMovies.new
cluster.indexes[:film]
Index
class Film < Elasticsearch::Resources::Index
define_configuration defaults: -> { |index|
index.name = 'film'
}
define_type movie: 'Movie'
end
film = Film.new
film.types[:movies]
Type
class Movies < Elasticsearch::Resources::Type
define_configuration defaults: -> { |type|
type.name = 'movie'
}
define_document 'Movie'
end
movies = Movies.new
movies.get(id: 'tron')
Document
class Movie < Elasticsearch::Resources::Document
define_attributes :title, :year
end
movie = Movie.new(id: 'tron', type: movies, attributes: { title: 'Tron', year: 1982 })
movie.title
movie.year
Development
Install dependencies using bundle install
. Run tests using bundle exec rspec
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/delner/elasticsearch-resources.
License
The gem is available as open source under the terms of the Apache 2.0 License.