更新以显示工作示例
我正在尝试对 ElasticSearch 中的用户名集合进行部分搜索。
四处搜寻已将我指向nGram Tokenizer
方向,但我对正确的实施感到困惑,也没有得到任何结果。
这是从我正在处理的项目中剥离的相关代码。
我尝试了不同的组合和搜索类型,但无济于事。
安装程序.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);
配置文件.cs
public class Profile
{
public string Id { get; set; }
[ElasticProperty(IndexAnalyzer = "mynGram")]
public string Username { get; set; }
}
任何提示将不胜感激。