ElasticSearch NEST 手动映射分析器需要的子字段

ElasticSearch NEST manually map subfields needed for analyzer

我已经能够根据下面的原始请求获得正确的 ElasticSearch 命令来插入和搜索我的记录:

PUT /sample
{
  "settings": {
    "index.number_of_shards": 5,
    "index.number_of_replicas": 0,
    "analysis": {
      "filter": {
        "nGram_filter": {
          "type": "nGram",
          "min_gram": 2,
          "max_gram": 20,
          "token_chars": [
            "letter",
            "digit"
          ]
        },
        "edgenGram_filter": {
          "type": "edgeNGram",
          "min_gram": 2,
          "max_gram": 20
        }
      },
      "analyzer": {
        "ngram_index_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "lowercase",
            "nGram_filter"
          ]
        },
        "edge_ngram_index_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "lowercase",
            "edgenGram_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "test": {
      "properties": {
        "name": {
          "type": "string",
          "fields": {
            "prefixes": {
              "type": "string",
              "analyzer": "edge_ngram_index_analyzer",
              "search_analyzer": "standard"
            },
            "substrings": {
              "type": "string",
              "analyzer": "ngram_index_analyzer",
              "search_analyzer": "standard"
            }
          }
        }
      }
    }
  }
}

但是,我现在在尝试使用 NEST 命令转换此示例时遇到问题。这是我到目前为止所做的,它编译并创建索引,但是 prefixessubstrings 的子字段不存在。

Client.CreateIndex("sample", i => i
                    .Settings(s => s
                        .NumberOfShards(10) 
                        .NumberOfReplicas(0)
                        .Analysis(a => a
                            .TokenFilters(tf => tf
                                .NGram("nGram_filter", td => td
                                    .MinGram(2)
                                    .MaxGram(20)
                                 )
                                .EdgeNGram("edgeNGram", td => td
                                    .MinGram(2)
                                    .MaxGram(20)
                                 )
                            )
                            .Analyzers(anz => anz
                                .Custom("ngram_index_analyzer", cc => cc
                                    .Tokenizer("keyword")
                                    .Filters("lowercase", "nGram_filter")
                                )
                                .Custom("edge_ngram_index_analyzer", cc => cc
                                    .Tokenizer("keyword")
                                    .Filters("lowercase", "edgenGram_filter")
                                )
                            )
                        )
                    )
                    .Mappings(m => m
                        .Map<test>(map => map
                            .Properties(ps => ps
                                 .Text(t => t
                                     .Name(n => n.name)                 
                                     .Fields(f => f
                                         .Text(tt => tt
                                            .Name("prefixes")
                                            .Analyzer("edge_ngram_index_analyzer")
                                            .SearchAnalyzer("standard")
                                         )
                                         .Text(tt => tt
                                            .Name("substrings")
                                            .Analyzer("ngram_index_analyzer")
                                            .SearchAnalyzer("standard")
                                         )
                                     )
                                 )
                            )
                        )
                    )
                );

EdgeNGram 令牌过滤器名称似乎有错字。一个条目是 edgeNGram,而另一个条目是 edgenGram_filter。所以当我 运行 下面的映射使用 nest

client.CreateIndex("sample", i => i
        .Settings(s => s
            .NumberOfShards(10)
            .NumberOfReplicas(0)
            .Analysis(a => a
                .Analyzers(anz => anz
                    .Custom("ngram_index_analyzer", cc => cc
                        .Tokenizer("keyword")
                        .Filters("lowercase", "nGram_filter")
                    )
                    .Custom("edge_ngram_index_analyzer", cc => cc
                        .Tokenizer("keyword")
                        .Filters("lowercase", "edgeNGram_filter")
                    )
                )
                .TokenFilters(tf => tf
                    .NGram("nGram_filter", td => td
                        .MinGram(2)
                        .MaxGram(20)
                        )
                    .EdgeNGram("edgeNGram_filter", td => td
                        .MinGram(2)
                        .MaxGram(20)
                        )
                )
            )
        )
        .Mappings(m => m
            .Map<test>(map => map
                .Properties(ps => ps
                        .Text(t => t
                            .Name(n => n.name)
                            .Fields(f => f
                                .Text(tt => tt
                                .Name("prefixes")
                                .Analyzer("edge_ngram_index_analyzer")
                                .SearchAnalyzer("standard")
                                )
                                .Text(tt => tt
                                .Name("substrings")
                                .Analyzer("ngram_index_analyzer")
                                .SearchAnalyzer("standard")
                                )
                            )
                        )
                )
            )
        )
    );

它将其转换为适当的 JSON,包括两个子字段。希望对您有所帮助。