我有下面的代碼,我試圖用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