I am using completion suggesters to provide search suggestions as the user types. I added an IsActive
property to my C# class, and I cannot figure out how to use that in the .Suggest.Completion
query. I know it has something to do with contexts but I cannot find any examples for elasticsearch / nest 6.8.
How do I update my mappings and query to prevent documents with IsActive=false from being suggested?
This is my backing class:
[ElasticsearchType(
IdProperty = "search"
)]
public class SearchCompletion
{
public string search { get; set; }
/// <summary>
/// Use this field for aggregations and sorts
/// </summary>
[Keyword]
public string search_keyword { get; set; }
public bool isActive { get; set; }
/// <summary>
/// To use for sorting results when searching SearchCompletions
/// directly since you can't sort by the Completionfield.Weight
/// property for some reason
/// </summary>
public int weight { get; set; }
public CompletionField suggest { get; set; }
}
This is my Mapping Method:
public static void MapSearchCompletions(ElasticClient client, string index)
{
var mapResponse = client.Map<SearchCompletion>(m => m
.Index(index)
.AutoMap()
// WHAT GOES HERE??
.Properties(props => props
.Completion(c => c
.Name(n => n.isActive)
.Contexts(context => context
.Category(cat => cat.Name("isActive"))
)
)
)
); //re-apply the index mapping
}
This is My Query
var response = client.Search<SearchCompletion>(s => s
.Index(suggest_index)
.Suggest(su => su
.Completion("search", cs => cs
.Field(f => f.suggest)
.Contexts(con => con.Context("")) // WHAT GOES HERE??
.Prefix(search)
.Size(size)
)
)
);