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.


ISuggestionDocumentQuery<T> suggestUsing(SuggestionBase suggestion);

ISuggestionDocumentQuery<T> suggestUsing(Consumer<ISuggestionBuilder<T>> builder);
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


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

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

ISuggestionOperations<T> withOptions(SuggestionOptions options);
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


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
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();

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

Example II

SuggestionWithTerm suggestionWithTerm = new SuggestionWithTerm("FullName");

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