标签架构提供类似于 Stack Overflow 标签同义词的功能

发布于 2024-10-16 07:39:01 字数 344 浏览 2 评论 0原文

我目前正在为一个小型书签应用程序(使用MySQL)设计一个数据库,我想对标签系统做一些巧妙的事情。虽然最初这不是问题,但我想实现类似于 Stack Overflow 标签同义词的功能,其中每个标签可以有多个映射到它的子标签。例如,这将允许对“hi”的标签搜索返回标记有“hello”的书签。

我熟悉构建一个多对多标签系统,其中包含三个表:“tags”、“posts”和“posts_tags”,我想让同义词适应这一点。

我最初的想法是每个标签都可以有一个“父”字段,其中包含它映射到的标签的 ID。然而,这可能会导致大量孤立标签,并且管理起来将是一场噩梦;我正在寻找速度和优雅。

如果有人有任何想法/指导,我们将不胜感激!谢谢

I'm currently designing a database for a small bookmarking application (using MySQL) and I'd like to do something clever with the tag system. Although it's not an issue initially, I want to implement something similar to Stack Overflow's tag synonyms, where each tag can have multiple sub-tags that map to it. This would allow tag searches for 'hi' to return bookmarks tagged with 'hello' for example.

I'm familiar with building a many-to-many tag system in which you have three tables: 'tags', 'posts' and 'posts_tags' and I'd like to make synonyms fit in with this.

My initial thoughts were that each tag could have a 'parent' field which contains the ID of the tag it maps to. However, this could cause a lot of orphaned tags and would be a nightmare to manage; I'm looking for speed and elegance.

If anybody has any ideas/guidance it'd be much appreciated! Thanks

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

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

发布评论

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

评论(2

江湖彼岸 2024-10-23 07:39:01

您可以使用子表作为父表。例如,

Tags
tagId, pk

ChildToParentTags
childTagId, pk, fk (pk of this table, fk into Tags table)
parentTagId, fk (fk into Tags table, have an index for this column).

Post
postId, pk

assume many-to-many post to tag relationship
PostToTag
postId, pk
tagId, pk

使用 childTagId 作为 ChildToParentTags 表的 pk 将标记限制为 0 或 1 个父级,但允许父级有多个子级。

按标签查询帖子:

select
 post.postId,
 post.otherStuff
from
 post 
  inner join postToTag on
   post.postId = postToTag.postId
  inner join tag on
   postToTag.tagId = tag.tagId
where
 tag.something = 'desired tag value'

You could use a child to parent table. For example,

Tags
tagId, pk

ChildToParentTags
childTagId, pk, fk (pk of this table, fk into Tags table)
parentTagId, fk (fk into Tags table, have an index for this column).

Post
postId, pk

assume many-to-many post to tag relationship
PostToTag
postId, pk
tagId, pk

Using childTagId as the pk of the ChildToParentTags table limits a tag to 0 or 1 parent, but allows a parent to have multiple children.

Query for post by tag:

select
 post.postId,
 post.otherStuff
from
 post 
  inner join postToTag on
   post.postId = postToTag.postId
  inner join tag on
   postToTag.tagId = tag.tagId
where
 tag.something = 'desired tag value'
被你宠の有点坏 2024-10-23 07:39:01

您可以使用分配组的系统。如上所述建立亲子关系并没有多大帮助。而创建群组关系有助于加快搜索速度。

创建一个名为 groups 的表 -

id, name, groupid

每个同义词元素都应分配一个 groupId(可以是分配的代码编号)。每当元素发生移动,或者将新元素分配给组,或者从组中移动现有元素时,您所要做的就是更新 groupid。

这使得搜索速度更快,因为每当您需要搜索某些内容时,您所要做的就是搜索 groupid。无需 IN 子句即可搜索具有相同 groupid 的所有元素。

我假设该表与其他表有 FK 关系。无论您在哪里有 FK 关系,您都可以将“groupid”作为 FK 关系,而不是使用具有 PK-FK 关系的“id”。

You can use a system which assigns groups. Creating parent child relationship as mentioned above does not help much. Whereas creating a group relationship helps make search faster.

Create a table called as groups -

id, name, groupid

Each elements that are synonyms should be assigned a groupId (which can be a code assigned number). Whenever there is a movement of elements, or a new element is assigned to a group, or an existing element is moved from a group, all you have to do is update the groupid.

This makes search faster, because, whenever you have to search for something, all you have to do is search for a groupid. All elements with same groupid can be search without the need for an IN clause.

I am assuming that this table will have a FK relationship with some other table. Wherever you have FK relationship, instead of having "id" with PK-FK relation, you can have "groupid" as FK relationship.

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