我正在使用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>
答案 0 :(得分:2)
如上所述on GitHub:
Facet查询是您要用于此的内容: http://pat.github.io/thinking-sphinx/facets.html
它的两个方面 - 首先,您要将product_category_id从字段转换为属性并将其设置为facet:
Range("B1").Formula = join(application.transpose(Range("A1:A4")),vblf)
然后,您可以进行方面调用以获取您之后的摘要信息:
has product_category_id, :facet => true