Search code examples
pythonelasticsearchelastic-stackelasticsearch-5elasticsearch-dsl

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


I am using elasticsearch python client to connect to elasticsearch.

While trying to add mapping to index, I am getting following warning:

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)

And while indexing the record, got this warning:

/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)

I am using Using elasticsearch "7.15.1"

pip packages:

elasticsearch==7.15.1

elasticsearch-dsl==7.4.0

My settings and mappings are:

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"
                                    }
                                }
                             }
                        ]
            }

I need help in adjusting the mapping, this mapping was working fine on elastic 6.0.1. After upgrading to 7.15.1 started getting warning.


Solution

  • You are trying to set an analyzer on a keyword field. The Elasticsearch analyzer documentation states at the top of the page:

    Only text fields support the analyzer mapping parameter.

    You have to change the type of your field to text or specify no analyzer at all for the keyword fields. You can also use normalizers to apply token filters to your keyword fields. As mentioned in the answer from this question on the Elastic discuss page.

    The trim token filter that you want to use is not explicitly mentioned in the list of compatible filters, but I tried it with the Kibana dev tools, and it seems to work:

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