我可以使用以下代码将数据从 Pandas 数据框导入elasticsearch。我只需要添加带有自动生成的序列号的id列。但是我可以使用messageid列作为ID吗?
# message id looks like nucb-9a7ff0885b95efae
df["id"] = [x for x in range(len(df["messageid"])) ]
# the above statement works but the following does not
#df["id"] = df["messageid"]
tmp = df.to_json(orient = "records")
df_json= json.loads(tmp)
import elasticsearch
es = elasticsearch.Elasticsearch('https://some_site.com')
for id in df_json:
es.index(index='fromdf', doc_type='mydf', body=id)
elasticsearch中的id不必为数字。但是在使用python时,出现错误
RequestError: TransportError(400, u'MapperParsingException[failed to parse [id]]; nested: NumberFormatException[For input string: "nucb-a006fd8dd60ac7a6"]; ')
如何确保可以对非数字ID使用批量方法?
换句话说,该代码应与
df["id"] = df["messageid"]
最佳答案
索引方法签名:
def index(self, index, doc_type, body, id=None, params=None):
...
:arg index: The name of the index
:arg doc_type: The type of the document
:arg body: The document
:arg id: Document ID
...
因此,您的数据应转到正文,标识您数据的标识符应转到id。如果要存储由mesageid标识的消息,可以这样做:
for row_dict in df_json:
es.index(index='fromdf', doc_type='mydf', body=row_dict, id=row_dict['messageid'])
您还可以通过使用已经定义的函数(例如pandas.DataFrame.to_dict)来极大地简化代码,从而不必为了获取字典而转换为json和加载json。
关于python - elasticsearch批量方法失败,并带有字母数字ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34127339/