在旧版本的Nest中我能够做到这一点:
var newIndexRequest = client.CreateIndex(ci => ci.Index(IndexName.ToLower())
.Analysis(a => a
.Analyzers(an => an
.Add("search_ngram_analyzer", customAnalyzer))
.Tokenizers(t => t
.Add("search_ngram_tokenizer", new NGramTokenizer
{
MaxGram = 500, // qty of chars to separate into tokens
MinGram = 2
})))
.AddMapping<Models.CustomerSearchResult>(m => m
.MapFromAttributes()
.Properties(p => p
.String(s => s.Name("short_name").CopyTo("search").Index(FieldIndexOption.NotAnalyzed))
.String(s => s.Name("legacy_name").CopyTo("search").Index(FieldIndexOption.NotAnalyzed))
.String(s => s.Name("legacy_contact_name").CopyTo("search").Index(FieldIndexOption.NotAnalyzed))
.String(s => s.Name("search").Index(FieldIndexOption.Analyzed).Analyzer("search_ngram_analyzer"))
)
));
由于文档尚未更新(或者我无法找到它),我无法弄清楚如何在Nest 2.x中执行相同的操作。)
..我知道我需要使用AutoMap而不是MapFromAttributes,但不知道如何正确设置Analyzer,Tokenizer和CopyTo函数。
到目前为止,这就是我的全部内容:
var customerSearchIdxDesc = new CreateIndexDescriptor(Constants.ElasticSearch.CustomerSearchIndexName)
.Mappings(ms => ms.Map<SO.Services.Data.ServiceModel.DtoTypes.Customer.SearchResult>(m => m.AutoMap()));
var response = client.CreateIndex(customerSearchIdxDesc);
更新
越来越近了我想:/
var customerSearchIdxDesc = new CreateIndexDescriptor(Constants.ElasticSearch.CustomerSearchIndexName)
.Settings((f) =>
{
return f.Analysis(analysis => analysis
.Analyzers(analyzers => analyzers
.Custom("search_ngram_analyzer", a => a
.Filters("lowercase")
.Tokenizer("search_ngram_tokenizer")))
.Tokenizers(tokenizers => tokenizers
.NGram("search_ngram_tokenizer", t => t
.MinGram(2)
.MaxGram(500)
.TokenChars(TokenChar.Digit, TokenChar.Letter, TokenChar.Punctuation, TokenChar.Punctuation, TokenChar.Symbol))));
})
.Mappings(ms => ms.Map<SO.Services.Data.ServiceModel.DtoTypes.Customer.SearchResult>(m => m.AutoMap()));
答案 0 :(得分:1)
这是我的解决方案
var customerSearchIdxDesc = new CreateIndexDescriptor(Constants.ElasticSearch.CustomerSearchIndexName)
.Settings(f =>
f.Analysis(analysis => analysis
.Analyzers(analyzers => analyzers
.Custom(Constants.ElasticSearch.AnalyzerNames.LowercaseNGram, a => a
.Filters("lowercase")
.Tokenizer(Constants.ElasticSearch.TokenizerNames.NoWhitespaceNGram)))
.Tokenizers(tokenizers => tokenizers
.NGram(Constants.ElasticSearch.TokenizerNames.NoWhitespaceNGram, t => t
.MinGram(2)
.MaxGram(500)
.TokenChars(TokenChar.Digit, TokenChar.Letter, TokenChar.Punctuation, TokenChar.Punctuation, TokenChar.Symbol)
)
)
)
)
.Mappings(ms => ms.Map<ServiceModel.DtoTypes.Customer.SearchResult>(m => m
.AutoMap()
.Properties(p => p
.String(n => n.Name(c => c.CustomerName).CopyTo(f =>
{
return new FieldsDescriptor<string>().Field("search");
}).Index(FieldIndexOption.Analyzed).Analyzer(Constants.ElasticSearch.AnalyzerNames.LowercaseNGram))
.String(n => n.Name(c => c.ContactName).CopyTo(fs => fs.Field(Constants.ElasticSearch.CombinedSearchFieldName)).Index(FieldIndexOption.Analyzed).Analyzer(Constants.ElasticSearch.AnalyzerNames.LowercaseNGram))
.String(n => n.Name(c => c.City).CopyTo(fs => fs.Field(Constants.ElasticSearch.CombinedSearchFieldName)).Index(FieldIndexOption.Analyzed).Analyzer(Constants.ElasticSearch.AnalyzerNames.LowercaseNGram))
.String(n => n.Name(c => c.StateAbbreviation).CopyTo(fs => fs.Field(Constants.ElasticSearch.CombinedSearchFieldName)).Index(FieldIndexOption.Analyzed).Analyzer(Constants.ElasticSearch.AnalyzerNames.LowercaseNGram))
.String(n => n.Name(c => c.Country).CopyTo(fs => fs.Field(Constants.ElasticSearch.CombinedSearchFieldName)).Index(FieldIndexOption.Analyzed).Analyzer(Constants.ElasticSearch.AnalyzerNames.LowercaseNGram))
.String(n => n.Name(c => c.PostalCode).CopyTo(fs => fs.Field(Constants.ElasticSearch.CombinedSearchFieldName)).Index(FieldIndexOption.Analyzed).Analyzer(Constants.ElasticSearch.AnalyzerNames.LowercaseNGram))
.String(n => n.Name(Constants.ElasticSearch.CombinedSearchFieldName).Index(FieldIndexOption.Analyzed).Analyzer(Constants.ElasticSearch.AnalyzerNames.LowercaseNGram))
)
)
);
var response = client.CreateIndex(customerSearchIdxDesc);