Version conflict when using the delete method of elasticsearch-dsl
Asked Answered
J

2

7

So, we're using elasticsearch in our Django project, and we're using the elasticsearch-dsl python library.

We got the following error in production:

ConflictError(409, '{"took":7,"timed_out":false,"total":1,"deleted":0,"batches":1,"version_conflicts":1,"noops":0,"retries":{"bulk":0,"search":0},"throttled_millis":0,"requests_per_second":-1.0,"throttled_until_millis":0,"failures":[{"index":"events","type":"_doc","id":"KJ7SpWsBZnen1jNBRWWM","cause":{"type":"version_conflict_engine_exception","reason":"[KJ7SpWsBZnen1jNBRWWM]: version conflict, required seqNo [1418], primary term [1]. current document has seqNo [1419] and primary term [1]","index_uuid":"2-fSZILVQzuJE8KVmpLFXQ","shard":"0","index":"events"},"status":409}]}')

And with better formatting:

{
    "took": 7,
    "timed_out": false,
    "total": 1,
    "deleted": 0,
    "batches": 1,
    "version_conflicts": 1,
    "noops": 0,
    "retries": {
        "bulk": 0,
        "search": 0
    },
    "throttled_millis": 0,
    "requests_per_second": -1.0,
    "throttled_until_millis": 0,
    "failures": [
        {
            "index": "events",
            "type": "_doc",
            "id": "KJ7SpWsBZnen1jNBRWWM",
            "cause": {
                "type": "version_conflict_engine_exception",
                "reason": "[KJ7SpWsBZnen1jNBRWWM]: version conflict, required seqNo [1418], primary term [1]. current document has seqNo [1419] and primary term [1]",
                "index_uuid": "2-fSZILVQzuJE8KVmpLFXQ",
                "shard": "0",
                "index": "events"
            },
            "status": 409
        }
    ]
}

The code that produced the error was this call to the dsl delete method:

connections.create_connection(
    hosts=[settings.ELASTICSEARCH_HOST],
    timeout=20,
)
search = EventDocument.search()
# The query is made by the django model's id
search.query('match', id=self.id).delete()

And here's the definition of EventDocument:

from elasticsearch_dsl import (
    Document,
    Integer,
)


class EventDocument(Document):
    id = Integer()
    # other fields

Our biggest issue right now is that we don't have access to the server, we got the error through an automated email we configured for errors. So I don't even know how to reproduce it.

Hope you can help, thanks.

Jitney answered 1/7, 2019 at 18:24 Comment(1)
Your answer wasn't useful, this was a question about a specific library that works with ES and you just copy pasted stuff from the official docs that I had already read at the time, you were just trying to get an easy accepted answer. I don't remember how I solved this, but it wasn't thanks to that answer.Jitney
T
5

This error is happening due to the version conflict in your documents. From ES official docs

Elasticsearch is distributed. When documents are created, updated, or deleted, the new version of the document has to be replicated to other nodes in the cluster. Elasticsearch is also asynchronous and concurrent, meaning that these replication requests are sent in parallel, and may arrive at their destination out of sequence. Elasticsearch needs a way of ensuring that an older version of a document never overwrites a newer version.

Read more about how to handle the version conflict http 409 exception in ES in this official doc, which also explains why you get the exception and various ways on how to handle it and explains the concept in details.

Timbuktu answered 2/7, 2019 at 0:59 Comment(0)
M
0
search.query('match', id=self.id).params(conflicts="proceed").delete()

Should do the trick. Docs: elasticsearch-dsl, elasticsearch

Mullinax answered 23/2 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.