ThinkingSphinx 索引 ActsAsTaggable Model.tag_counts

发布于 2024-10-06 01:36:45 字数 945 浏览 2 评论 0原文

为了分配标签,我的软件的用户搜索他们现有的标签(在自动完成中填充)。我想在 Sphinx 中为此搜索建立索引,以便标签搜索具有超强响应能力。我看过关于如何使用 sphinx 索引模型标签的帖子 这在尝试查找具有该标签名称的所有对象时很有帮助,但我想为搜索现有标签的人索引标签搜索本身。

我正在尝试使用 Sphinx 进行此搜索

Lead.tag_counts(:条件 => [“tags.name like ?”, 'accounting%'])

生成 SQL

SELECT Tags.id, Tags.name, COUNT() AS count FROM tags LEFT OUTER JOIN 标记 ON Tags.id = taggings.tag_id AND taggings.context = 'tags' INNER JOIN 导致导通leads.id = taggings.taggable_id 哪里 ((taggings.taggable_type = '引导' AND Tags.name 如 'accounting%' )) GROUP BY tag.id, Tags.name 有 COUNT()

<块引用>

0

我假设我必须向标签模型添加索引才能允许下面的工作,但不太确定它们应该是什么。

Tag.sphinx_leads_scope.search(params[:tag])

谢谢,约翰-保罗

In order to assign tags, users of my software search their existing tags (which is populated in an autocomplete). I want to index this search in Sphinx so the tag search is ultra responsive. I have seen posts on how to index a Model's tags with sphinx which helps when trying to find all the objects with that tag name, but I want to index the tag search itself for people searching for existing tags.

I am trying to use Sphinx for this search

Lead.tag_counts(:conditions =>
["tags.name like ?", 'accounting%'])

Which produces the SQL

SELECT tags.id, tags.name, COUNT() AS count FROM tags LEFT
OUTER JOIN taggings ON tags.id =
taggings.tag_id AND taggings.context =
'tags' INNER JOIN leads ON leads.id =
taggings.taggable_id WHERE
((taggings.taggable_type = 'Lead' AND
tags.name like 'accounting%' )) GROUP
BY tags.id, tags.name HAVING COUNT(
)

0

I assume I have to add indexes to the Tag model to allow the below to work, but not quite sure what they should be.

Tag.sphinx_leads_scope.search(params[:tag])

Thanks, John-Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍风燃霜 2024-10-13 01:36:45

鉴于 acts-as-taggable-on 有自己的 Tag 类,这有点棘手,但如果你将此代码放在某个地方(也许在初始化程序中?),我认为它应该可以工作 - 尽管它是未经测试:

ActsAsTaggableOn::Tag.define_index do
  indexes name
end

添加 sphinx 作用域比较棘手 - 您需要为此进行 Monkeypatch ActsAsTaggableOn::Tag 。不过,这绝对不是不可能的。您可能还想通过标记添加可标记类型的过滤器...尽管字符串过滤器非常有限 - 请确保您 阅读文档。另外,如果您沿着字符串过滤器路径前进,您最终可能会得到一个标签的多个标签类型,因此您需要用逗号连接 CRC32 值...查看 这个有点相关的答案(对于代码片段)。

Given acts-as-taggable-on has its own Tag class, this gets a little tricky, but if you put this code somewhere (perhaps in an initializer?), I think it should work - though it's untested:

ActsAsTaggableOn::Tag.define_index do
  indexes name
end

Adding a sphinx scope is trickier - you'd need to monkeypatch ActsAsTaggableOn::Tag for that. Definitely not out of the question, though. You also may want to add a filter for taggable type via taggings... although string filters are very limited - make sure you read the docs. Also, if you are going down the string filter path, you'll end up with possibly multiple tagging types for a tag, and so you'll want to concatenate the CRC32'd values with commas... check out this somewhat related answer (for the code snippets).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文