【问题标题】:Creating a DocType class with a Integer field with elasticsearch-dsl使用 elasticsearch-dsl 创建具有整数字段的 DocType 类
【发布时间】:2016-02-18 22:57:21
【问题描述】:

我想使用 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

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

谢谢。

编辑

根据本文档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

【问题讨论】:

    标签: python-2.7 elasticsearch elasticsearch-dsl


    【解决方案1】:

    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 行检查here

    【讨论】:

      猜你喜欢
      • 2016-11-26
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多