尝试使用具有分析器的 dynamic_templates 和 search_analyzer 添加映射时出现警告

Warning while trying to add mapping with dynamic_templates having analyzer and search_analyzer

我正在使用 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)

我正在使用 Using elasticsearch "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"
                                    }
                                }
                             }
                        ]
            }

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

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

Only text fields support the analyzer mapping parameter.

您必须将字段类型更改为文本或根本不为关键字字段指定任何分析器。您还可以在 Elastic 讨论页面上使用 normalizers to apply token filters to your keyword fields. As mentioned in the answer from this question

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

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