如何在python的elasticsearch中索引大型json响应?使用Elasticsearch DSL

2024-10-06 11:53:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我在文档中发现了这种方法: https://elasticsearch-dsl.readthedocs.io/en/latest/index.html

class Article(Document):
    title = Text(analyzer='snowball', fields={'raw': Keyword()})
    body = Text(analyzer='snowball')
    tags = Keyword()
    published_from = Date()
    lines = Integer()

像索引一样

# create and save and article
article = Article(meta={'id': 42}, title='Hello world!', tags=['test'])
article.body = ''' looong text '''
article.published_from = datetime.now()
article.save()

但是这种方法不适合我,因为我有非常大的JSON,包含超过100个字段,而且我有多个JSON,就像那样。以这种形式转换JSON是很困难的:

article = Article(meta={'id': 42}, title='Hello world!', tags=['test'])

有什么想法吗,如何直接索引json而不使用类似模型的包装器?


Tags: and方法textfromjsontitlesavearticle
1条回答
网友
1楼 · 发布于 2024-10-06 11:53:12

您应该能够使用json的dict结构,而无需将所有内容转换为DSL。只需使用普通的elasticsearch客户机,例如,请参见下面的低级客户机示例:https://github.com/elastic/elasticsearch-py

请注意,如果您有大量的文档,使用批量导入API是非常有用的,它可以更快。你知道吗

相关问题 更多 >