尝试使用具有分析器和搜索分析器的动态模板添加映射时出现警告

Posted

技术标签:

【中文标题】尝试使用具有分析器和搜索分析器的动态模板添加映射时出现警告【英文标题】:Warning while trying to add mapping with dynamic_templates having analyzer and search_analyzer 【发布时间】:2021-12-14 05:45:36 【问题描述】:

我正在使用elasticsearch python客户端连接elasticsearch。

在尝试将映射添加到索引时,我收到以下警告:

es.indices.put_mapping(index=index, body=mappings)

/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/base.py:209: ElasticsearchWarning: ], attempted to validate it with the following match_mapping_type: [string], caused by [unknown parameter [search_analyzer] on mapper [__dynamic__attributes] of type [keyword]]
/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/base.py:209: ElasticsearchWarning: ], attempted to validate it with the following match_mapping_type: [string], caused by [unknown parameter [search_analyzer] on mapper [__dynamic__metadata] of type [keyword]]
  warnings.warn(message, category=ElasticsearchWarning)

在索引记录时,收到以下警告:

/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/base.py:209: ElasticsearchWarning: Parameter [search_analyzer] is used in a dynamic template mapping and has no effect on type [keyword]. Usage will result in an error in future major versions and should be removed.
  warnings.warn(message, category=ElasticsearchWarning)
/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/base.py:209: ElasticsearchWarning: Parameter [analyzer] is used in a dynamic template mapping and has no effect on type [keyword]. Usage will result in an error in future major versions and should be removed.
  warnings.warn(message, category=ElasticsearchWarning)

我正在使用使用弹性搜索“7.15.1”

pip 包:

elasticsearch==7.15.1

elasticsearch-dsl==7.4.0

我的设置和映射是:

settings = "analysis": "analyzer": "my_analyzer": 
                                      "type": "custom",
                                      "tokenizer": "keyword",
                                      "filter": ["trim"]
                                      
                         
            
mappings = "dynamic_templates": [
                            "attributes": 
                                "match_mapping_type": "string",
                                "path_match": "attributes.*",
                                "mapping": 
                                    "type": "keyword",
                                    "analyzer": "my_analyzer",
                                    "search_analyzer": "my_analyzer"
                                    
                                
                             ,
                            "metadata": 
                                "match_mapping_type": "string",
                                "path_match": "metadata.*",
                                "mapping": 
                                    "type": "keyword",
                                    "analyzer": "my_analyzer",
                                    "search_analyzer": "my_analyzer"
                                    
                                
                             
                        ]
            

我在调整映射方面需要帮助,此映射在弹性 6.0.1 上运行良好。升级到 7.15.1 后开始收到警告。

【问题讨论】:

【参考方案1】:

您正在尝试在 keyword 字段上设置分析器。页面顶部的Elasticsearch analyzer documentation 状态:

只有 文本 字段支持分析器映射参数。

您必须将字段的类型更改为文本或根本不为关键字字段指定任何分析器。您还可以使用 normalizers 将令牌过滤器应用于您的关键字字段。正如 Elastic 讨论页面上 question 的回答中所述。

兼容过滤器列表中未明确提及您要使用的修剪令牌过滤器,但我使用 Kibana 开发工具尝试过,它似乎有效:

PUT normalizer_trim

  "settings": 
    "analysis": 
      "normalizer": 
        "my_normalizer": 
          "type": "custom",
          "filter": ["lowercase", "trim"]
        
      
    
  ,
  "mappings": 
    "properties": 
      "foo": 
        "type": "keyword",
        "normalizer": "my_normalizer"
      
    
  

【讨论】:

知道了。我只想要我的字段keyword,因为想要完全匹配而不想要中断文本进行搜索。那我怎么能用trimfiltertypekeyword。我想在索引和搜索时删除前导和尾随空格。 令牌过滤器可以应用于带有规范化器的关键字字段,我调整了我的答案以包含此信息。 谢谢,成功了!

以上是关于尝试使用具有分析器和搜索分析器的动态模板添加映射时出现警告的主要内容,如果未能解决你的问题,请参考以下文章

Logstash动态模板映射收集Nginx的Json格式的日志

集合和映射(Set And Map)

如何在具有动态大小数组的模板类中重载 operator=

ElasticSearch之动态映射和模板

Azure 搜索语言分析器和变音符号

如何使用 angularjs 处理具有动态过滤添加的高级搜索?