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 | ||
---|---|---|
suggestion | SuggestionBase | Defines the type of suggestion that should be executed |
builder | Action<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 | ||
---|---|---|
fieldName | string | Points to the index field that should be used for operation |
path | Expression<Func<T, object>> | 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 |
displayName | string | User defined friendly name for suggestion result. If null , field name will be used. |
options | SuggestionOptions | Non-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 | ||
---|---|---|
PageSize | int | Maximum number of suggestions that will be returned |
Distance | StringDistanceTypes | String distance algorithm to use (None , Levenshtein , JaroWinkler , NGram ) |
Accuracy | float? | Suggestion accuracy |
SortMode | SuggestionSortMode | Indicates in what order the results should be returned (None , Popularity ) |
Multiple suggestions
You are able to ask for multiple suggestions using a single query.
// Query for suggested terms for single term:
// ==========================================
Dictionary<string, SuggestionResult> suggestions = await asyncSession
// 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"))
.ExecuteAsync();
Example I
- Sync
- Async
- RQL
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();
Dictionary<string, SuggestionResult> suggestions = await asyncSession
.Query<Employee, Employees_ByFullName>()
.SuggestUsing(builder => builder
.ByField("FullName", "johne")
.WithOptions(new SuggestionOptions
{
Accuracy = 0.4f,
PageSize = 5,
Distance = StringDistanceTypes.JaroWinkler,
SortMode = SuggestionSortMode.Popularity
}))
.ExecuteAsync();
from index 'Employees/ByFullName'
select suggest('FullName', 'johne', '{ "Accuracy" : 0.4, "PageSize" : 5, "Distance" : "JaroWinkler", "SortMode" : "Popularity" }')
Example II
- Sync
- Async
- RQL
Dictionary<string, SuggestionResult> suggestions = session
.Query<Employee, Employees_ByFullName>()
.SuggestUsing(new SuggestionWithTerm("FullName") { Term = "johne" })
.Execute();
Dictionary<string, SuggestionResult> suggestions = await asyncSession
.Query<Employee, Employees_ByFullName>()
.SuggestUsing(new SuggestionWithTerm("FullName") { Term = "johne" })
.ExecuteAsync();
from index 'Employees/ByFullName'
select suggest('FullName', 'johne')
Example III
Looking for suggestions with dynamic query usage for multiple fields:
- Sync
- Async
- RQL
Dictionary<string, SuggestionResult> suggestions = session.Query<Employee>()
.SuggestUsing(x => x
.ByField(y => y.FirstName, "johne")
.WithDisplayName("CustomLastName"))
.AndSuggestUsing(x => x
.ByField(y => y.LastName, "owen"))
.Execute();
Dictionary<string, SuggestionResult> suggestions = await asyncSession.Query<Employee>()
.SuggestUsing(x => x
.ByField(y => y.FirstName, "johne")
.WithDisplayName("CustomFirstName"))
.AndSuggestUsing(x => x
.ByField(y => y.LastName, "owen"))
.ExecuteAsync();
from Employees
select suggest('FirstName', 'johne') as CustomFirstName, suggest('LastName', 'owen')