Updated to show a working sample.
I am trying to do a partial search in a collection of usernames in ElasticSearch.
A search around pointed me in the direction of nGram Tokenizer , but I am not sure about the correct implementation and am not getting any results.
This is the appropriate code removed from the project I'm working on.
I tried different combinations and search types to no avail.
setup.cs
var client = new ElasticClient(settings.ConnectionSettings); // (Try and) Setup the nGram tokenizer. var indexSettings = new IndexSettings(); var custonAnalyzer = new CustomAnalyzer(); customAnalyzer.Tokenizer = "mynGram"; customAnalyzer.Filter = new List<string> { "lowercase" }; indexSettings.Analysis.Analyzers.Add("mynGram", customAnalyzer); indexSettings.Analysis.Tokenizers.Add("mynGram", new NGramTokenizer { MaxGram = 10, MinGram = 2 }); client.CreateIndex(settings.ConnectionSettings.DefaultIndex, indexSettings); client.MapFromAttributes<Profile>(); // Create and add a new profile object. var profile = new Profile { Id = "1", Username = "Russell" }; client.IndexAsync(profile); // Do search for object var s = new SearchDescriptor<Profile>().Query(t => t.Term(c => c.Username, "russ")); var results = client.Search<Profile>(s);
Profile.cs
public class Profile { public string Id { get; set; } [ElasticProperty(IndexAnalyzer = "mynGram")] public string Username { get; set; } }
Any advice would be highly appreciated.
source share