How to remove dynamic field from mapping with Elastic Reindex API

We have a dynamic field defined in multiple indexes that is of type geo_shape, and uses the points_only param. Due to a) the deprecation of points_only in version 7.x, and b) the fact that we don't use that field any more, we want to remove it from the mapping and the data, although the mapping is the most important, since we don't search on that field.

First, here is the mapping definition:

"mappings": {
    "dynamic_templates": [
        {
            "base_geo": {
                "match": "*Geo",
                "mapping": {
                    "points_only": true,
                    "type": "geo_shape"
                }
            }
        },
    ...
    ]
}

It appears that the Reindex API can be used to do this, since in order to remove a field from a mapping, a new index has to be created. As such, I've been trying variations on this to POST _reindex

{
    "source": {
        "index": "local_federal_agency_models_1708127195"
    },
    "dest": {
        "index": "local_federal_agency_models_1708127195_3"
    },
    "script": {
        "source": "ctx._source.remove('base_geo')"
    }
}

However, this not only removes the base_geo field, but it removes the entire dynamic_templates array, so it removes all dynamic mappings.
As for the documents themselves, I know I can use an ingest pipeline, but how can I just remove my base_geo field mapping when re-indexing?

Hi @sedwardsgt, a reindex operation does not copy the mapping definition of the original index. It only copies the source data (not the mapped data) from one index to the other.

To achieve your intended purpose, you should:

  1. Create a new empty index
  2. Set the mapping of that index to what you want it to be. In this case that's presumably the same as the source index, but without your base_geo dynamic template mapping
  3. Run reindex from the original index to the new index

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.