将多个 Thinking Sphinx 查询的结果合并为一个分页结果

发布于 2024-12-27 16:57:20 字数 846 浏览 2 评论 0原文

有没有一种简单的方法可以将多个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睡美人的小仙女 2025-01-03 16:57:20

思考狮身人面像与神成合作。
所以你的 gemfile 中已经有 kaminari 了。您只需要做:

result = bob.ideas.search 'pancakes', :order => :created_at, :sort_mode => :desc
result += alice.ideas.search 'waffles', :order => :created_at, :sort_mode => :desc

结果不再是 ThinkingSphinx::Search。这是一个数组,

result = Kaminari.paginate_array(result)

您可以对结果使用分页和简单排序

Thinking Sphinx work with Kaminari.
So you already have kaminari in your gemfile. You just have to do :

result = bob.ideas.search 'pancakes', :order => :created_at, :sort_mode => :desc
result += alice.ideas.search 'waffles', :order => :created_at, :sort_mode => :desc

Result is no longer a ThinkingSphinx::Search. It's an array

result = Kaminari.paginate_array(result)

Your can use pagination and simple sort on result

挽清梦 2025-01-03 16:57:20
first_search = bob.ideas.search 'pancakes', :order => :created_at, :sort_mode => :desc
second_search = bob.ideas.search 'pancakes', :order => :created_at, :sort_mode => :desc
results = first_search.flatten + second_search.flatten

您现在可以按照您想要的方式按日期排序

sorted_results  = results.sort_by(&:date)

希望这会有所帮助

first_search = bob.ideas.search 'pancakes', :order => :created_at, :sort_mode => :desc
second_search = bob.ideas.search 'pancakes', :order => :created_at, :sort_mode => :desc
results = first_search.flatten + second_search.flatten

you can now sort by date like you want

sorted_results  = results.sort_by(&:date)

hope this helps

临风闻羌笛 2025-01-03 16:57:20

您可能可以很轻松地做到这一点,但您需要重写查询以使其更加通用,并在 Idea 模型本身上进行搜索。

Idea.search 'pancakes | waffles', :with => {:thinker_id => [bob.id, alice.id]}, 
                                  :order => :created_at, 
                                  :sort_mode => :desc,
                                  :match_mode => :boolean

你的模型将是:

class Idea < ActiveRecord::Base
  belongs_to :thinker

  define_index do
    indexes text
    has created_at, thinker_id
  end
end

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.

Idea.search 'pancakes | waffles', :with => {:thinker_id => [bob.id, alice.id]}, 
                                  :order => :created_at, 
                                  :sort_mode => :desc,
                                  :match_mode => :boolean

And your model would be:

class Idea < ActiveRecord::Base
  belongs_to :thinker

  define_index do
    indexes text
    has created_at, thinker_id
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文