简体   繁体   中英

How do I make ElasticSearch Client available application wide?

I am using elasticsearch_ruby within a Rails App. I chose not to use elasticsearch_rails because I do not want such tight integration with my ActiveRecord objects.

Using elasticsearch_ruby and elasticsearch_transport , a new client is created like this:

client = ElasticSearch::Client.new(log:true)

How do I make this client accessible application wide. I only want to create the instance once and then use it application wide.

What is the best practice for doing this?

It seems that there is something obvious that I'm missing.

Use an initializer
1. Create a file under configuration/initializer/elasticsearch.rb
2. Initialize your client like $elasticsearch_client=ElasticSearch::Client.new(log:true)
3. Use this $elasticsearch_client anywhere in your rails app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM