如何将数据发布数据发送到作业API ElasticSearch

时间:2019-05-20 19:23:14

标签: python elasticsearch

我正在尝试使用elasticsearch将数据发布到机器学习api。 json文档需要使用哪种格式?

我试图用txt文件中的换行符分隔的json文档发送数据。我也尝试使用转储和加载来回转换为json无济于事。该文档指出,文档可以用空格分隔,但是无论我尝试什么,它都不会接受。

https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-post-data.html

这是一个另存为file_name.json的json文档的示例:

[{"myid": "id1", "client": "client1", "submit_date": 1514764857},
 {"my_id": "id2", "client": "client_2", "submit_date": 1514764857}]

这是发布数据所需的基本代码:

from elasticsearch import Elasticsearch
from elasticsearch.client.xpack import MlClient

es = elastic_connection()
es_ml = MlClient(es)

def post_training_data(directory='Training Data', file_name='file_name.json'):
        with open(os.path.join(directory, file_name), mode='r') as train_file:
            train_data = json.load(train_file)
            es_ml.post_data(job_id=job_id, body=train_data)

post_training_data()

这是我遇到的具体错误:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "..\train_model.py", line 218, in post_training_data
    self.es_ml.post_data(job_id=self.job_id, body=train_data)
  File "..\inc_anamoly\lib\site-packages\elasticsearch\client\utils.py", line 76, in _wrapped
    return func(*args, params=params, **kwargs)
  File "..\inc_anamoly\lib\site-packages\elasticsearch\client\xpack\ml.py", line 81, in post_data
    body=self._bulk_body(body))
AttributeError: 'MlClient' object has no attribute '_bulk_body'

1 个答案:

答案 0 :(得分:0)

事实证明这是一个错误。已报告问题。

https://github.com/elastic/elasticsearch-py/issues/959