如果为布尔类型提供 false 值,则无法使用 elasticsearch dsl 创建文档

Not able to create document with elasticsearch dsl if give boolean type with false value

我有如下代码

from elasticsearch_dsl.connections import connections
from elasticsearch_dsl import DocType, String, Boolean

class BaseDoc(DocType):
    id = String(required=True, index='not_analyzed')
    name = String(required=True)
    deleted = Boolean(required=True, null_value=True)

    class Meta:
        index = 'test'


class MyDoc(BaseDoc):
    pass

connections.create_connection(hosts=['localhost:9200'])

MyDoc.init()

doc = MyDoc(id='test', name='test', deleted=False)

doc.save(refresh=True)

我收到错误

Traceback (most recent call last):
  File "/tmp/test.py", line 31, in <module>
    doc.save(refresh=True)
  File "/usr/lib/python2.7/site-packages/elasticsearch_dsl/document.py", line 240, in save
    self.full_clean()
  File "/usr/lib/python2.7/site-packages/elasticsearch_dsl/utils.py", line 453, in full_clean
    self.clean_fields()
  File "/usr/lib/python2.7/site-packages/elasticsearch_dsl/utils.py", line 447, in clean_fields
    raise ValidationException(errors)
elasticsearch_dsl.exceptions.ValidationException: {'deleted': [ValidationException('Value required for this field.',)]}

如果你看到了,我在尝试创建 doc 对象时在代码中传递了 delete=False

什么时候不取deleted值?

如果我删除 deleted

doc = MyDoc(id='test', name='test')

doc.save(refresh=True)

然后也报同样的错误,elasticsearch dsl似乎没有处理删除。

您遇到的问题was fixed in PR #390

看来您仍在使用 0.0.11 版。如果您使用的是 ES 2.x,您应该升级到版本 2.2.0,如果您使用的是 ES 5.x,那么您应该升级到版本 5.1.0,您的问题应该已解决。