【发布时间】: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