通过JCR实现基于标签的搜索系统的最佳方式,如Modeshape [英] Best Way to implements a tag based search system by JCR like Modeshape

查看:114
本文介绍了通过JCR实现基于标签的搜索系统的最佳方式,如Modeshape的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要JCR之类的基于标签的搜索系统,如Modeshape。我想通过一些标签搜索节点。
问题是实现它的最佳方法是什么?

I need a tag based search system by JCR like Modeshape. i want to search nodes by some tags. Question is that what is the best way to implement it?


  1. 为标签添加新节点类型和mixins
    所以,如果这是真的,我在哪里可以定义可供用户查看的标签名称?

  2. 实现标签层次结构并在我的节点中引用它们。
    所以,如果这是真的我怎么能引用它们?

  3. 任何其他方式。


推荐答案

有几种方法可以在JCR中实现标记。您选择哪个选项取决于您自己的应用程序的需求。以下是我所知道的四种选择。

There are several ways to implement tags in JCR. Which option you pick will depend upon the needs of your own application(s). Here are four options I know of.

选项1:使用Mixins

为每个标记定义一个mixin节点类型定义,它是一个标记mixin(它没有属性定义或子节点定义),使用NodeTypeManager动态注册它们。然后,当您想要标记节点时,只需向该节点添加表示标记的mixin。任何节点都可以有多个标记,您可以查询具有特定标记的所有节点。

Define for each tag a mixin node type definition that is a marker mixin (it has no property definitions or child node definitions), registering them dynamically using the NodeTypeManager. Then when you want to "tag" a node, simply add to that node the mixin that represents the tag. Any node could have multiple tags, and you could query for all the nodes that have a particular tag.

(在此响应的其余部分中,acme用作通用命名空间。您应该将其替换为适合您自己的应用程序和组织的命名空间。)

例如,给定标签acme:tag1,您可以找到具有此标记的所有节点,并使用简单查询:

For example, given a tag "acme:tag1", you could find all nodes that have this tag with the simple query:

SELECT * FROM [acme:tag1]

这种方法的缺点是维护标签很麻烦。创建新标记需要注册新节点类型。您不能轻易地重命名标记,而是必须使用新名称为标记创建mixin;找到mixin代表旧标签的所有节点,删除旧的mixin,然后添加新的mixin;最后删除旧标记的节点类型定义(在任何地方不再使用之后)。删除旧标签以类似的方式完成。另一个缺点是,将附加元数据(例如,显示名称)与标签相关联并不容易,因为节点类型定义上不允许使用额外的属性。

The disadvantage of this approach is that maintaining tags is cumbersome. Creating new tags requires registering new node types. You cannot easily rename tags, but instead would have to create the mixin for the tag with the new name; find all nodes that have the mixin representing the old tag, remove the old mixin, and add the new one; and finally remove the node type definition for the old tag (after it is no longer used anywhere). Removing old tags is done in a similar manner. Another disadvantage is that it is not easy to associate additional metadata (e.g., display name) with a tag, since extra properties aren't allowed on node type definitions.

这方法应该表现得很好。

This approach should perform quite well.

选项2:使用分类和强引用

在这种方法中,您将在存储库的一个区域中创建一个简单的节点结构,您可以在其中为每个标记创建一个节点(例如,分类)。在此节点上,您可以设置描述标记的属性(例如,显示名称);这些属性可以随时更改(例如,重命名标记)。

In this approach, you would create a simple node structure in an area of the repository into which you can create a node for each tag (e.g., a taxonomy). On this node you could set properties that describe the tag (e.g., display name); these properties can be changed at any time (e.g., to rename the tag).

然后要将标记应用到节点,您只需创建一些排序与标签的关系。一种方法是定义一个mixin节点类型,其中包含REFERENCE类型的acme:tags多值属性。如果要将一个或多个标记应用于节点,只需将mixin添加到节点并将acme:tags属性设置为标记节点。

Then to "apply" the tag to a node, you simply have to create some sort of relationship to the tag. One way is to define a mixin node type that contains a "acme:tags" multivalued property of type REFERENCE. When you want to apply one or more tags to a node, simply add the mixin to the node and set the "acme:tags" property to the tag node(s).

要查找特定标记的所有节点,可以在标记节点上调用getReferences()以查找包含对标记节点的引用的所有节点。

To find all nodes of a particular tag, you can call "getReferences()" on a tag node to find all of the nodes that contain a reference to the tag node.

这种方法的好处是所有标签都必须在一个或多个分类法(包括用户特定的分类法)中进行控制/管理。但是,也有一些缺点。首先,REFERENCE属性的性能可能不是很好。一些JCR实现完全不鼓励使用REFERENCES。 ModeShape没有,但是当有许多节点包含对同一节点的引用时(例如,许多具有单个标记的节点),ModeShape可能会开始降低REFERENCE性能。

This approach has the benefit that all tags have to be controlled/managed within one or more taxonomies (including perhaps user-specific taxonomies). However, there are some disadvantages, too. First and foremost, the performance of REFERENCE properties might not be great. Some JCR implementations discourage the use of REFERENCES altogether. ModeShape does not, but ModeShape might start to degrade REFERENCE performance when there are lots of nodes that contain references to the same node (e.g., lots of nodes with a single tag).

