将多个 Thinking Sphinx 查询的结果合并为一个分页结果
有没有一种简单的方法可以将多个 Thinking Sphinx 搜索的结果合并为一个结果?所有这些搜索都在同一模型上,但搜索具有不同的搜索词。我想做的是合并结果,以便它们都可以按日期列排序并接收正确的分页。
假设我有一个 Thinker 类和一个 Idea 类。
class Thinker < ActiveRecord::Base
has_many :ideas
end
class Idea < ActiveRecord::Base
belongs_to :thinker
define_index do
indexes text
has created_at
end
end
假设我有两个思想家,鲍勃和爱丽丝。我想组合以下搜索:
bob.ideas.search 'pancakes', :order => :created_at, :sort_mode => :desc
alice.ideas.search 'waffles', :order => :created_at, :sort_mode => :desc
...并以某种方式组合它们,以便将鲍勃(煎饼)和爱丽丝(华夫饼)想法的集合混合在一起,按创建的降序排序,并按 Thinking Sphinx 正确分页。在实际用例中,我可以以这种方式组合 2 到 15 个搜索。
我知道搜索方法返回 ThinkingSphinx::Search <大批。我考虑过手动将这些对象拼接在一起,但事实上我正在寻找排序和分页,这使得这有点棘手。
在 Thinking Sphinx 中是否有一种优雅的方法来做到这一点,或者我没有错过任何东西,而我几乎必须自己推出?
Is there a simple way to combine the results of multiple Thinking Sphinx searches into a single result? All of these searches are on the same model, but the searches have distinct search terms. What I'm trying to do is combine the results so that they can all be sorted by a date column and receive proper pagination.
Say I have a Thinker class and an Idea class.
class Thinker < ActiveRecord::Base
has_many :ideas
end
class Idea < ActiveRecord::Base
belongs_to :thinker
define_index do
indexes text
has created_at
end
end
And say I have two thinkers, Bob, and Alice. I want to combine the following searches:
bob.ideas.search 'pancakes', :order => :created_at, :sort_mode => :desc
alice.ideas.search 'waffles', :order => :created_at, :sort_mode => :desc
...and somehow combine them so that the collection of Bob's (pancake) and Alice's (waffle) ideas are mixed together, sorted by descending created_at, and properly paginated by Thinking Sphinx. In the actual use case, I could have anywhere between 2 and 15 searches to combine in this fashion.
I know that the search method returns a ThinkingSphinx::Search < Array. I thought about manually splicing these objects together, but the fact that I'm looking for both sorting and pagination makes this a bit tricky.
Is there an elegant way to do this in Thinking Sphinx or am I not missing anything and I pretty much have to roll my own?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
思考狮身人面像与神成合作。
所以你的 gemfile 中已经有 kaminari 了。您只需要做:
结果不再是 ThinkingSphinx::Search。这是一个数组,
您可以对结果使用分页和简单排序
Thinking Sphinx work with Kaminari.
So you already have kaminari in your gemfile. You just have to do :
Result is no longer a ThinkingSphinx::Search. It's an array
Your can use pagination and simple sort on result
您现在可以按照您想要的方式按日期排序
希望这会有所帮助
you can now sort by date like you want
hope this helps
您可能可以很轻松地做到这一点,但您需要重写查询以使其更加通用,并在
Idea
模型本身上进行搜索。你的模型将是:
You can probably do this pretty easily, but you need to rewrite your queries to be more generic and search on the
Idea
model itself.And your model would be: