elasticsearch-DSL lib 在保存文档时为文本字段设置默认值

elasticsearch-DSL lib set default value for Text field on saving Document

我们如何检查 elasticsearch-DSL 库中的 Field 对象(例如文本、整数、关键字)是否不包含任何值,比如 None 或空字符串?在这种情况下,在将文档保存到 ES 之前,我需要使用某种默认但动态的值设置文档对象中的字段。谢谢。

最简单的方法是覆盖 save() 方法并在那里执行,您可以在此处查看示例:https://github.com/elastic/elasticsearch-dsl-py/blob/master/examples/parent_child.py#L84-L85

我认为当前的实施不可能。详细信息可用 here。但是,如果您使用的是 elasticsearch rest 客户端,则可以添加逻辑以在保存之前生成一些随机 String/Number。