How to add a new field with a default value to all docs in an index?

Hi,

I need to add a new field with null default value to all documents in an index.
I know the following query, but how to add a default value to it?

PUT customer/_mapping/_doc
{
"properties": {
"major": {
"type": "keyword"
}
}
}

Best regards,
Ellie

I believe it's useless as providing a null value does not add any value...
Why not just updating the schema as you did?

Because later I want to do an update and change those values that are NULL to something else like:

POST customer/_doc/_update_by_query
{
"script": {
"inline": "ctx._source.major = 'student'"
},
"query":
{"bool":
{"must": {"match_all": {}},
"filter": {"term": {"major": "NULL"}}
}
},
"size": 1
}
}

and this query does not work.

Thought maybe that's because the value of the new field is not NULL? you have any other ideas?

Can't you use a bool -> must_not -> exist query?

Yes I can :slight_smile:

Thanks for the tip!

/Ellie

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