gpt4 book ai didi

django - 在 elasticsearch-dsl-py 中进行索引时,嵌套字段会出现 TypeError

转载 作者:行者123 更新时间:2023-12-03 00:39:45 29 4
gpt4 key购买 nike

每当我尝试保存具有嵌套字段的对象时,我都会收到类型错误。
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

为什么会出现这个错误?当我删除嵌套的更新文件时,它一切正常,尽管创建字段是相同的。这里有什么问题?

最佳答案

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

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

关于django - 在 elasticsearch-dsl-py 中进行索引时,嵌套字段会出现 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49690045/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com