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

时间:2019-07-01 18:24:43

标签: python django elasticsearch elasticsearch-dsl elasticsearch-py

因此,我们在Django项目中使用elasticsearch,并且在使用elasticsearch-dsl python库。

我们在生产中遇到以下错误:

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}]}')

使用更好的格式:

{
    "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
        }
    ]
}

产生错误的代码是对此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()

这是EventDocument的定义:

from elasticsearch_dsl import (
    Document,
    Integer,
)


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

我们目前最大的问题是我们无权访问服务器,我们通过为错误配置的自动电子邮件收到了错误。所以我什至不知道如何复制它。

希望您能提供帮助,谢谢。

1 个答案:

答案 0 :(得分:2)

由于文档中的版本冲突,导致发生此错误。来自ES官方文档

  

Elasticsearch已分发。当文档被创建,更新或   删除后,该文档的新版本必须复制到其他版本   集群中的节点。 Elasticsearch也是异步的,   并发,意味着这些复制请求是在发送中   并行,并且可能不按顺序到达其目的地。   Elasticsearch需要一种方法来确保旧版本的   文档永远不会覆盖较新的版本。

this官方文档中了解有关如何在ES中处理version conflict http 409异常的更多信息,该文档还解释了为什么会得到异常以及处理异常的各种方式,并详细说明了该概念。