简体   繁体   English

使用elasticsearch-dsl的delete方法时版本冲突

[英]Version conflict when using the delete method of elasticsearch-dsl

So, we're using elasticsearch in our Django project, and we're using the elasticsearch-dsl python library.因此,我们在 Django 项目中使用了 elasticsearch,并且使用了 elasticsearch-dsl python 库。

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:产生错误的代码是对 dsl delete方法的调用:

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 :这是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.希望能帮到你,谢谢。

This error is happening due to the version conflict in your documents.由于文档中的版本冲突,发生此错误。 From ES official docs来自 ES 官方文档

Elasticsearch is distributed. Elasticsearch 是分布式的。 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 也是异步并发的,这意味着这些复制请求是并行发送的,并且可能会乱序到达目的地。 Elasticsearch needs a way of ensuring that an older version of a document never overwrites a newer version. Elasticsearch 需要一种方法来确保文档的旧版本永远不会覆盖新版本。

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.官方文档中阅读有关如何处理 ES 中version conflict http 409异常的更多信息,其中还解释了为什么会出现异常以及如何处理它的各种方法,并详细解释了概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM