gpt4 book ai didi

python - elasticsearch python 批量 api (elasticsearch-py)

转载 作者:太空狗 更新时间:2023-10-30 02:45:01 24 4
gpt4 key购买 nike

我对 py-elasticsearch bulk 感到困惑@Diolor 解决方案有效 https://stackoverflow.com/questions/20288770/how-to-use-bulk-api-to-store-the-keywords-in-es-by-using-python , 但我想使用普通的 es.bulk()

我的代码:

from elasticsearch import Elasticsearch
es = Elasticsearch()
doc = '''\n {"host":"logsqa","path":"/logs","message":"test test","@timestamp":"2014-10-02T10:11:25.980256","tags":["multiline","mydate_0.005"]} \n'''
result = es.bulk(index="logstash-test", doc_type="test", body=doc)

错误是:

 No handlers could be found for logger "elasticsearch"
Traceback (most recent call last):
File "./log-parser-perf.py", line 55, in <module>
insertToES()
File "./log-parser-perf.py", line 46, in insertToES
res = es.bulk(index="logstash-test", doc_type="test", body=doc)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch-1.0.0-py2.7.egg/elasticsearch/client/utils.py", line 70, in _wrapped
return func(*args, params=params, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch-1.0.0-py2.7.egg/elasticsearch/client/__init__.py", line 570, in bulk
params=params, body=self._bulk_body(body))
File "/usr/local/lib/python2.7/dist-packages/elasticsearch-1.0.0-py2.7.egg/elasticsearch/transport.py", line 274, in perform_request
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch-1.0.0-py2.7.egg/elasticsearch/connection/http_urllib3.py", line 57, in perform_request
self._raise_error(response.status, raw_data)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch-1.0.0-py2.7.egg/elasticsearch/connection/base.py", line 83, in _raise_error
raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.TransportError: TransportError(500, u'ActionRequestValidationException[Validation Failed: 1: no requests added;]')

为 POST 调用生成的 url 是

/logstash-test/test/_bulk

POST 正文是:

{"host":"logsqa","path":"/logs","message":"test test","@timestamp":"2014-10-02T10:11:25.980256","tags":["multiline","mydate_0.005"]}

所以我用手做了 che curl:此 curl 不起作用:

> curl -XPUT http://localhost:9200/logstash-test/test2/_bulk -d
> '{"host":"logsqa","path":"/logs","message":"test
> test","@timestamp":"2014-10-02T10:11:25.980256","tags":["multiline","mydate_0.005"]}
> '
>
> {"error":"ActionRequestValidationException[Validation Failed: 1: no requests added;]","status":500}

所以错误是部分正确的,但我确实希望 elasticsearch.bulk() 能够正确管理输入参数。

pythonf函数是:

bulk(*args, **kwargs)
:arg body: The operation definition and data (action-data pairs), as
either a newline separated string, or a sequence of dicts to
serialize (one per row).
:arg index: Default index for items which don't provide one
:arg doc_type: Default document type for items which don't provide one
:arg consistency: Explicit write consistency setting for the operation
:arg refresh: Refresh the index after performing the operation
:arg routing: Specific routing value
:arg replication: Explicitly set the replication type (default: sync)
:arg timeout: Explicit operation timeout

最佳答案

如果有人目前正​​在尝试使用批量 api 并想知道格式应该是什么,那么以下是对我有用的:

doc = [
{
'index':{
'_index': index_name,
'_id' : <some_id>,
'_type':<doc_type>
}
},
{
'field_1': <value>,
'field_2': <value>
}
]

docs_as_string = json.dumps(doc[0]) + '\n' + json.dumps(doc[1]) + '\n'
client.bulk(body=docs_as_string)

关于python - elasticsearch python 批量 api (elasticsearch-py),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26159949/

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