如何使用自定义分析器创建ElasticSearch NEST v.5客户端的索引?

时间:2017-02-02 10:22:22

标签: c# elasticsearch nest

使用NEST v.5创建索引的正确方法是什么?我在这里看到了类似的帖子:Specifying and using a NGramTokenizer with the C# NEST client for Elastic Search。但似乎API已经改变了。我可以通过以下方式完成:

ConnectionSettings settings = new ConnectionSettings(new Uri("http://localhost:9200"));

IndexSettings indexSettings = new IndexSettings();
            CustomAnalyzer customAnalyzer = 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
                                                  });

elasticClient = new ElasticClient(settings);

            elasticClient.CreateIndex("taskmanager", s => s
                .Settings(sett => sett
                    .Analysis(a => a
                        .Analyzers(anl => anl
                            .Custom("customAnalyzer", c => c
                                // how to set my custom analyzer?
                                .Tokenizer("mynGram")

                            )
                        )
                    )
                )
            );

问题是我不知道如何使用流畅的API设置我的设置。

1 个答案:

答案 0 :(得分:1)

我在博客上找到了答案:ELASTIC SEARCH : CREATE INDEX USING NEST IN .NET

第一个我需要创建索引设置和自定义分析器:

IndexSettings indexSettings = new IndexSettings();
CustomAnalyzer customAnalyzer = 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
                                                  });

我的错误是我没有使用包含我们设置的IndexState。

IndexState indexConfig = new IndexState
{
   Settings = indexSettings
};

elasticClient.CreateIndex(“mycustomname”,i =&gt; i      .InitializeUsing(indexConfig) );