Elasticsearch 5.4 - steps to delete field from only one type

Hello, thank you for your responses, it is really helpful.

I have a question but may be it is similar to other. I have one index with a lot of types with different mapping. At some moment i need to delete one field only from one type, as I understand I have to reindex whole index but how to do it correctly?

I see this way:

  1. get mapping from existing index type and copy it without field to delete; (mapping is created dynamically so I don't know exactly about it and have to ask it for each type)
  2. create new index with types and put this new mapping
  3. scroll all documents of existing index types
  4. index them without deleted field with bulk into new index
  5. drop old index
  6. set alias to new index (i need the same name for index) - there is no way to rename index except to reindex everything again

I suppose this issue is usual. This way seems me very heavy. May be I lost something and my issue may be solved by script or reindex plugin or something else???

Hi @Vikentyi

  1. get mapping from existing index type and copy it without field to delete; (mapping is created dynamically so I don't know exactly about it and have to ask it for each type)
  1. create new index with types and put this new mapping

If all your mappings are dynamic then you can skip this step altogether, and just rely on dynamic mappings.

If you want to create mappings yourself, you can just do GET {index}/_mapping to get all the mappings or GET {index} to get all the mapping, settings, and aliases in one step. You can use this response as a basis for creating your new index. (You'll need to delete some index settings like created_date, version.*, uuid).

  1. scroll all documents of existing index types
  1. index them without deleted field with bulk into new index

You can do this with the reindex API and a script, eg:

POST _reindex
{
  "source": {
    "index": "old_index"
  },
  "dest": {
    "index": "new_index"
  },
  "script": {
    "inline": """
    if (ctx._type == "bad_type") {
      ctx._source.remove("bad_field");
    }
"""
  }
}
  1. drop old index
  1. set alias to new index (i need the same name for index) - there is no way to rename index except to reindex everything again

This can be done in a single atomic step with the update-aliases API:

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "new_index",
        "alias": "old_index"
      }
    },
    {
      "remove_index": {
        "index": "old_index"
      }
    }
  ]
}
1 Like

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