如何使用Django REST框架读写elasticsearch?

2024-03-29 09:27:42 发布

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

我尝试使用Django REST框架读/写elasticsearch。在

经过调查,我发现下面的方法使用了Elasticsearch DSL。在

elasticsearch-with-django-the-easy-way

但是,此方法与elasticsearch同时写入数据库。 我怎么能只用elasticsearch写这个?在

对不起。我的英语不是很强。在


Tags: thedjango方法框架rest数据库witheasy
1条回答
网友
1楼 · 发布于 2024-03-29 09:27:42

如果您想找到REST Djangoelasticsearch连接的框架,可以选中这个:http://django-rest-elasticsearch.readthedocs.io/en/latest/index.html。如果你想为你找到的指南创建NoSQL解决方案,你应该创建一个没有模型的Django应用程序,并使用json.tsv/.csv文件为你的elasticsearch数据建立索引。它将类似于:

class ElementIndex(DocType):
    ROWNAME = Text()
    ROWNAME = Text()

    class Meta:
        index = 'index_name'

def indexing(self):
    obj = ElementIndex(
        ROWNAME=str(self['NAME']),
        ROWNAME=str(self['NAME'])
    )
    obj.save(index="index_name")
    return obj.to_dict(include_meta=True)

def bulk_indexing(args):

    # ElementIndex.init(index="index_name")
    ElementIndex.init()
    es = Elasticsearch()

    //here your result dict with data from source

    r = bulk(client=es, actions=(indexing(c) for c in result))
    es.indices.refresh()

正如我说的,您可以使用上面链接中的REST和{}。然后创建您的Django模板或使用ReactJS或其他方法创建前端。在

相关问题 更多 >