我使用的是带有postgres的Rails 3.2.8。假设我有一个可以按类型和标签过滤的Book模型。我很好奇如何为每个过滤器实现facet count (类型,标签,...)显示在搜索结果中。ui将类似于以下内容:
Book Type
Banking(30)
IT(20)
Tags
business(20)
rspec(2)
rails(3)我一直在谷歌上搜索,试图找到不使用Sphinx,Solr或任何其他搜索引擎的方法。postgres或activerecord能做到吗?
发布于 2012-11-22 00:52:26
type_facet = Book.select('type, count(*)').where(:type => ["Banking","IT"]).group(:type)
tag_facet = Book.select('tag, count(*)').where(:tag => ["business","rspec","rails"]).group(:tag)在你的前端迭代上面的facet..
发布于 2012-11-21 19:46:46
你在找这样的东西吗?
Book Type
Banking (<%= Book.where(:type => "Banking").count %>)
IT (<%= Book.where(:type => "IT").count %>)
Tags
business (<%= Book.where(:tag => "business").count %>)
rspec (<%= Book.where(:tag => "rspec").count %>)
rails (<%= Book.where(:tag => "rails").count %>)发布于 2012-11-22 01:22:23
Book.count(:all, :group => 'type')
Book.count(:all, :group => 'tag')我认为它将为您返回计数和每个类型/标记。
https://stackoverflow.com/questions/13492008
复制相似问题