我有以下代码,我正在尝试使用 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