這似乎是一個非常簡單的問題,但是我從其他解決方案和網站嘗試的所有內容都無法正常工作。我有三個字段,我不想索引或查詢 - :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