Ernesto
Ernesto

Reputation: 944

Adding default value to existing mapping in elastic search

I have an index with mapping. I decided to add a new field to existing mapping:

{

  "properties": {
    "sexifield": {
      "type": "keyword",
      "null_value": "NULL"
    }
  }

}

As far as I understand, the field should appear in existing documents when I reindex. So when I use api to reindex:

{
  "source": {
    "index": "index_v1"
  },
  "dest": {
    "index": "index_v2",
    "version_type": "external"
  }
}

I see that the mapping for index_v2 does not consist sexifield, and documents are not consisting it neither. Also this operation took less than 60ms.

Please point me, what I do not understand from it...

Adding the new documents to the first index (via java API, for an entity which has not this field (sexifield), so probably elastic should add me the default one) with sexifield, also does not create me this additional field.

Thanks in advance for tips.

Regards

Upvotes: 6

Views: 6415

Answers (1)

JBone
JBone

Reputation: 1794

great question +1 ( I learned something while solving your problem)

I don't know the answer to how to consider the second mapping (reindexed mapping) while reindexing, but here is how I would update the reindexed index (all the documents) once the reindexing is done from original index. I still continue to research to see if there is a way to consider the default values that are defined in the mapping of the second index while reindexing, but for now see if this solution helps..

POST /index_v2/_update_by_query
{
  "script": {
    "lang": "painless",
    "inline": "ctx._source.sexifield = params.null_value",
    "params": {
      "null_value": "NULL"
    }
  }
}

Upvotes: 3

Related Questions