Ruby: ElasticSearch + Tire :: Search :: SearchRequestFailed - IndexMissingException bus error?

I want to use ElasticSearch + Tire for searches stored in MongoDB.

However, when I try to search, I get the following error:

Tires :: Search :: SearchRequestFailed in SearchController # index

404 : {"error":"IndexMissingException[[events] missing]","status":404} 

From what I understand, this tells me that there are no indexes for the event, although I told him to generate them when I ran db:setup .

Model:

 class Event include Mongoid::Document include Mongoid::Timestamps include Tire::Model::Search include Tire::Model::Callbacks field :name, :type => String field :description, :type => String field :started_at => Time field :ended_at => Time def to_indexed_json self.as_json end end 

Controller:

  def search Event.tire.search(params[:q]) end 

Any ideas on how to resolve this, please?

+6
source share
5 answers

... or even better, just run:

 rake environment tire:import CLASS=Event FORCE=true 
+12
source

Install the initializer (the following will work locally on your computer and on the hero with the addition to bonsai, just in case ...):

 # config/initializers/bonsai.rb if ENV['BONSAI_INDEX_URL'] Tire.configure do url "http://index.bonsai.io" end BONSAI_INDEX_NAME = ENV['BONSAI_INDEX_URL'][/[^\/]+$/] else app_name = Rails.application.class.parent_name.underscore.dasherize BONSAI_INDEX_NAME = "#{app_name}-#{Rails.env}" end 

In your model add index_name :

 class Event include Mongoid::Document include Mongoid::Timestamps include Tire::Model::Search include Tire::Model::Callbacks index_name BONSAI_INDEX_NAME field :name, :type => String field :description, :type => String field :started_at => Time field :ended_at => Time def to_indexed_json self.as_json end end 

Then open the Rails console with rails c and run:

 1.9.2p290 :001 >Event.create_elasticsearch_index => 200 : {"ok":true,"acknowledged":true} 1.9.2p290 :002 > Tire.index BONSAI_INDEX_NAME 1.9.2p290 :003 > import Event.all 1.9.2p290 :004?> refresh 1.9.2p290 :005?> end 

You should see something similar:

 MONGODB (0ms) ... ['system.namespaces'].find({}) MONGODB (0ms) ... ['events'].find({}) => #<Tire::Index:0xca8fb18 @name="your-app-name-development",@response=200 : {"ok":true,"_shards":{"total":10,"successful":5,"failed":0}}> 1.9.2p290 :006 > 

Now run the rails and try again.

+3
source

... see also:

 Event.index.import Event.all 

Thus, all your records are loaded into memory, serialized in JSON and sent by wire to ElasticSearch. This, as well as two solutions before, should throw IndexMissingException [[events] missing] out of the way.

+1
source

This can happen if you change the configuration. Try removing Tire.configure if you define it.

+1
source

Have you indexed your event model? And your object model? Elastic search also searches for an index of objects

I have a rake task to reindex my models

desc "Reindex Event"
task: reindex_events =>: environment do

 batch_size = 1000 count = batch_size Event.all.find_in_batches(:batch_size => batch_size) { |objects| puts "Count: " + count.to_s count += batch_size Event.index.import objects } 

end

0
source

Source: https://habr.com/ru/post/913589/


All Articles