您好,我正在使用python上传新功能以进行弹性搜索,属性为“关键字”,并且包含单词列表,如下所示:
keywords= ['txn', 'code', 'file', 'process']
我的功能如下:
def upload_features(string,elements):
headers = {
'Content-Type': 'application/json',
}
#here the list of keywords
x2 = ','.join('\\"{0}\\"'.format(w) for w in elements)
data = '{"script": "ctx._source.keywords = [' + str(x2) + ']"}'
instruction='http:myip/updates_job33/_doc/'
try:
response = requests.post(instruction+string+'/_update', headers=headers, data=data)
except:
print('error upload')
print('response finished')
print(response.json())
return response.json()
这很好,但是我不确定这是否是最干净的方法,尤其是我正在寻找减少这一行的方法:
x2 = ','.join('\\"{0}\\"'.format(w) for w in elements)
带有一个正常列表,然后对其进行格式化,以避免弹性搜索错误,因此,我想感谢您对此情况的反馈。