-------- step - 1 copy and paste this code in application.rb -----------------------
require 'elasticsearch'
class ElasticsearchPushCluster
ELASTICSEARCH_HOSTS = ['http://127.0.0.1:9200']
def self.session
@@session ||= Elasticsearch::Client.new hosts: ELASTICSEARCH_HOSTS
end
end
-----------step 2 copy and paste this code inside model ----------------------
def self.elastic_index_record(params, hash_record)
elastic_hash = {
account_id: params[:account_id].to_i,
push_application_id: params[:push_application_id].to_i,
push_device_id: params[:push_device_id].to_i,
event_name: params[:event_name],
custom_attributes: "#{ hash_record }",
latitude: params[:latitude],
longitude: params[:longitude],
created_at: Time.zone.now,
updated_at: Time.zone.now,
session_id: params[:session_id]
}
client = ElasticsearchPushCluster.session
response_record = client.index index: "push_event_tracking_#{ params[:account_id] }", type: "push_event_tracking", body: elastic_hash
elastic_search = ElasticsearchPushCluster.session.search index: "push_event_tracking_#{ params[:account_id].to_i }", body: { query: { match: { event_name: "#{params[:event_name]}" } }, sort: [{ "created_at": { "order": "desc" } } ] }
elastic_response = elastic_search["hits"]["hits"].first["_source"]
return elastic_response
end
It is nice blog Thank you provide important information and i am searching for same information to save my time Ruby on Rails Online Training
ReplyDelete