Session: Querying: How to Work with Suggestions

The suggestion feature is available through query extension methods. It gives you the ability to find word similarities using string distance algorithms.

Syntax

ISuggestionDocumentQuery<T> suggestUsing(SuggestionBase suggestion);

ISuggestionDocumentQuery<T> suggestUsing(Consumer<ISuggestionBuilder<T>> builder);
Parameters
suggestion SuggestionBase Defines the type of suggestion that should be executed
builder Consumer<ISuggestionBuilder<T>> Builder with a fluent API that constructs a SuggestionBase instance

Builder

ISuggestionOperations<T> byField(String fieldName, String term);

ISuggestionOperations<T> byField(String fieldName, String[] terms);

ISuggestionOperations<T> withOptions(SuggestionOptions options);
Parameters
fieldName String Points to the index field that should be used for operation
term String Term that will be used as a basis of the suggestions
terms String[] Terms that will be used as a basis of the suggestions
options SuggestionOptions Non-default options that should be used for operation

Options

private int pageSize = 15;

private StringDistanceTypes distance = StringDistanceTypes.LEVENSHTEIN;

private Float accuracy = 0.5f;

private SuggestionSortMode sortMode = SuggestionSortMode.POPULARITY;

// getters and setters for fields listed above
Options
pageSize int Maximum number of suggestions that will be returned
distance StringDistanceTypes String distance algorithm to use (NONE, LEVENSTEIN, JARO_WINKLER, N_GRAM)
accuracy Float Suggestion accuracy
sortMode SuggestionSortMode Indicates in what order the results should be returned (None, Popularity)

Example I

SuggestionOptions options = new SuggestionOptions();
options.setAccuracy(0.4f);
options.setPageSize(5);
options.setDistance(StringDistanceTypes.JARO_WINKLER);
options.setSortMode(SuggestionSortMode.POPULARITY);

Map<String, SuggestionResult> suggestions = session
    .query(Employee.class, Employees_ByFullName.class)
    .suggestUsing(builder ->
        builder.byField("FullName", "johne")
            .withOptions(options))
    .execute();
from index 'Employees/ByFullName' 
select suggest('FullName', 'johne', '{ "Accuracy" : 0.4, "PageSize" : 5, "Distance" : "JaroWinkler", "SortMode" : "Popularity" }')

Example II

SuggestionWithTerm suggestionWithTerm = new SuggestionWithTerm("FullName");
suggestionWithTerm.setTerm("johne");

Map<String, SuggestionResult> suggestions = session
    .query(Employee.class, Employees_ByFullName.class)
    .suggestUsing(suggestionWithTerm)
    .execute();
from index 'Employees/ByFullName' 
select suggest('FullName', 'johne')