Reputation: 345
I'd like to ask, if there is a gem you would recommend me to use in order to connect my Rails app with elasticsearch like Searchkick or Chewy. It would be very nice if there were some simple tutorials about how to use it. Thanks.
Upvotes: 0
Views: 1263
Reputation: 18765
It depends on which Rails/elasticsearch combination you are using:
elasticsearch version >= 1
elasticsearch version <= 1
Tire has a lot of examples, elasticsearch rails not so many so here is a quick start:
# Gemfile
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
bundle install
# lib/tasks/elasticsearch.rake
require 'elasticsearch/rails/tasks/import'
Load your existing model data from db into elasticsearch:
bundle exec rake environment elasticsearch:import:model CLASS='YourModel'
# app/models/your_model.rb
class YourModel < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
end
Somewhere else in your code (say controller or scope in method or wherever you prefer)
# search with field filter
@your_models = YourModel.search(query: { match: { your_field: "whatever" } }).records
# faceted search
@your_models = YourModel.search(query: { match: { your_field: "whatever" } }, facets: { your_facet_name: { terms: { field: "your_facet_field", all_terms: true, order: "term" } } }).records
# to access to the faceted part of the elasticsearch response:
facet_results = @your_models..response.response['facets']['your_facet_name']['terms']
Upvotes: 5