您对问题的回答越具体,所获得的答案就越好。不过,这里有一个索引,该索引设置了一个分析器(带有过滤器)和令牌生成器(EdgeNGram),然后使用它们在Tag类的Name字段上创建自动完成索引。
public class Tag{ public string Name { get; set; }}Nest.IElasticClient client = null; // Connect to ElasticSearchvar createResult = client.CreateIndex(indexName, index => index .Analysis(analysis => analysis .Analyzers(a => a .Add( "autocomplete", new Nest.CustomAnalyzer() { Tokenizer = "edgeNGram", Filter = new string[] { "lowercase" } } ) ) .Tokenizers(t => t .Add( "edgeNGram", new Nest.EdgeNGramTokenizer() { MinGram = 1, MaxGram = 20 } ) ) ) .AddMapping<Tag>(tmd => tmd .Properties(props => props .MultiField(p => p .Name(t => t.Name) .Fields(tf => tf .String(s => s .Name(t => t.Name) .Index(Nest.FieldIndexOption.not_analyzed) ) .String(s => s .Name(t => t.Name.Suffix("autocomplete")) .Index(Nest.FieldIndexOption.analyzed) .IndexAnalyzer("autocomplete") ) ) ) ) ));在github上的NEST的单元测试项目中,还有一个相当完整的映射示例。
https://github.com/elasticsearch/elasticsearch-
net/blob/develop/src/Tests/Nest.Tests.Unit/Core/Map/FluentMappingFullExampleTests.cs
编辑:
要查询索引,请执行以下操作:
string queryString = ""; // search stringvar results = client.Search<Tag>(s => s .Query(q => q .Text(tq => tq .onField(t => t.Name.Suffix("autocomplete")) .QueryString(queryString) ) ));


