如何用燕窝数组值设置属性添加到ES-指数(How to add settings property

2019-11-04 02:28发布

“我想在指数使用ElasticSearch和NEST .NET客户端自动完成SEACH去创造。我下面就本教程http://qbox.io/blog/an-introduction-to-ngrams-in-elasticsearch和创建索引和它的设置时遇到了问题Sepcifically我想创建具有以下设置的指数(取直出本教程的TL的; DR,首先):

PUT /test_index
{
  "settings": {
    "analysis": {
      "filter": {
        "edge_ngram_filter": {
           "type": "edge_ngram",
           "min_gram": 2,
           "max_gram": 20
        }
     },
     "analyzer": {
        "edge_ngram_analyzer": {
           "type": "custom",
           "tokenizer": "standard",
           "filter": [
              "lowercase",
              "edge_ngram_filter"
           ]
        }
     }
  }
}

我的问题是,我不知道如何发送的"filter": [ "lowercase", "edge_ngram_filter" ]使用NEST部分。 我现在的尝试是这样的:

esclient.CreateIndex("test_index", s => s
    .Settings(settings => settings
        .Add("analysis.filter.edge_ngram_filter.type", "edge_ngram")
        .Add("analysis.filter.edge_ngram_filter.min_gram", "2")
        .Add("analysis.filter.edge_ngram_filter.max_gram", "20")
        .Add("analysis.analyzer.edge_ngram_analyzer.type", "custom")
        .Add("analysis.analyzer.edge_ngram_analyzer.tokenizer", "standard")
        // Interesting part on the line below!
        .Add("analysis.analyzer.edge_ngram_analyzer.filter", new string[] { "lowercase", "edge_ngram_filter" })
    )
);

但这种失败枝条一JsonWriterException:“不支持的类型:System.String []使用JsonSerializer类来获取对象的JSON表示路径‘settings.index’。”。 我曾试图简单地手动提供JSON阵列( .Add("analysis.analyzer.edge_ngram_analyzer.filter", "[ \"lowercase\", "\edge_ngram_analyzer\" ]")的JSON代替转义为一个字符串。

是否NEST API提供这样的方法吗?

Answer 1:

UpdateSettings(..)有更愉快的语法来设置分析设置。

这是你如何处理你的情况:

client.UpdateSettings(s => s
    .Index(indexName)
    .Analysis(a => a
        .TokenFilters(f => f.Add("edge_ngram_filter", new EdgeNGramTokenFilter
        {
            MinGram = 2,
            MaxGram = 20
        }))
        .Analyzers(analyzer => analyzer.Add("edge_ngram_analyzer", new CustomAnalyzer
        {
            Tokenizer = "standard",
            Filter = new List<string> {"lowercase", "edge_ngram_filter"}
        }))));

记得关闭索引之前,你会更新索引设置。

希望能帮助到你。

更新:

您可以和创建索引时实现这一目标。 无需更新设置。

client.CreateIndex(indexName, i => i
    .Analysis(a => a
        .TokenFilters(f => f.Add("edge_ngram_filter", new EdgeNGramTokenFilter
        {
            MinGram = 2,
            MaxGram = 20
        }))
        .Analyzers(analyzer => analyzer.Add("edge_ngram_analyzer", new CustomAnalyzer
        {
            Tokenizer = "standard",
            Filter = new List<string> {"lowercase", "edge_ngram_filter"}
        }))));


文章来源: How to add settings property with array value to ES-index using Nest