I have an Object in that I have a field Name Tags of type string.
Ex: "Dubai,India, Australia"
I implemeted AutoComplete for this, if I try to search Dubai i.e; Dub will Dubai be shown as a autocomplete tag or the complete "Dubai,India, Australia" will be shown.
public async Task<List<Tag>> Autocomplete(string query)
{
var response = await this.client.SearchAsync<BlogPost>(sr => sr
.Suggest(scd => scd
.Completion("tag-completion", cs => cs
.Prefix(query)
.Fuzzy(fsd => fsd
.Fuzziness(Fuzziness.Auto))
.Field(r => r.Tags))));
List<Tag> tagSuggestions = ExtractAutocompleteSuggestions(response);
return tagSuggestions;
}
private List<Tag> ExtractAutocompleteSuggestions(ISearchResponse<BlogPost> response)
{
List<Tag> results = new List<Tag>();
var suggestions = response.Suggest["tag-completion"].Select(s => s.Options);
suggestions.ToList().ForEach(s =>
{
results.AddRange(s.Select(opt => new Tag
{
Tags = opt.Source.Tags
}));
});
return results;
}
This is the code that I Implemented for the Model brlow.
public class BlogPost
{
public string BlogId { get; set; }
public string Content { get; set; }
[Completion]
public string Tags { get; set; }
public DateTime PostedDate { get; set; }
public string Category { get; set; }
[Text]
public string Title { get; set; }
}