Generate auto-completion using a plugin

The plugin indexing interface provides a method supplyAutoCompletionEntriesForProfiles() which enables auto-completion CSV to be generated for results pages.

This should only be implemented on a plugin designed to run on a results page. Although you can implement auto-completion on a data source, the resultant auto-completion index will not be accessible.

Prerequisite

In order to generate auto-completion, your plugin must be configured to provide indexing functionality.

Generate auto-completion for the results page

Generating auto-completion for a results page involves a couple of steps:

  • Generating one or more auto-completion entries, each consisting of a trigger, action and what to display when triggered. These entries are equivalent to a row from the auto-completion.csv file for a results page.

  • Adding these entries to the auto-completion for the results page.

Create an auto-completion entry

An auto-completion entry is generated by creating an AutoCompletionEntry object.

Create an auto-completion entry with a single trigger
AutoCompletionEntry entry = AutoCompletionEntry.builder() (1)
        .trigger("john smith") (2)
        .weight(900) (3)
        .action(new OpenUrl("https://people.example.com/john-smith.html")) (4)
        .category("") (5)
        .display(new JSONData("{\"firstname\":\"John\", \"lastname\":\"Smith\", \"email\":\"john.smith@example.com\", \"username\":\"jsmith\"}")) (6)
        .build();
1 AutocompletionEntry.builder() is used to build an auto-completion entry via a set of methods that set the various fields of an auto-completion entry.
2 Define a single auto-completion trigger using the trigger() method.
3 Sets the weighting used for selecting the auto-completions to return.
4 Set the action to open a URL when the suggestion is clicked. See: com.funnelback.plugin.index.model.querycompletion.action for a list of available actions.
5 This entry is not part of a category group.
category grouping is rarely used as it is a legacy feature of the original auto-completion specification that only supported a single auto-completion source. If you need to generate separate groupings you should create a separate results page for each group.
6 Sets a JSON object that will be returned for use when displaying the auto-completion suggestion. See com.funnelback.plugin.index.model.querycompletion.display for a list of available display types.
When returning the display element we recommend always returning a JSON object as this will ensure your auto-completion suggestions can be templated at display time using a handlebars template. Returning HTML is not recommended as this essentially hard-codes the display response.
Create an auto-completion entry with multiple triggers
 AutoCompletionEntry.builder()
     .triggers(List.of("banana","apple","orange")) (1)
     .weight(850)
     .display(new PlainText("Expand your search to include other fruits?")) (2)
     .category("")
     .action(new ExtendQuery("fruit")) (3)
     .build()
1 Multiple triggers can be set for an auto-completion suggestion by calling the triggers() method.
2 Sets a plain text display suggestion.
3 Sets the action to add terms to current query. When this auto-completion suggestion is selected, the query is re-run with an additional word ("fruit") added to the original query.

Example: Add auto-completion entries to the results page

package com.example.plugin;

import com.funnelback.plugin.index.model.querycompletion.action.OpenUrl;
import com.funnelback.plugin.index.model.querycompletion.display.JSONData;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.funnelback.plugin.index.IndexConfigProviderContext;
import com.funnelback.plugin.index.IndexingConfigProvider;
import com.funnelback.plugin.index.consumers.AutoCompletionConsumer;
import com.funnelback.plugin.index.model.querycompletion.AutoCompletionEntry;
import java.util.List;
import java.util.Set;

public class ExampleIndexingConfigProvider implements IndexingConfigProvider {

    private static final Logger log = LogManager.getLogger(ExamplendexingConfigProvider.class);

    @Override
    public void supplyAutoCompletionEntriesForProfiles(List<IndexConfigProviderContext> contextForProfilesThatRunThisPlugin, AutoCompletionConsumer consumer) { (1)

        // For each results page (where this plugin is enabled) in the parent search package
        contextForProfilesThatRunThisPlugin.forEach((context) -> { (2)
            String resultsPage = context.getProfileWithView().get(); (3)

            // Create an auto-completion entry with a single trigger
            AutoCompletionEntry entry = AutoCompletionEntry.builder() (4)
                    .trigger("john smith")
                    .action(new OpenUrl("https://people.example.com/john-smith.html"))
                    .category("")
                    .display(new JSONData("{\"firstname\":\"John\", \"lastname\":\"Smith\", \"email\":\"john.smith@example.com\", \"username\":\"jsmith\"}"))
                    .build();
            // Add the auto-completion entry to the results page
            consumer.applyAutoCompletionEntryToProfiles(entry, Set.of(resultsPage)); (5)
        });
    }
}
1 supplyAutoCompletionEntriesForProfiles supplies auto-completion for a results page.
2 Because results pages are not indexed separately (the indexing actually runs on the parent search package) we need to iterate over each results page (profile) where this plugin is enabled.
3 Gets the name of the current results page that is being processed.
4 Creates an auto-completion entry. The fields that are set using method calls correspond to the fields of an auto-completion CSV file.
5 The applyAutoCompletionEntryToProfiles() method adds the auto-completion entry to the specified results page.

See also