思考狮身人面像组选项

发布于 2024-10-13 04:49:25 字数 605 浏览 5 评论 0 原文

好吧,我有 4 个模型,我正在像这样的一次搜索中搜索

#category model
define_index do
  indexes :name, :description, :tag
  indexes sub_categories.name, :as => :subcategory_name
end

#pattern model
define_index do
  indexes :name, :tag_name, :reference
  indexes colorways.filename, :as => :color_name
end

,依此类推......我还有一个搜索控制器,可以通过此命令调用所有模型,

@results = ThinkingSphinx.search params[:search], :page => params[:page], :per_page => 25

我需要根据模型对结果进行分组,例如我首先想要所有的图形结果,然后是类别,然后是子类别在一起,所以在我看来,我可以将所有图形放在一起,然后将类别放在一起......关于按对象对它们进行分组或排序的最佳方式的任何想法......

Ok so i have 4 models that I am searching on in one search like this

#category model
define_index do
  indexes :name, :description, :tag
  indexes sub_categories.name, :as => :subcategory_name
end

#pattern model
define_index do
  indexes :name, :tag_name, :reference
  indexes colorways.filename, :as => :color_name
end

and so on.... I also have a searches controller that calls them all by this command

@results = ThinkingSphinx.search params[:search], :page => params[:page], :per_page => 25

I need to group the results based on the model so for example i want all the graphic results first then the categories and then the subcategories together so in my view i can have all the graphics together and the categories together.....any ideas on the best way to group or sort these by objects...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

佼人 2024-10-20 04:49:25

有两种方法...首先,Thinking Sphinx 将类名的 CRC 版本存储为属性,因此您可以要求 Sphinx 按此排序:

ThinkingSphinx.search params[:search],
  :page     => params[:page],
  :per_page => 25,
  :order    => 'class_crc ASC, @relevance DESC'

但是,这不会强制执行特定顺序(您提到您想要 Graphic结果第一) - 因此您可能需要考虑不同的方法 - 向每个索引定义添加一个手动属性,并用一个整数确定顺序:

has '1', :as => :model_order, :type => :integer

根据您想要在索引中的位置,将每个索引定义的 1 更改为 2、3 等搜索结果。然后搜索看起来像这样:

ThinkingSphinx.search params[:search],
  :page     => params[:page],
  :per_page => 25,
  :order    => 'model_order ASC, @relevance DESC'

希望这两个选项之一适合您的需求。

There's two approaches... firstly, Thinking Sphinx stores a CRC'd version of the class name as an attribute, so you can ask Sphinx to sort by that:

ThinkingSphinx.search params[:search],
  :page     => params[:page],
  :per_page => 25,
  :order    => 'class_crc ASC, @relevance DESC'

However, this doesn't enforce a specific order (you mentioned you want Graphic results first) - so you may want to consider a different approach - adding a manual attribute to each index definition with an integer determining the order:

has '1', :as => :model_order, :type => :integer

Change the 1 to 2, 3, etc for each index definition depending on where you want it in the search results. And then searching looks like this:

ThinkingSphinx.search params[:search],
  :page     => params[:page],
  :per_page => 25,
  :order    => 'model_order ASC, @relevance DESC'

Hopefully one of those two options suits what you're after.

圈圈圆圆圈圈 2024-10-20 04:49:25

一种快速而肮脏的方法是使用 group_by 和模型类名称:

@results.group_by(&:class)

但它可能不适用于大结果套。为此,您可以考虑将模型名称添加为每个表中的一列,并使用 ThinkingSphinx 进行分组,如下所述 在文档中

A quick and dirty way is to use group_by and the model class name:

@results.group_by(&:class)

But it probably won't work well for large result sets. For that, you might consider adding the model name as a column in each table, and do the grouping using ThinkingSphinx as described in the docs.

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