我有下面的代码,我试图用ElasticSearch来查询它。轮胎/ 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
我会接受它,因为你有,以及写答案 – 2012-04-09 22:20:55