在 2.x 中嵌套 CopyTo、分词器和分析器

Nest CopyTo, Tokenizer and Analyzer in 2.x

在旧版本的 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()));

这是我的解决方案

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);