Tagging Plugin Developer Guide - Write Your Own Tags Provider

This section explains how you can create a custom Tags Provider.

Creating a custom Tags Provider can be useful if you want to search for tags in a special way. Your document type may have a property that has a special meaning in relation to other documents, and you want to take advantage of that in the way you suggest tags.

What is a Tags Provider?

A Tags Provider is a Java class that extends AbstractTagsProvider and implements the getTags method. To use the Tags Provider, you need to configure it in a plugin cluster in the Console.

Example

Below is the code for a simple Tags Provider. It suggest all tags it can find (in the set of 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 Tags Provider uses the document (JcrNodeModel) only to retrieve the repository session. It retrieves all nodes found in the facetsearch (the facetsearch lists all the tags found in the repository).

The Tags Index

To speed up searching for tags, the tags property is indexed. AllTagsProvider retrieves the location of the tags index at construction time. We later use that location in the getTags method to retrieve the facetsearch node (to construct the NodeIterator).

The tags index should be used as much as possible, as it is much more efficient than iterating through the nodes yourself, or using XPath or SQL queries to retrieve the tags.

Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?