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

query.suggestUsing(suggestion);

query.suggestUsing(suggestionBuilder);
Parameters
suggestion SuggestionBase Defines the type of suggestion that should be executed
suggestionBuilder (SuggestionBuilder) => void Builder with a fluent API that constructs a SuggestionBase instance

Builder

suggestionBuilder.byField(fieldName, term);

suggestionBuilder.byField(fieldName, terms);

suggestionBuilder.withOptions(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 object Non-default options that should be used for operation
   pageSize number Maximum number of suggestions that will be returned
   distance StringDistanceTypes String distance algorithm to use (None, Levenshtein, JaroWinkler, NGram)
   accuracy number Suggestion accuracy
   sortMode SuggestionSortMode Indicates in what order the results should be returned (None, Popularity)

Example I

const suggestions = await session
    .query({ indexName: "Employees/ByFullName" })
    .suggestUsing(builder =>
        builder.byField("FullName", "johne")
            .withOptions({
                accuracy: 0.4,
                pageSize: 5,
                distance: "JaroWinkler",
                sortMode: "Popularity"
            }))
    .execute();
from index 'Employees/ByFullName' 
select suggest('FullName', 'johne', '{ "Accuracy" : 0.4, "PageSize" : 5, "Distance" : "JaroWinkler", "SortMode" : "Popularity" }')

Example II

const suggestionWithTerm = new SuggestionWithTerm("fullName");
suggestionWithTerm.term = "johne";

const suggestions = await session
    .query({ indexName: "Employees/ByFullName" })
    .suggestUsing(suggestionWithTerm)
    .execute();
from index 'Employees/ByFullName' 
select suggest('FullName', 'johne')