ElasticSearch Issues

ISSUE: Primary shard is not active or isn’t assigned to a known node.ISSUE: Primary shard is not active or isn’t assigned to a known node.

SOLUTION:
curl -XGET localhost:9200/shards?v // To check shards are working or not

configure elasticsearch environment in project as well.
Create file in /config/initializers/elasticsearch.rb

config = {    host: “http://localhost:9200/”,    transport_options: {        request: { timeout: 5 }    }}
if File.exists?(“config/elasticsearch.yml”)  config.merge!(YAML.load_file(“config/elasticsearch.yml”)[Rails.env].symbolize_keys)end
Elasticsearch::Model.client = Elasticsearch::Client.new(config)

and Create file in /config/elasticsearch.yml
development: &default
host: ‘http://localhost:9200/’
test:
<<: *default
staging:
<<: *default
production:
<<: *default

Make sure if working in development, /etc/elasticsearch/elasticsearch.yml
index.number_of_shards = 1
index.number_of_replicas = 0

 

Reference: https://medium.com/@thecolorfulcrayon/configuring-elasticsearch-on-rails-8bcbe973e9e7

Advertisements

MySql with MongoDB in Rails

This is an area I’ve studied a lot lately, because I have an interest in using NoSQL instead of SQL databases for some of my models. Since we’re on Rails 5 and Mongoid 6 now, I just thought I’d share these steps for setting it up now:

1) Create your Rails app with the ‘–skip-active-record’ switch.

2) Remove sqlite3 from your Gemfile, add Mongoid to your Gemfile, and run ‘bundle’.

3) Run ‘rails g mongoid:config’.

4) Check your ‘application.rb’ file and make sure that inside the ‘class Application’ there is the line ” Mongoid.load! ‘./config/mongoid.yml’ “. It’s sometimes not included when the config is generated, but it’s needed to use Mongoid.

5) Mongoid is ready to go. The Rails generators for ‘model’, ‘scaffold’ etc have been overridden by Mongoid. Any models, scaffolds etc that you create will create classes that include the Mongoid::Document module instead of inheriting from ApplicationRecord in the models folder.

—–

And something I’ve also found useful is using both ActiveRecord and Mongoid. MongoDB works great for some types of data, but sometimes I still feel more comfortable with SQL for some data, especially if I feel the need to wrap it in transactions.

1) Create your Rails app.

2) Add Mongoid to your Gemfile and run ‘bundle’.

3) Run ‘rails g mongoid:config’.

4) Check your ‘application.rb’ file and make sure that inside the ‘class Application’ there is the line ” Mongoid.load! ‘./config/mongoid.yml’ “. It’s sometimes not included when the config is generated, but it’s needed to use Mongoid.

5) Mongoid is ready to go. The Rails generators for ‘model’, ‘scaffold’ etc have been overridden by Mongoid. Any models, scaffolds etc that you create will create
classes that include the Mongoid::Document module instead of inheriting
from ApplicationRecord in the models folder.

6) The ActiveRecord generators are still available, you just need to specify to use the ‘active_record’ versions as you use them. For example, at this point ‘rails g model user email’ generates a Mongoid model but ‘rails g active_record:model user email’ generates an ActiveRecord model and creates the needed migration. If you chose to make this a Mongoid model, there is no migration to worry about. If you chose to make this an ActiveRecord model, run the migration with ‘rails db:migrate’. (Rails 5 moved rake functionality into the rails command)

7) You can create associations between ActiveRecord and Mongoid models by manually coding methods. You’ll need to consider foreign keys. For example, if an ActiveRecord User has many Mongoid Posts, you’d add a foreign key field to the Post model. Its type would be Integer since ActiveRecord models use the Integer type for their id. Keep in mind that queries made from these associations can be slower because they traverse two data stores. Take things like lazy loading and the n+1 problem into consideration.

Reference: https://gorails.com/guides/setting-up-rails-4-with-mongodb-and-mongoid

“as_json” Rails

as_json has very flexible way to configure complex object according to model relations

EXAMPLE

Model campaign belongs to shop and has one list
Model list has many list_tasks and each of list_tasks has many comments
We can get one json which combines all those data easily.

@campaign.as_json(
    {
        except: [:created_at, :updated_at],
        include: {
            shop: {
                except: [:created_at, :updated_at, :customer_id],
                include: {customer: {except: [:created_at, :updated_at]}}},
            list: {
                except: [:created_at, :updated_at, :observation_id],
                include: {
                    list_tasks: {
                        except: [:created_at, :updated_at],
                        include: {comments: {except: [:created_at, :updated_at]}}
                    }
                }
            },
        },
        methods: :tags
    })

Notice methods: :tags can help you attach any additional object which doesn’t have relations with others. You just need to define a method with name tags in model campaign. This method should return whatever you need (e.g. Tags.all)

Official documentation for as_json