使用 Sunspot 时,我在按组搜索时遇到了一些问题。
这是一个例子:
# == Schema Information
#
# Table name: movies
#
# id :integer(4) not null, primary key
# title :string(255)
class Movie < ActiveRecord::Base
has_and_belongs_to_many :actors
searchable do
text :title
integer :ages, multiple: true do
actors.map(&:age)
end
text :names, multiple: true do
actors.map(&:name)
end
end
end
# == Schema Information
#
# Table name: actors
#
# id :integer(4) not null, primary key
# name :string(255)
# age :integer(30)
class Actor < ActiveRecord::Base
has_and_belongs_to_many :movies
searchable do
integer :age
text :name
end
end
我想找到每部电影都有一位名叫约翰的 30 岁演员。
Movie.search do
with(:names).equal_to("John")
with(:ages).equal_to(30)
with(:title).equal_to("...")
# ...
end
问题在于它可能会找到一部有两个演员的电影。一个叫约翰,一个是 30 岁。有没有办法以某种方式将它们组合在一起,以便找到的电影中有一个 30 岁的演员叫约翰?