Skip to main content

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

// This dynamic query on the 'Products' collection has NO resulting documents
List<Product> products = session
.Query<Product>()
.Where(x => x.Name == "chaig")
.ToList();
Parameters
suggestionSuggestionBaseDefines the type of suggestion that should be executed
builderAction<ISuggestionBuilder<T>>Builder with a fluent API that constructs a SuggestionBase instance

Builder

// Query for suggested terms for single term:
// ==========================================

Dictionary<string, SuggestionResult> suggestions = session
// Make a dynamic query on collection 'Products'
.Query<Product>()
// Call 'SuggestUsing'
.SuggestUsing(builder => builder
// Request to get terms from field 'Name' that are similar to 'chaig'
.ByField(x => x.Name, "chaig"))
.Execute();
Parameters
fieldNamestringPoints to the index field that should be used for operation
pathExpression<Func<T, object>>Points to the index field that should be used for operation
termstringTerm that will be used as a basis of the suggestions
termsstring[]Terms that will be used as a basis of the suggestions
optionsSuggestionOptionsNon-default options that should be used for operation

Options

// Query for suggested terms for multiple terms:
// =============================================

Dictionary<string, SuggestionResult> suggestions = session
// Make a dynamic query on collection 'Products'
.Query<Product>()
// Call 'SuggestUsing'
.SuggestUsing(builder => builder
// Request to get terms from field 'Name' that are similar to 'chaig' OR 'tof'
.ByField(x => x.Name, new[] { "chaig", "tof" }))
.Execute();
Options
PageSizeintMaximum number of suggestions that will be returned
DistanceStringDistanceTypesString distance algorithm to use (None, Levenshtein, JaroWinkler, NGram)
Accuracyfloat?Suggestion accuracy
SortModeSuggestionSortModeIndicates in what order the results should be returned (None, Popularity)

Example I

Dictionary<string, SuggestionResult> suggestions = session
.Query<Employee, Employees_ByFullName>()
.SuggestUsing(builder => builder
.ByField("FullName", "johne")
.WithOptions(new SuggestionOptions
{
Accuracy = 0.4f,
PageSize = 5,
Distance = StringDistanceTypes.JaroWinkler,
SortMode = SuggestionSortMode.Popularity
}))
.Execute();

Example II

Dictionary<string, SuggestionResult> suggestions = session
.Query<Employee, Employees_ByFullName>()
.SuggestUsing(new SuggestionWithTerm("FullName") { Term = "johne" })
.Execute();