これは本当に簡単な問題のようですが、他のソリューションやウェブサイトから試したことはありません。私は索引付けまたは照会したくない3つのフィールドを持っています - :p_s
、:gender
および:part_of_speech
- ただし、索引付けまたは照会を指定していないにもかかわらず、elasticsearchはこれらのフィールドから値を戻しています。途中で、this articleは、インデックス作成にno
と言っていますが、これはどこで発生するのかを示していません。レールで弾性検索の特定のフィールドを返します
用語コントローラー:
def search
@terms = Term.search(params[:query]).page(params[:page])
end
モデル:含まelasticsearch
メソッドを使用すると、多くの意味になると思い
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