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

时间:2017-04-20 05:06:46

标签: elasticsearch tokenize nest analysis analyzer

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

Original ElasticSearch trying to be converted to NEST

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命令转换此示例时遇到问题。这是我到目前为止所做的,它编译并创建索引,但前缀子串的子字段不存在。

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")
                                         )
                                     )
                                 )
                            )
                        )
                    )
                );

1 个答案:

答案 0 :(得分:1)

似乎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。希望有所帮助。