将NEST 6.6迁移到Elasticsearch客户端8,不清楚如何定义分析器

2admgd59  于 2022-12-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(149)

我正在尝试将我的Elasticsearch迁移到8,但我很难理解如何正确地迁移我的分析器定义。
这是我以前使用NEST 6.6时的代码(尽可能简化):

Client.CreateIndex(
  index => index.Settings(
    settings => settings.Analysis(
       analysis => analysis.TokenFilters(
       tokenFilter => tokenFilter.Synonym("synonym", syn => syn.SynonymsPath("analysis/synonym.txt"))
    ).Analyzers(analyzers => analyzers
       .Custom("mycustom", cust => cust
         .Filters("stop", "synonym").Tokenizer("standard")
        )
    )
  )
)

这将创建具有以下内容的索引:

{
  "settings": {
    "index": {
      "analysis": {
        "filter": {
          "synonym": {
            "type": "synonym",
            "synonyms_path": "analysis/synonym.txt"
          }
        },
        "analyzer": {
          "mycustom": {
            "filter": [
              "stop",
              "synonym"
            ],
            "type": "custom",
            "tokenizer": "standard"
          }
        }
      }
    }
  }
}

下面是我尝试迁移到Elastic.clients.Elasticsearch 8:

Client.Indices.Create(index => index.Settings(
 settings => setting.Analysis(
   analysis => analysis
     .Filter(tokenFilter => tokenFilter.Add(
        "synonym", new TokenFilter(new TokenFilterDefinitions(
            // This is where I start getting lost
            new Dictionary<string, ITokenFilterDefinition> {
            { "synonym", new SynonymTokenFilter() { // What are the keys meant to be?
                  SynonymsPath = "analysis/synonym.txt"
             } } }))))
     .Analyzer(analyzers =>
        analyzers.Custom("mycustom", cust => cust.Filter(new[] {"stop", "synonym"})
                 .Tokenizer("standard"))
        )) 
)

这显然是错误的语法,因为生成的JSON请求如下所示:

"filter": {
    "synonym": {
        "synonym": {
            "synonyms_path": "analysis/synonym.txt",
            "type": "synonym"
         }
    }
...

我也试过:

tokenFilter.Add("synonym", new SynonymTokenFilter() { 
   SynonymsPath = "analysis/synonym.txt"
})

尝试将其在JSON层次结构中上移,但由于SynonymTokenFiltertokenFilter.Add所需的TokenFilter不兼容,因此无法编译。
我仍然不明白如何在代码中重新创建与以前相同类型的索引。

ctzwtxfj

ctzwtxfj1#

我也在Elastic discussion forums上问过同样的问题,得到的回答是这是新客户机自动代码生成的一个bug,并引发了issue
在此期间,我使用如下代码解决了这个问题:

var response = Client.Transport.Request<CreateIndexResponse>(
    HttpMethod.PUT,
    $"/{indexName}",
    PostData.String("<actual index definition as a JSON string>")
);

这是可行的,但并不理想,因为我们必须维护索引定义的JSON,而不是在代码中定义它。

相关问题