#Add custom field to elasticsearch
https://github.com/elastic/elasticsearch-rails/wiki
##дано
We have model ../app/models/log_debug.rb
class LogDebug < LogDatabase
self.table_name = "log_debug"
...
belongs_to :user
belongs_to :cluster
belongs_to :team
belongs_to :node
### search elasticsearch
include LogDebugElasticsearchSearchable
...
end
We have module ../app/models/concerns/log_debug_elasticsearch_searchable.rb
module LogDebugElasticsearchSearchable
settings index: { number_of_shards: 1 } do
mappings dynamic: 'true' do
indexes :id, :index => :not_analyzed, :type => 'integer'
...
indexes :visible_client, :index => :not_analyzed, :type => 'boolean'
end
end
def self.search(filter)
#
__elasticsearch__.search(
{
min_score: 0.5,
query: {
filtered: {
query:{
query_string: {
query: q + '*',
analyze_wildcard:true,
fields: ['message', 'data']
...
}
end
end
end
##задача
we need add custom fields, that are below, to elasticsearch. This fields is absent in DB table log_debug
node_name
user_name
team_name
cluster_name
##решение
add indexes :node_name, :index => :not_analyzed
to block mappings
add method as_indexed_json
to ../app/models/concerns/log_debug_elasticsearch_searchable.rb
add 'node_name'
to fields:['message', 'data', 'node_name']
in self.search
method in ../app/models/concerns/log_debug_elasticsearch_searchable.rb
add method node_name
to ../app/models/log_debug.rb
see code below
../app/models/log_debug.rb
class LogDebug < LogDatabase
self.table_name = "log_debug"
...
belongs_to :user
belongs_to :cluster
belongs_to :team
belongs_to :node
### search elasticsearch
include LogDebugElasticsearchSearchable
...
# for add custom fields to elasticsearch
def node_name
return node.name if node
nil
end
def user_name
return user.username if user
nil
end
def team_name
return team.name if team
nil
end
def cluster_name
return cluster.name if cluster
nil
end
...
end
../app/models/concerns/log_debug_elasticsearch_searchable.rb
module LogDebugElasticsearchSearchable
settings index: { number_of_shards: 1 } do
mappings dynamic: 'true' do
indexes :id, :index => :not_analyzed, :type => 'integer'
...
# add custom fieds to elastic
indexes :node_name, :index => :not_analyzed
indexes :user_name, :index => :not_analyzed
indexes :team_name, :index => :not_analyzed
indexes :cluster_name, :index => :not_analyzed
#
...
indexes :visible_client, :index => :not_analyzed, :type => 'boolean'
end
end
def as_indexed_json(options={})
attrs = {
:source_id => self.source_id,
:type_id => self.type_id,
:user_id => self.user_id,
:team_id => self.team_id,
:cluster_id => self.cluster_id,
:node_id => self.node_id,
:message => self.message,
:data => self.data,
:node_name => self.node_name,
:user_name => self.user_name,
:team_name => self.team_name,
:cluster_name => self.cluster_name,
:ip => self.ip,
:level => self.level,
#:_score => self._score,
:created_at => self.created_at,
:visible_client => self.visible_client,
}
#
attrs.as_json
end
def self.search(filter)
#
__elasticsearch__.search(
{
min_score: 0.5,
query: {
filtered: {
query:{
query_string: {
query: q + '*',
analyze_wildcard:true,
fields: ['message', 'data', 'node_name', 'user_name', 'team_name', 'cluster_name']
...
}
end
end
end
method for indexing/reindexing LogDebug
def self.es_import_log
#
prefix = Rails.configuration.gex_config[:elasticsearch_prefix]
#
model = LogDebug
model.__elasticsearch__.create_index! force: true
model.__elasticsearch__.refresh_index!
model.import(:force=>true, index: prefix+'log_debug')
#model.import
end
../config/gex/gex_config._anyenvironment_.yml
# elasticsearch
elasticsearch_host: '51.0.1.21'
elasticsearch_prefix: 'gex.'