嵌套字段在elasticsearch-dsl-py中索引时给出TypeError

时间:2018-04-06 09:52:24

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

每当我尝试保存具有嵌套字段的对象时,我都会收到Type错误。

TypeError: isinstance() arg 2 must be a type or tuple of types

我有以下文档类:

class CreationIndex(InnerDoc):

    created_by = Keyword()
    created_date = Date()

class UpdationIndex(InnerDoc):

    updated_by = Keyword()
    updated_date = Date()

class IndustryIndex(DocType):
    name = Text(analyzer= 'english')
    creation = CreationIndex()
    updation = Nested(UpdationIndex())

    class Meta:
        index = 'industry'

def bulk_indexing():

    elastic_connection = Elasticsearch(hosts=['localhost'], timeout=20)
    if elastic_connection.indices.exists('industry'):
        elastic_connection.indices.delete('industry')
    if elastic_connection.indices.exists('category'):
        elastic_connection.indices.delete('category')

    IndustryIndex.init()
    print('HI')
    bulk(client=elastic_connection, actions=(b.indexing() for b in models.Industry.objects.all()))

我的行业模型是一个django模型(我使用mongoDB,因此它本身就是一个动态文档),定义为

class Industry(DynamicDocument,BaseCreationUpdation):
    name=StringField(required=True,unique=True)

    def save(self,*args,**kwargs):
        '''
        '''
        self=save_created_updated_info(self)
        super(Industry,self).save(args,kwargs)

    def indexing(self):
        objCreation = CreationIndex(
            created_by = self.creation.created_by,
            created_date = self.creation.created_date.date().isoformat()
            )
        print(objCreation.to_dict())
        obj = IndustryIndex(
            meta = {'id': str(self.id)},
            name = self.name,
            creation = objCreation.to_dict(),
            updation = [],
            )

        for objUp in self.updation:

            objUpdation = UpdationIndex(
                updated_by = objUp.updated_by,
                updated_date = objUp.updated_date.date().isoformat()
                )
            print(objUpdation)
            obj.updation.append(objUpdation)
            print()
            print(obj.to_dict())
            print()
        obj.save()

        return obj.to_dict(include_meta=True)

    @classmethod
    def index_document(cls,sender, document, **kwargs):
        document.indexing()

现在每当我运行bulk_indexing模块时,都会遇到问题:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "D:\projects\django\bhetincha\category\search.py", line 57, in bulk_indexing
    bulk(client=elastic_connection, actions=(b.indexing() for b in models.Industry.objects.all()))
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch\helpers\__init__.py", line 257, in bulk
    for ok, item in streaming_bulk(client, actions, **kwargs):
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch\helpers\__init__.py", line 180, in streaming_bulk
    client.transport.serializer):
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch\helpers\__init__.py", line 58, in _chunk_actions
    for action, data in actions:
  File "D:\projects\django\bhetincha\category\search.py", line 57, in <genexpr>
    bulk(client=elastic_connection, actions=(b.indexing() for b in models.Industry.objects.all()))
  File "D:\projects\django\bhetincha\category\models.py", line 80, in indexing
    obj.save()
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\document.py", line 405, in save
    self.full_clean()
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\utils.py", line 417, in full_clean
    self.clean_fields()
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\utils.py", line 403, in clean_fields
    data = field.clean(data)
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\field.py", line 179, in clean
    data = super(Object, self).clean(data)
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\field.py", line 90, in clean
    data = self.deserialize(data)
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\field.py", line 81, in deserialize
    for d in data
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\field.py", line 81, in <listcomp>
    for d in data
  File "C:\Users\prabi\Envs\bhetincha\lib\site-packages\elasticsearch_dsl\field.py", line 160, in _deserialize
    if isinstance(data, self._doc_class):
TypeError: isinstance() arg 2 must be a type or tuple of types

为什么会出现此错误?当我删除嵌套的更新字段时,它都有效,尽管创建字段是相同的。问题是什么?

1 个答案:

答案 0 :(得分:3)

初始化嵌套字段的方式是错误的 - 您需要传递一个类而不是类的实例作为构造函数的参数(即删除括号):

updation = Nested(UpdationIndex)  # Instead of Nested(UpdationIndex())