选项3:使用分类和弱引用

此选项是混合类似于上面的选项2,除了acme:tags属性是WEAKREFERENCE而不是REFERENCE。您仍然可以定义和管理一个或多个分类法。要查找具有特定标记的节点,您不能在标记节点上使用getReferences()方法(因为它们不适用于WEAKREFERENCE属性),但您可以使用查询轻松执行此操作:

This option is a hybrid similar to Option 2 above except that the "acme:tags" properties would be WEAKREFERENCE instead of REFERENCE. You would still define and manage one or more taxonomies. To find nodes with a particular tag, you can't use the "getReferences()" method on the tag node (since they don't work with WEAKREFERENCE properties), but you can easily do this with a query:

SELECT * FROM [acme:taggable] AS taggable 
JOIN [acme:tag] AS tag ON taggable.[acme:tags] = tag.[jcr:uuid]
AND LOCALNAME(tag) = 'tag1'

这种方法确实强制使用一个或多个分类法,使控制标签更容易,因为它们必须存在于分类中才能使用。重命名和删除也更容易。性能方面,这比REFERENCE方法更好,因为WEAKREFERENCE属性在大量引用时表现更好,无论它们是指向一个节点还是多个。

This approach does enforce using one or more taxonomies, makes it a bit easier to control the tags, since they must exist in a taxonomy before they can be used. Renaming and removing is also easier. Performance-wise, this is better than the REFERENCE approach, since WEAKREFERENCE properties will perform better with large numbers of references, regardless of whether they all point to one node or many.

缺点是您可以删除标记,即使它仍在使用,但包含对该删除标记的WEAKREFERENCE的节点将不再有效。这可以通过应用程序中的某些约定来解决,或者通过简单地使用分类法上的元数据来表示特定标记是已弃用且不应使用。 (IMO,后者实际上是这种方法的一个好处。)

The disadvantage is that you can remove a tag even if it is still used, but the nodes that contain a WEAKREFERENCE to that removed tag will not be valid anymore. This can be remedied with some conventions in your application, or by simply using metadata on the taxonomy to say that a particular tag is "deprecated" and shouldn't be used. (IMO, the latter is actually a benefit of this approach.)

此选项通常比选项2更好地执行和扩展。

This option will generally perform and scale much better than Option 2.

选项4:使用字符串属性

另一种方法是简单地使用STRING属性来标记每个节点要应用的标记的名称。例如,您可以定义一个mixin(例如,acme:taggable)来定义多值STRING属性,当您想要标记一个节点时,只需添加mixin(如果尚未存在)并添加名称标记为acme:tagsSTRING属性的值(同样,如果它还没有作为值出现)。

Yet another approach is to simply use a STRING property to tag each node with the name of the tag(s) that are to be applied. For example, you could define a mixin (e.g., "acme:taggable") that defines a multi-valued STRING property, and when you want to tag a node simply add the mixin (if not already present) and add the name of the tag as a value on the "acme:tags" STRING property (again, if it's not already present as a value).

这种方法的主要优点是它非常简单:您只是在要标记的节点上使用字符串值。要查找使用特定标记标记的所有节点(例如,tag1),只需发出查询:

The primary advantage of this approach is that it is very simple: you're simply using string values on the node that is to be tagged. To find all nodes that are tagged with a particular tag (e.g., "tag1"), simply issue a query:

SELECT * 
FROM [acme:taggable] AS taggable 
WHERE taggable.[acme:tags] = 'tag1'

标签管理很简单:没有管理。如果要重命名标记,则可以重命名标记值。如果要删除标记(并从使用它标记的节点中删除),则可以通过从acme:tags属性中删除值(可能在后台作业中)来完成。

Management of the tags is easy: there is no management. If a tag is to be renamed, then you could rename the tag values. If a tag is to be deleted (and removed from the nodes that are tagged with it), then that can be done by removing the values from the "acme:tags" properties (perhaps in a background job).

请注意,这允许使用任何标记名称,因此最适用于标记名称根本不受控制的情况。如果要控制用作标记值的字符串列表,只需在存储库中创建分类(如上面的选项2和3中所述),并让应用程序将值限制为分类中的值。您甚至可以拥有多个分类,其中一些可能是特定于用户的。但是这种方法与选项2或3没有完全相同的控制。

Note that this allows any tag name to be used, and thus works best for cases where the tag names are not controlled at all. If you want to control the list of strings used as tag values, simply create a taxonomy in the repository (as described in Options 2 and 3 above) and have your application limit the values to those in the taxonomy. You can even have multiple taxonomies, some of which are perhaps user-specific. But this approach doesn't have quite the same control as Options 2 or 3.

此选项将比选项3执行得更好(因为查询更简单),但也会扩展。

This option will perform a bit better than Option 3 (since the queries are simpler), but will scale just as well.

这篇关于通过JCR实现基于标签的搜索系统的最佳方式,如Modeshape的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