【问题标题】:Using OpenSearch Python bulk api to insert data to multiple indices使用 OpenSearch Python bulk api 将数据插入多个索引
【发布时间】:2022-08-15 22:59:30
【问题描述】:

本文档展示了如何在 curl 中使用 POST 请求插入具有多个索引的批量数据:https://opensearch.org/docs/latest/opensearch/index-data/

如果我有这种格式的数据,

[
{ \"index\": { \"_index\": \"index-2022-06-08\", \"_id\": \"<id>\" } }
{ \"A JSON\": \"document\" }
{ \"index\": { \"_index\": \"index-2022-06-09\", \"_id\": \"<id>\" } }
{ \"A JSON\": \"document\" }
{ \"index\": { \"_index\": \"index-2022-06-10\", \"_id\": \"<id>\" } }
{ \"A JSON\": \"document\" }
]

批量请求应采用来自\"_index\": \"index-2022-06-08\" 的索引名称

我试图使用 OpenSearch-py 库来做同样的事情,但我找不到任何示例 sn-p 这样做。我正在使用这种格式从 AWS Lambda 发送请求。

client = OpenSearch(
            hosts = [{\'host\': host, \'port\': 443}],
            http_auth = awsauth,
            use_ssl = True,
            verify_certs = True,
            connection_class = RequestsHttpConnection
            )
        
        resp = helpers.bulk(client, logs, index= index_name, max_retries = 3)

在这里,我不得不提到 index_name 作为批量请求中的参数,因此它不会从数据本身中获取 index_name。如果我没有在参数中提到 index_name,我会得到错误 4xx index_name missing。

我也在研究批量 api 源代码:https://github.com/opensearch-project/opensearch-py/blob/main/opensearchpy/helpers/actions.py#L373

它看起来不像 index_name 是一个强制参数。

谁能帮我解决我想念的问题?

  • @Divyank 链接已死

标签: api rest elasticsearch opensearch


【解决方案1】:

我遇到了同样的问题,并在elasticsearch.py bulk-helpers documentation 中找到了解决方案。当搜索端点返回的_source-structure 中提供文档时,它可以工作。

调用批量方法:

            resp = helpers.bulk(
                self.opensearch,
                actions,
                max_retries=3,
            )

其中操作是这样的字典列表:

[{
    '_op_type': 'update',
    '_index': 'index-name',
    '_id': 42,
    '_source': {
        "title": "Hello World!",
        "body": "..."
    }
}]

_op_type 可用作附加字段来定义应为文档调用的操作(indexupdatedelete、...)。

希望这可以帮助遇到相同问题的任何人!

【讨论】:

    猜你喜欢
    • 2015-10-29
    • 2022-12-17
    • 1970-01-01
    • 2021-05-11
    • 2018-02-26
    • 2018-11-29
    • 2018-07-03
    • 2021-08-08
    • 2022-01-19
    相关资源
    最近更新 更多