Tagging Plugin Developer Guide - Implementing a Custom Tags Provider
This guide describes how to implement a custom Tags Provider for the Bloomreach Content tagging plugin.
A custom Tags Provider is useful when you need to suggest tags based on specific logic or relationships within your content model. For example, if your document type includes a property that relates to other documents in a unique way, you can use a custom provider to generate tag suggestions that leverage this relationship.
Tags Provider Overview
A Tags Provider is a Java class that extends AbstractTagsProvider and implements the getTags method. To activate your Tags Provider, register it in a plugin cluster using the Console.
Example Implementation
The following example shows a simple Tags Provider. This implementation suggests all tags found across all documents.
public class AllTagsProvider extends AbstractTagsProvider { public final static String SCORE = "score"; public final static String TAGS_INDEX = "tags.index"; private double score; private String tagsIndex; public AllTagsProvider(IPluginContext context, IPluginConfig config) { super(context, config); score = config.getDouble(SCORE, 0.1); tagsIndex = config.getString(TAGS_INDEX); } public TagCollection getTags(JcrNodeModel nodeModel) throws RepositoryException { Session session = nodeModel.getNode().getSession(); TagCollection tags = new TagCollection(); // retrieve the facetsearch NodeIterator iterator = session.getRootNode().getNode(tagsIndex).getNodes(); while (iterator.hasNext()) { Node tagNode = iterator.nextNode(); if (tagNode.getPrimaryNodeType().getName().equals(HippoNodeType.NT_FACETSUBSEARCH)) { Tag tag = new Tag(ISO9075Helper.decodeLocalName(tagNode.getName()), tagNode.getProperty("hippo:count").getLong() * score); tags.add(tag); } } return tags; } }
This provider uses the supplied JcrNodeModel only to obtain a repository session. It then retrieves all nodes under the facetsearch node, which contains all tags present in the repository.
Tags Index
The tags property is indexed to improve search performance. The AllTagsProvider retrieves the tags index location during construction and uses it in the getTags method to access the facetsearch node and construct the NodeIterator.
Always use the tags index when possible. Accessing the index is significantly more efficient than iterating over nodes or using XPath or SQL queries to retrieve tags.