EsClient
This elasticsearch client is just all you need to index and search your data with persistent http connection. It don't tend to wrap elasticsearch dsl into ruby style dsl. Excon used for http staff. There is adapter for ActiveRecord models.
Installation
Add this line to your application's Gemfile:
gem 'es_client'
And then execute:
$ bundle
Or install it yourself as:
$ gem install es_client
Usage
Create index:
index = EsClient::Index.new('products')
index.create
Add document to index:
index.save_document('product', 1, {id: 1, name: 'Table', sku: '123'})
Fetch document:
index.find('product', 1)
Add few document with one query i.e. bulk index:
index.bulk(:index, 'product', [{id: 2, name: 'Chair'}, {id: 2, name: 'Lamp'}])
And, of course, search:
index.search(query: {query_string: {query: 'table OR chair'}}).decoded
Configuration options
EsClient.setup do |config|
# Elasticsearch host
# config.host = 'http://localhost:9200'
# Log file path
config.log_path = Rails.root.join('log', 'elasticsearch.log')
# Log level (successful requests logged in debug)
# config.logger.level = ::Logger::Severity::INFO unless Rails.env.development?
# Log options, should be set to false in production for better performance
# `log_binary` - log binary data in bulk requests
# `log_response` - log response json, can be disabled in production
# `pretty` - pretty generate json in logs
# config.logger_options = {log_binary: true, log_response: true, pretty: true}
# Application wide index prefix
# config.index_prefix = 'my_app_name_here'
# Enable indexing callbacks
# config.callbacks_enabled = true
# Options passed to http client initializer (Excon currently)
# config.http_client_options = {persistent: true}
end
With ActiveRecord model:
Include EsClient modules:
class User
include ::EsClient::ActiveRecord::Glue
include ::EsClient::ActiveRecord::Shortcuts
end
Create index:
User.es_client.index.create
Indexing performed on save
and destroy
callbacks:
user = User.create(name: 'alex')
Fetch record elasticsearch document:
user.es_doc
Search:
User.es_client.search(query: {query_string: {query: 'table OR chair'}})
Reindex all your data:
User.es_client_reindex
Or with progressbar (requires ruby-progressbar
gem):
User.es_client_reindex_with_progress
Contributing
- Fork it ( https://github.com/leschenko/es_client/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request