使用 Sunspot/Solr 进行组?
我在使用 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 岁?
I'm having some problem searching by group when using Sunspot.
Here is an example:
# == 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
I want to find every movie that has an actor named John at age 30.
Movie.search do
with(:names).equal_to("John")
with(:ages).equal_to(30)
with(:title).equal_to("...")
# ...
end
The problem is here that it may find a movie that has two actors; one named John and one at age 30. Is there a way to somehow group this together so that the movie found have an actor named John at age 30?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就像 Maurício Linhares 在他的评论中所写的那样,解决方案是按电影检查演员模型和分组。
问题是 Sunspot 不支持 Solr 3.3 或 4.0,这是唯一支持分组的 Solr 版本。
这是我使用 Sunspot 1.2.1 和 Solr 3.3 的解决方案。
在我的示例中,
movie_id
放置在 actor 表中,这在我的实际应用程序中并未完成。归功于 alindeman 为他的 示例要点。
The solution, just like Maurício Linhares wrote in his comment, is to go through the actors model and group by movies.
The problem is that Sunspot doesn't support Solr 3.3 or 4.0, which is the only Solr versions that support grouping.
Here is my solution using Sunspot 1.2.1 and Solr 3.3.
In my example
movie_id
is placed in the actors table, this isn't done in my real application.Cred to alindeman for his example gist.