在elasticsearch中设置关键字字段
Setting keyword field in elasticsearch
我将 Rails 从 4 升级到 5.2,并将 elasticsearch 从 2.4 更新到 6.8。
以下是我遇到的错误。
[400] {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [activity_type] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}]
这是请求*的样子:
activity_types=>{:terms=>{:field=>"activity_type"}}
看看activity_search.rb:
class ActivitySearch < Search::Model
include DateSearch
apply_date_filters
apply_date_aggregations
filters :activity_types, type: :terms, field: :activity_type, aggregate: false
aggregates :activity_types, :subject_ids, :levels
....
看看model.rb:
module Search
class Model
def add_aggregation(name, opts={})
self._aggregations = _aggregations.deep_merge({name.to_sym => opts})
return if method_defined? "aggregate_#{name}"
define_method "aggregate_#{name}" do |config={}|
type = config.delete(:type){ :terms }
field = config.delete(:field){ name.to_s.singularize }
Search::Aggregation.new(type, field, opts)
end
def response
@response ||= Search::Response.new(search.response)
end
def request
@request ||= build_request
end
def search
@search ||= run_search
end
private
def run_search
search_class.search(request.params)
end
def search_class
self.class.name.demodulize.gsub('Search', '').safe_constantize
end
def build_request
request = Search::Request.new
request.query(*queries)
request.filter(*filters)
request.aggregate(aggregations)
request.sort(*sorts)
request
end
end
activity_search_facade.rb:
class Companies::ActivitySearchFacade < SimpleDelegator
delegate :aggregations, to: :response
end
谁能帮忙解决这个错误?
请注意 elasticsearch-rails
gem 版本为 6.0.0。
如果您需要更多代码,请告诉我。
*
如何更改此请求以使其在版本 6 中正确运行?
更新
是否可以根据我遇到的错误解决此问题?
curl -X GET 'localhost:9200/development_activities'
"activity_type":{"type":"text" }
如何将其更改为 type: "keyword"
? ^
请检查您的 elasticsearch 版本,因为 elasticsearch-rails
gem 版本遵循 elasticsearch 版本号,当您更新 elasticsearch-rails
时,它可能无法与较早的 elastic-search 兼容版本。
所以 activity 模型看起来像:
class Activity < ApplicationRecord
include SearchCommon
这就是 search_common.rb 的样子:
module SearchCommon
extend ActiveSupport::Concern
included do
include Elasticsearch::Model
after_commit on: [:create, :update] do
__elasticsearch__.index_document
end
after_commit on: [:destroy] do
__elasticsearch__.delete_document
end
index_name [Rails.env, model_name.collection].join('_')
end
end
所以我刚刚添加:
module SearchCommon
extend ActiveSupport::Concern
included do
include Elasticsearch::Model
after_commit on: [:create, :update] do
__elasticsearch__.index_document
end
after_commit on: [:destroy] do
__elasticsearch__.delete_document
end
mapping do # ADDITION
indexes :activity_type, type: 'keyword' # ADDITION
end # ADDITION
index_name [Rails.env, model_name.collection].join('_')
end
end
我将 Rails 从 4 升级到 5.2,并将 elasticsearch 从 2.4 更新到 6.8。
以下是我遇到的错误。
[400] {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [activity_type] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}]
这是请求*的样子:
activity_types=>{:terms=>{:field=>"activity_type"}}
看看activity_search.rb:
class ActivitySearch < Search::Model
include DateSearch
apply_date_filters
apply_date_aggregations
filters :activity_types, type: :terms, field: :activity_type, aggregate: false
aggregates :activity_types, :subject_ids, :levels
....
看看model.rb:
module Search
class Model
def add_aggregation(name, opts={})
self._aggregations = _aggregations.deep_merge({name.to_sym => opts})
return if method_defined? "aggregate_#{name}"
define_method "aggregate_#{name}" do |config={}|
type = config.delete(:type){ :terms }
field = config.delete(:field){ name.to_s.singularize }
Search::Aggregation.new(type, field, opts)
end
def response
@response ||= Search::Response.new(search.response)
end
def request
@request ||= build_request
end
def search
@search ||= run_search
end
private
def run_search
search_class.search(request.params)
end
def search_class
self.class.name.demodulize.gsub('Search', '').safe_constantize
end
def build_request
request = Search::Request.new
request.query(*queries)
request.filter(*filters)
request.aggregate(aggregations)
request.sort(*sorts)
request
end
end
activity_search_facade.rb:
class Companies::ActivitySearchFacade < SimpleDelegator
delegate :aggregations, to: :response
end
谁能帮忙解决这个错误?
请注意 elasticsearch-rails
gem 版本为 6.0.0。
如果您需要更多代码,请告诉我。
* 如何更改此请求以使其在版本 6 中正确运行?
更新
是否可以根据我遇到的错误解决此问题?
curl -X GET 'localhost:9200/development_activities'
"activity_type":{"type":"text" }
如何将其更改为 type: "keyword"
? ^
请检查您的 elasticsearch 版本,因为 elasticsearch-rails
gem 版本遵循 elasticsearch 版本号,当您更新 elasticsearch-rails
时,它可能无法与较早的 elastic-search 兼容版本。
所以 activity 模型看起来像:
class Activity < ApplicationRecord
include SearchCommon
这就是 search_common.rb 的样子:
module SearchCommon
extend ActiveSupport::Concern
included do
include Elasticsearch::Model
after_commit on: [:create, :update] do
__elasticsearch__.index_document
end
after_commit on: [:destroy] do
__elasticsearch__.delete_document
end
index_name [Rails.env, model_name.collection].join('_')
end
end
所以我刚刚添加:
module SearchCommon
extend ActiveSupport::Concern
included do
include Elasticsearch::Model
after_commit on: [:create, :update] do
__elasticsearch__.index_document
end
after_commit on: [:destroy] do
__elasticsearch__.delete_document
end
mapping do # ADDITION
indexes :activity_type, type: 'keyword' # ADDITION
end # ADDITION
index_name [Rails.env, model_name.collection].join('_')
end
end