【问题标题】:Trying to get count of products in each category in Thinking Sphinx试图在 Thinking Sphinx 中计算每个类别中的产品数量
【发布时间】:2015-05-30 11:33:17
【问题描述】:

我正在使用 Thinking Sphinx,并希望获取每个类别的产品数量。我的索引文件:

ThinkingSphinx::Index.define :product, :with => :active_record do
  # fields
  indexes title, :sortable => true
  indexes product_category_id

  has created_at, updated_at
end

我尝试使用查询,使用 group_by

Product.search('phone').group_by(&:product_category_id)

但它会返回包含所有搜索结果的分组哈希,但我只想查看计数,并希望使此查询更轻松。

我也试过查询

Product.search('phone', :select => 'COUNT(product.id) as cr').group_by(&:market_id)

但它返回错误

<ThinkingSphinx::SyntaxError: sphinxql: syntax error, unexpected IDENT, expecting DISTINCT or '*' near 'product.id) as cr FROM `product_core` WHERE MATCH('phone') AND `sphinx_deleted` = 0 LIMIT 0, 20; SHOW META' - SELECT COUNT(product.id) as cr FROM `product_core` WHERE MATCH('phone') AND `sphinx_deleted` = 0 LIMIT 0, 20; SHOW META>

【问题讨论】:

  • 尝试将 .count() 与括号内的列名附加到您的查询中
  • 我试过了,但是 simpe count() 只返回散列的大小,而带有列名的计数返回 0
  • stackoverflow.com/questions/30133478/… 类似场景,但仅使用 ActiveRecord 即可解决。
  • 我知道了,但我无法在 sphinx 搜索方法中添加选择条件
  • 尝试使用count(*) sphinx 实际上不支持为非不同计数命名列

标签: sql ruby-on-rails sphinx thinking-sphinx


【解决方案1】:

如上所述on GitHub

方面查询是您想要使用的: http://pat.github.io/thinking-sphinx/facets.html

它有两个方面 - 首先,您需要将 product_category_id 从字段转换为属性并将其设置为方面:

has product_category_id, :facet => true

然后您可以进行分面调用以获取您所需要的摘要信息:

Product.facets('phone')[:product_category_id]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    相关资源
    最近更新 更多