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


(Elham Roshan) #1

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


(David Pilato) #2

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


(Elham Roshan) #3

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?


(David Pilato) #4

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


(Elham Roshan) #5

Yes I can :slight_smile:

Thanks for the tip!

/Ellie


(system) #6

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