如何从Sphinx结果集中的每个结果中发现关联的模型属性? (Rails、ThinkingSphinx)
我有以下 Rails 模型:
ad
category
ad belongs to category
当我执行广告搜索时,仅搜索广告标题以查找匹配项,我希望能够找出结果集中有多少广告属于每个类别,以便我可以生成类别名称列表,显示每个类别中有多少个匹配的广告。
我不知道如何提取类别名称和类别名称。 sphinx 结果集中每个类别内的广告数量,您能提供帮助吗?
I have the following Rails models:
ad
category
ad belongs to category
When I perform a search for an advert only the adverts title is searched for a match, I want to be able to find out how many ads in the result set belong to each category so that I can then generate a list of category names showing how many matching ads there are in each.
I have no idea how to extract the category names & number of ads within each category from the sphinx result set, can you help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
获取每个类别的广告数量摘要的最佳方法是将category_id 的属性添加到您的广告索引定义中,并将其标记为方面。
然后,在运行 rake ts:rebuild 以便 Sphinx 知道您的更改后,运行构面搜索:
您需要自己执行的额外步骤是翻译每个类别 id(类别 id 的键)生成的散列)转换为类别模型/名称。
The best approach for getting a summary of the number of ads per category would be to add an attribute of the category_id to your Ad index definition, and flag it as a facet as well.
Then, after running
rake ts:rebuild
so Sphinx is aware of your changes, run a facet search:The extra step that you'll need to do yourself is to translate each category id (the keys of the resulting hash) into a Category model/name.