次のコードがあり、ElasticSearch を使用してクエリを実行しようとしています。
Book.search(:q=>'Foo') を実行すると機能しますが、Book.search(:author=>'Doctor') を実行すると機能しません。私のデータベースには、「Barz、Foo、Doctor」のような名前のエントリがあります。
スノーボールを使用して名前を分割しているため、クエリで用語または用語を使用する必要があるかどうかわかりません。用語を試してみたところ、エラーが発生しました。用語では、結果が得られません。
class Author < ActiveRecord::Base
has_many :books
end
class Book < ActiveRecord::Base
belongs_to :author
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :title,
indexes :description
indexes :author,type: 'object', properties: {
name: { type: 'multi_field',
fields: { name: { type: 'string', analyzer: 'snowball' },
exact: { type: 'string', index: 'not_analyzed' }
}
} }
end
def to_indexed_json
to_json(:include=>{:author=>{:only=>[:name]}} )
end
def self.search(params = {})
tire.search(load:true) do
query do
boolean do
should { string params[:q] } if params[:q].present?
should { term params[:author] } if params[:author].present?
end
end
filter :term, :active=>true
end
end
end