如何使用自定义分析器创建 ElasticSearch NEST v.5 客户端的索引?
How to create index of ElasticSearch NEST v.5 client by using custom analyzer?
使用 NEST v.5 创建索引的正确方法是什么?我在这里看到了类似的 post: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")
)
)
)
)
);
问题是我不知道如何使用 fluent 来设置我的设置 API。
我在博客上找到了答案: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 => i
.InitializeUsing(indexConfig)
);
使用 NEST v.5 创建索引的正确方法是什么?我在这里看到了类似的 post: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")
)
)
)
)
);
问题是我不知道如何使用 fluent 来设置我的设置 API。
我在博客上找到了答案: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 => i .InitializeUsing(indexConfig) );