ElasticSearch模式_语法_异常在索引附近非法重复

5cnsuln7  于 2022-10-06  发布在  ElasticSearch
关注(0)|答案(1)|浏览(155)

我正在尝试使用.NET客户端(Nest ver.7.17.4与弹性8.4),我得到了以下例外情况。

Elasticsearch.Net.ElasticsearchClientException: 'Request failed to execute. Call: Status code 400 from: PUT /temp-index-for-integration-tests?pretty=true&error_trace=true. ServerError: Type: pattern_syntax_exception Reason: "Illegal repetition near index 80
([^\p{L}\d]+)|(?<=\D)(?=\d)|(?<=\d)(?=\D)|(?<=[\p{L}&&[^\p{Lu}]])(?=\p{Lu})|(?<=\p{Lu})(?=\p{Lu}[\p{L}&&[^\p{Lu}]])
                                                                          ^"'

这个正则表达式在从开发控制台创建时似乎工作得很好,所以不确定我遗漏了什么。

以下是作品:

PUT test-index-3
{
  "settings": {
    "analysis": {
      "analyzer": {
        "camel": {
          "type": "pattern",
          "pattern": "([^\p{L}\d]+)|(?<=\D)(?=\d)|(?<=\d)(?=\D)|(?<=[\p{L}&&[^\p{Lu}]])(?=\p{Lu})|(?<=\p{Lu})(?=\p{Lu}[\p{L}&&[^\p{Lu}]])"
        }
      }
    }
  }
}

这不会在Indices.CreateAsync上引发异常

public async Task IndexCreateAsync(IEnumerable<IFieldDefinition> fieldDefinitions, string indexName)
{
    Dictionary<PropertyName, IProperty> indexFields = fieldDefinitions
        .Select(f => _elasticsearchMapper.Map(f))
        .ToDictionary(p => p.Name, p => p);

    PutMappingRequest mappings = new PutMappingRequest(indexName)
    {
        Properties = new Properties(indexFields)
    };

    var patternAnalyzer = new PatternAnalyzer
    {
        Pattern = @"([^\p{L}\d]+)|(?<=\D)(?=\d)|(?<=\d)(?=\D)|(?<=[\p{L}&&[^\p{Lu}]])(?=\p{Lu})|(?<=\p{Lu})(?=\p{Lu}[\p{L}&&[^\p{Lu}]])",
        Lowercase = true
    };

    IndexState indexSettings = new IndexState
    {
        Mappings = mappings,
        Settings = new IndexSettings
        {
            Analysis = new Analysis
            {
                Analyzers = new Analyzers
                {
                    {
                        ElasticsearchConstants.TextAnalysis.CustomAnalyzers.CustomPatternRegexCasesAndSpecialChars,
                        patternAnalyzer
                    }
                }
            }
        }
    };

    await _elasticClient.Indices.CreateAsync(indexName, s => s.InitializeUsing(indexSettings));
}
enyaitl3

enyaitl31#

OK找到了解决方案。我猜在弹性云中使用开发人员控制台和使用.NET客户端时会有不同的序列化

基本上,我需要做的是用一个斜杠\替换所有的\\字符。

因此,当在.NET代码中将Regex更改为

var patternAnalyzer = new PatternAnalyzer
            {
                Pattern = @"([^p{L}d]+)|(?<=D)(?=d)|(?<=d)(?=D)|(?<=[p{L}&&[^p{Lu}]])(?=p{Lu})|(?<=p{Lu})(?=p{Lu}[p{L}&&[^p{Lu}]])"
            };

一切都开始起作用了!

相关问题