gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:49:01 24 4
gpt4 key购买 nike

使用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来设置设置。

最佳答案

我在博客上找到了答案: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)
);

关于c# - 如何使用自定义分析器创建ElasticSearch NEST v.5客户端的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41999609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com