使用elasticsearch dsl创建一个带有Integer字段的DocType类



我想使用elasticsearch dsl 0.0.9库,但他们的例子已经过时了。我安装了最新版本,Integer和Boolean类型已经不存在了。

因此,他们的榜样不起作用。

from datetime import datetime
#There is no 'Integer' in elasticsearch_dsl
from elasticsearch_dsl import DocType, String, Date, Integer
from elasticsearch_dsl.connections import connections
# Define a default Elasticsearch client
connections.create_connection(hosts=['localhost'])
class Article(DocType):
    title = String(analyzer='snowball', fields={'raw': String(index='not_analyzed')})
    body = String(analyzer='snowball')
    tags = String(index='not_analyzed')
    published_from = Date()
    lines = Integer() ##############################   HERE

有人知道如何声明Integer字段吗?

谢谢。

编辑

根据本文件https://media.readthedocs.org/pdf/elasticsearch-dsl/latest/elasticsearch-dsl.pdf整数类型在0.0.9 中应该仍然可用

我不知道它为什么找不到它。

正如您从我的pip输出中看到的,我确实安装了0.0.9:Downloading elasticsearch_dsl-0.0.9-py2.py3-none-any.whl

Float、Double、Byte、Integer、Boolean、IP等字段类型是elasticsearch-dsl中的动态类。库本身创建了源代码中提到的这些类。在此处张贴文件中的示例代码以供快速参考。要获得完整的参考,您可以查看文件elasticsearch_dsl/field.py

# generate the query classes dynamicaly
for f in FIELDS:
    fclass = _make_dsl_class(Field, f)
    globals()[fclass.__name__] = fclass
    __all__.append(fclass.__name__)

FIELDS是一个包含所有这些字段类型的元组。因此,基本上为了回答您的问题,您的IDE将显示这些类不可用,但当您运行代码时,它们将自动创建。

从212号线检查。

最新更新