9

I am new to rails and to elastic search. I have seen other resources to configure using Tire, Searchkick and some others, but I want to use Elasticsearch gem. I have running rails application and running Elasticsearch server on my system but I do not how to configure them to communicate with each other.

Currently, I am facing a lot of troubles to do the same. Any help would be highly appreciated.

4
  • Please post your elasticsearch related code, and we don't know what's your problem/error. Commented Nov 23, 2015 at 10:11
  • require "elasticsearch/model" class Service < ActiveRecord::Base include Elasticsearch::Model include Elasticsearch::Model::Callbacks has_many :requests, :class_name => "::Request" belongs_to :employee, :class_name => "::Employee" end Service.import Commented Nov 23, 2015 at 14:26
  • Error ActionView::Template::Error ([404] {"error":{"root_cause":[{"type":"index_not_fo und_exception","reason":"no such index","index":"services","resource.type":"inde x_or_alias","resource.id":"services"}],"type":"index_not_found_exception","reaso n":"no such index","index":"services","resource.type":"index_or_alias","resource .id":"services"},"status":404}): Commented Nov 23, 2015 at 14:27
  • class ServicesController < ApplicationController before_action :set_service, only: [:show, :edit, :update, :destroy] # GET /services # GET /services.json def index @services = Service.search((params[:q].present? ? params[:q] : "*")).records end end Commented Nov 23, 2015 at 14:30

2 Answers 2

16

For a very basic quick start of the elastic's github gem for model indexing you could do the following in development environment with elasticsearch running on localhost:9200

in Gemfile:

gem 'elasticsearch-model'

then run on terminal:

$ bundle install

in app/models/service.rb include right after class declaration:

include Elasticsearch::Model

you can now play with it on console with existing data (results are just an example):

$ rails console

# Create the index for Service model on elasticsearch
> Service.__elasticsearch__.create_index!
=> {"acknowledged"=>true}

# Import current Service records into the index
> Service.import
  Service Load (207.3ms)  SELECT  "services".* FROM "services"  ORDER BY "services"."id" ASC LIMIT 1000

# Sample search returning total results
> Service.__elasticsearch__.search("mykeyword").results.total
=> 123

For more information and details you can take a look at the project's github page

Sign up to request clarification or add additional context in comments.

Comments

1

Better use elasticsearch-rails

in Gemfile:

gem install elasticsearch-rails

To import the records from your Article model, run:

$ bundle exec rake environment elasticsearch:import:model CLASS='Article'

To limit the imported records to a certain ActiveRecord scope, pass it to the task:

$ bundle exec rake environment elasticsearch:import:model CLASS='Article' SCOPE='published'

Run this command to display usage instructions:

$ bundle exec rake -D elasticsearch

If you want to use for model

elasticsearch-model, which contains search integration for Ruby/Rails models such as ActiveRecord::Base and Mongoid,

6 Comments

Does not "Elasticsearch" do the samething as done by both Elasticsearch-rails and Elasticsearch-model, I read Elasticsearch-rails in companion to Elasticsearch-rails so I thought instead of using two gems it is better to use one i.e Elasticsearch..please correct me if I am wrong.
I built an application. Now, I wanna add a feature so that user can refine the search based on selection like done by flipkart, paytm on different product.
yes right for your rails app use elastic-rails The elasticsearch-rails library is a companion for the the elasticsearch-model library, providing features suitable for Ruby on Rails applications.
I have added Elasticsearch-rails, Elasticsearch-model in gem file...Elasticsearch server is running at port no. 9200. but it through following error. My model name is "Service" services does not exist to be imported into. Use create_index! or the :force option to create it.
C:\datacheck> bundle exec rake environment elasticsearch:import:model CLASS='SER VICE' DL is deprecated, please use Fiddle DL is deprecated, please use Fiddle rake aborted! ArgumentError: services does not exist to be imported into. Use create_index! or the :force option to create it. C:in import' C:/datacheck/app/models/service.rb:10:in <top (required)>' (eval):1:in `block (3 levels) in <top (required)>' Tasks: TOP => elasticsearch:import:model (See full trace by running task with --trace) When I ran bundle exec rake environment elasticsearch:import:model CLASS='Service'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.