関連フィールドを複数フィールドとしてマップしました。'name' プロパティを雪玉アナライザーを使用して分析するように設定し、'exact' を分析しないように設定しました。このフィールドを検索してフィルターすることもできます。このフィールドでソートできません。ソートしようとすると、エラスティックは「ドキュメントごとに複数の値、またはフィールドごとに複数のトークンを持つ文字列型でソートできません」というエラーを返します。
正確に似た「生」と呼ばれる追加のフィールドを作成しようとしましたが、これも機能しませんでした。以下は私のマッピングと、タイヤの宝石を介してソートを実行しようとしている方法です:
mapping do
indexes :sectors, :type => 'object',
:properties => { :name => { :type => 'multi_field',
:fields => {
:name => { :type => 'string', :analyzer => 'snowball' },
:exact => { :type => 'string', :index => 'not_analyzed' , :include_in_all => false }
}
}
}
end
def to_indexed_json
to_json( :include => {
...
:sectors => { :only => ["name"] },
...
})
end
def self.search(params)
tire.search(:load => true, :page=>params[:page], :per_page => 12) do
if params[:query].present?
query { string params[:query], :default_operator => "OR" }
else
query { all }
end
filter :term, "sectors.name.exact" => params[:sector] if params[:sector].present?
sort { by 'sectors.name.exact','asc' } if !params[:sort][:sector].blank?
end
end