首页 > 解决方案 > 如何将 json 作为 json 而不是 PUT 方法推送到弹性搜索

问题描述

如何从 python 代码将数据推送到弹性搜索中

现在我用PUT命令推送数据

PUT /data/test/1
{
    "id": "Accounting 101",
    "dataproduct": "E3",
    "professor": {
        "name": "Thomas Baszo",
        "department": "finance",
        "facutly_type": "part-time",
        "email": "baszot@onuni.com"
        },
    "students_enrolled": 27,
    "course_publish_date": "2015-01-19",
    "course_description": "Act 101 is a course from the business school on the introduction to accounting that teaches students how to read and compose basic financial statements"
}

PUT /data/test/2
{
    "name": "Accounting 101",
    "room": "E3",
    "professor": {
        "name": "Thomas Baszo",
        "department": "finance",
        "facutly_type": "part-time",
        "email": "baszot@onuni.com"
        },
    "students_enrolled": 27,
    "course_publish_date": "2015-01-19",
    "course_description": "Act 101 is a course from the business school on the introduction to accounting that teaches students how to read and compose basic financial statements"
}

如何用python推送

伪代码

from elasticsearch import Elasticsearch
es = Elasticsearch()
es.cluster.health()
es.indices.create(index='data', ignore=400)

标签: pythonelasticsearch

解决方案


您可以使用_bulk端点,也可以index一一使用您的文档:

es.index('data',
         body={
             "name": "Accounting 101",
             "room": "E3"
             # ...
         },
         id=2)

推荐阅读