这似乎是一个非常简单的问题,但是我从其他解决方案和网站尝试的所有内容都无法正常工作。我有三个字段,我不想索引或查询 - :p_s
,:gender
和:part_of_speech
- 但elasticsearch仍然从这些字段返回值,即使我没有指定它们应该被索引或查询。大约一半下来,this article说要no
索引,但他们不表示这会发生。在rails中返回elasticsearch的特定字段
期限控制器:
def search
@terms = Term.search(params[:query]).page(params[:page])
end
型号:
require 'elasticsearch/model'
class Term < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
settings index: { number_of_shards: 1, number_of_replicas: 0 },
do
mappings dynamic: 'false' do
indexes :id, index: :not_analyzed
indexes :name, analyzer: :spanish_analyzer
indexes :definition, analyzer: :combined_analyzer
indexes :etymology1, analyzer: :combined_analyzer
indexes :etymology2, analyzer: :combined_analyzer
indexes :uses, analyzer: :combined_analyzer
indexes :notes1, analyzer: :combined_analyzer
indexes :notes2, analyzer: :combined_analyzer
end
end
def self.search(query)
__elasticsearch__.search(
{
query: {
multi_match: {
query: query,
fields: ['name^7', 'definition^6', 'etymology1^5', 'etymology2^4', 'uses^3', 'notes1^2', 'notes2^1'],
operator: 'and'
}
}
}
)
end
end
# Delete the previous term index in Elasticsearch
Term.__elasticsearch__.client.indices.delete index: Term.index_name rescue nil
# Create the new index with the new mapping
Term.__elasticsearch__.client.indices.create \
index: Term.index_name,
body: { settings: Term.settings.to_hash, mappings: Term.mappings.to_hash }
# Index all term records from the DB to Elasticsearch
Term.import(force: true)
对不起,我有一个新问题出现,我需要先解决......您的解决方案是我尝试过的方法之一对我无效。为了清楚起见,这段代码应该代替我的'def self.search(query)'方法,不是吗?它也应该跟在我的映射之后,对吧? – evanejin
使用此方法,您不再需要执行映射,因为这些字段不会再被索引,因此根本无法查询。您的搜索仍然可以保持原样,我想所有其他配置都可以在您的设置中完成。 – oreoluwa
嗯,好吧,我试着就像你说的,但它仍然返回那些额外的领域,我不想包括 – evanejin