What is the best practise to add a new field to an index in elasticsearch?

My problem is that I want to add a new field in my index but the value of the field is in the database, is it possible to update the data in my index by doing _update_by_query? if it is possible, how to do this to connect to the database (postgres) ?
or should I write a script to update each data and call the rest api each time?

There's no direct way (yet) to do such a thing. So this is something you need to manage by yourself.

You might be able to do this using Logstash using some jdbc filter.

If you need to update all the documents of your index, I'd recommend reindexing instead of doing updates.

Unless you can set the value to update from the current document (like, extracting the day of the week from a date field), update by query won't help in that case.

And how to do the reindexing with the addition of the new field instead of updates ?

The same way you indexed first your documents.

I have done it few time.

here is how I did using python
read every record from index in loop using elasticsearch_dsl, don't forget to get _id
create new recordset with same _id of original
write them back to same index and it will overwrite (delete/create) same record.

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