TIBCO EBX®
Information Search Add-on Documentation > Developer Guide
Navigation modeInformation Search Add-on Documentation > Developer Guide

Implementing a custom search algorithm

Overview

You can use the add-on's API to implement a custom search algorithm in Java. Administrators can use the algorithm when configuring the add-on. The following bullets highlight the requirements to implement a custom algorithm:

Attention

To use the sample code in the following sections, you must create and deploy your own custom module. For instructions, see the TIBCO EBX® developer documentation.

Example algorithm class

The following sample class defines a custom search algorithm. This class must extend the SearchDistance abstract class. The getDistance() method is where you define how the add-on calculates the similarity between a search keyword and the data being searched.

public class MyContainsAlgorithm extends SearchDistance
{
 
    public MyContainsAlgorithm(SchemaNode schemaNode, Locale locale)
 
    {
        super(schemaNode, locale);
    }
 
    @Override
    public float getDistance(String keyword, String text)
    {
        if (text.contains(keyword))
        {
            return 100f;
        }
        return 0;
    }
 
    @Override
    public boolean isNumericSupported()
    {
        return false;
    }
 
    @Override
    public boolean isFullTableScanRequired()
    {
        return true;
    }
 
    @Override
    public SearchComparator cloneMatching(SchemaNode newNode)
    {
        return new MyContainsAlgorithm(newNode, this.getLocale());
    }
 
}

Sample definition class

A definition class provides any labels, descriptions or parameters that will display in the UI during the configuration process. Additionally, this class instantiates the algorithm class from the previous section.

public class MyContainsAlgorithmDefinition implements AlgorithmDefinition
{
 
    @Override
    public UserMessage getLabel()
    {
        return UserMessage.createInfo("My Custom Algorithm");
    }
 
    @Override
    public UserMessage getDescription()
    {
        return UserMessage.createInfo("This is a custom algorith!");
    }
 
    @Override
    public List<ParameterDefinition> getParameters()
    {
        return null;
    }
 
    @Override
    public SearchComparator getAlgorithm(AlgorithmDefinitionContext adContext)
    {
        if (adContext == null)
        {
            return new MyContainsAlgorithm(null, Locale.US);
        }
        return new MyContainsAlgorithm(adContext.getField(), adContext.getLocale());
    }
 
    @Override
    public Class<SearchAlgorithmConfiguration> getSearchConfigurationClass()
    {
        return SearchAlgorithmConfiguration.class;
    }
 
    @Override
    public UserMessage checkParameterValueRange(List<InputParameter> parameters, Locale locale)
    {
        return null;
    }
 
}

Register the definition class

To register you definition class, add the following to the handleRepositoryStartup() method in your module's registration servlet.

private void registerTeseAlgorithms()
    {
        AlgorithmCatalog.add(new MyContainsAlgorithmDefinition());
    }