question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Upsert in `update`

See original GitHub issue

Would it make sense to tweak DocType.update() to perform an upsert if the item doesn’t already exist (at https://github.com/elastic/elasticsearch-dsl-py/blob/master/elasticsearch_dsl/document.py#L227)?

    meta = es.update(
        index=self._get_index(index),
        doc_type=self._doc_type.name,
        body={'doc': fields, 'doc_as_upsert': True, 'detect_noop': True},
        **doc_meta
    )

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
dboczekcommented, Feb 13, 2017

Example where upsert is convenient.

Lets assume I have simple document.

def Document(DocType): text = Text() read_counter = Integer()

Now consider that there is two separate programs that use this Document. One is indexing Documents without touching counter and other updates only counter. The later can assume that document always exist so I can use document.update(read_counter=reads). I can’t do the same for first because what I need is to index document if it does not exist or update if it exists unfortunately document.save() will overwrite whole document thus read_counter is lost in process.

I know I could get document first to check if it exists but the whole point of the update API in ES is to avoid that.

Note, this operation still means full reindex of the document, it just removes some network roundtrips and reduces chances of version conflicts between the get and the index. Although it says full reindex it reads current document merges it with received one and then saves so it does not loose data that was there.

Otherwise why implementing update at all? I can always get instance, update some fields and save whole document.

0reactions
xkungfucommented, May 4, 2021

so, how on earth update a doc like upsert? did you find a solution at last? is there a document tell us how to implement?

thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Upsert in MongoDB - GeeksforGeeks
In MongoDB, upsert is an option that is used for update operation e.g. update(), findAndModify(), etc. Or in other words, upsert is a ......
Read more >
Guide to Upsert in MongoDB - Baeldung
Upsert is a combination of insert and update (inSERT + UPdate = upsert). We can use the upsert with different update methods, i.e.,...
Read more >
Upsert in SQL: What is an upsert, and when should you use ...
The UPSERT command in CockroachDB performs an upsert based on the uniqueness of the primary key column or columns, and it will perform...
Read more >
What's the difference between INSERT, UPDATE, UPSERT ...
The UPSERT option is the combination of 'Update' and 'Insert' which means that it will check for the records that are inserted or...
Read more >
db.collection.update() — MongoDB Manual
Updates a single document that matches the query . If both upsert and multi are true and no documents match the query, the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found