在 Sphinx 搜索中包含 Rails ActiveRecord 方法
我正在使用 Thinking Sphinx 来支持我的 Rails 应用程序上的搜索。
我知道指南明确指出你无法索引模型方法 ,但我愿意。具体来说,我有一个模型,其实例可以通过 acts_as_taggable_on_steroids
通过 has_many_through
关系进行标记。重要的警告:模型还通过 awesome_nested_set
嵌套,并且我有通过嵌套继承的标签。
以下是我搜索继承标签的方式:
def inherited_tags
retval = []
cat = self
while (cat = cat.parent)
retval += cat.tags
end
retval.uniq
end
我可以使用显式(非继承)标签进行搜索:
define_index do
indexes title
indexes tags(:name)
end
此搜索似乎工作得很好,但我无法将它们组合起来以允许用户也使用继承标签进行搜索。非常感谢任何建议!
I'm using Thinking Sphinx to power the search on my Rails application.
I know the guide explicitly says that you can't index model methods, but I would like to. Specifically, I have a model whose instances can be tagged through a has_many_through
relationship via acts_as_taggable_on_steroids
. The important caveat: the model also nests via awesome_nested_set
, and I have tags inheriting through the nesting.
Here's how I'm searching for inherited tags:
def inherited_tags
retval = []
cat = self
while (cat = cat.parent)
retval += cat.tags
end
retval.uniq
end
I am able to search by explicit (not inherited) tags using:
define_index do
indexes title
indexes tags(:name)
end
This search seems to work just fine, but I'm having trouble combining them to allow users to search using inherited tags as well. Any advice is much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Sphinx 只能索引数据库中的数据,没有办法解决这个问题(有一个 XML 选项,但认为 sphinx 不支持它)。
最好的办法是向模型添加一个缓存属性,该属性对用户不可见,但用于搜索。
尝试类似的方法:
Sphinx can only index data that's in your database, there's no way around that (there is an XML option, but thinking sphinx doesn't support it).
Your best bet is to add a cached attribute to you model that's invisible to users but used for search.
Try something like: