我正在使用批量方法在 elasticsearch 中索引数据,以最大限度地减少在 elasticsearch 中索引数据的时间。问题是在使用批量方法后,我的旧查询失败(意味着返回 0 次匹配),即使是简单的查询匹配查询也返回零匹配
elasticsearch 版本 6.3,语言 python, library- Python Elasticsearch 客户端
最初,我使用这段代码在 Elasticsearch 中为数据编制了索引。
temp_entities_list = []
for each_row in master_entities:
entity_data = {}
entity_data['entity_id'] = each_row.id
entity_data['createdat'] = each_row.createdat
entity_data['updatedat'] = each_row.updatedat
entity_data['individual_business_tag']=each_row.individual_business_tag
temp_entities_list.append(entity_data)
def indexing(entity_list):
for entity in entity_list:
index_name = "demo"
yield{
"_index":index_name,
"_type":"businesses",
"_source" :{
"body":entity
}
}
try:
helpers.bulk(es,testing(temp_entities_list))
except Exception as exe:
indexing_logger.exception("Error:"+str(exe))
这是我的旧查询,当我一次索引一个对象时它工作正常。
{
"query": {
"match" : {
"entity_name" : {
"query" : "Premium Market",
"operator" : "and"
}
}
}
}
根据文档 https://elasticsearch-py.readthedocs.io/en/master/helpers.html#example , 我试过这段代码
def indexing(entity_list):
for entity in entity_list:
index_name = "demo"
yield{
"_index":index_name,
"_type":"businesses",
"doc" :{entity
}
}
出现此错误:
Traceback (most recent call last):
File "sql-to-elasticsearch.py", line 90, in <module>
helpers.bulk(es,indexing(temp_entities_list),chunk_size=500,)
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\elasticsearch\helpers\__init__.py", line 257, in bulk
for ok, item in streaming_bulk(client, actions, *args, **kwargs):
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\elasticsearch\helpers\__init__.py", line 180, in streaming_bulk
client.transport.serializer):
File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\elasticsearch\helpers\__init__.py", line 58, in _chunk_actions
for action, data in actions:
File "sql-to-elasticsearch.py", line 81, in indexing
index_name = "demo"
TypeError: unhashable type: 'dict'
最佳答案
我相信这会导致错误:
"doc" :{entity}
因为您的 entity
似乎是一个字典,您正试图将它放入一个集合中,而在 Python 中,只有不可变对象(immutable对象)可以存储在集合中(字符串、整数、 float 、元组.. .) 因为它们是可哈希的。
请注意,此表示法用于集合 {}
。
如果你想将它放入容器中,我建议使用列表:
"doc" : [entity]
或者如果你只是指向 entity
with doc 使用:
"doc" : entity
希望这对您有所帮助。
关于python - 在 elasticsearch 中进行批量上传时获取 `TypeError: unhashable type: ' dict'`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57722270/