Lea*_*wly 2 python json elasticsearch
我有一个.json.gz文件希望加载到弹性搜索中。
我的第一次尝试涉及使用json模块将JSON转换为字典列表。
import gzip
import json
from pprint import pprint
from elasticsearch import Elasticsearch
nodes_f = gzip.open("nodes.json.gz")
nodes = json.load(nodes_f)
Run Code Online (Sandbox Code Playgroud)
字典示例:
pprint(nodes[0])
{u'index': 1,
u'point': [508163.122, 195316.627],
u'tax': u'fehwj39099'}
Run Code Online (Sandbox Code Playgroud)
使用Elasticsearch:
es = Elasticsearch()
data = es.bulk(index="index",body=nodes)
Run Code Online (Sandbox Code Playgroud)
但是,这返回:
elasticsearch.exceptions.RequestError: TransportError(400, u'illegal_argument_exception', u'Malformed action/metadata line [1], expected START_OBJECT or END_OBJECT but found [VALUE_STRING]')
Run Code Online (Sandbox Code Playgroud)
除此之外,我希望能够找到tax给定point的查询,在这种情况下,对我应该如何索引与elasticsearch数据的影响。
Alfe向我指出了正确的方向,但是我无法使他的代码正常工作。
我发现了两种解决方案:
用for循环逐行:
es = elasticsearch.Elasticsearch()
for node in nodes:
_id = node['index']
es.index(index='nodes',doc_type='external',id=_id,body=node)
Run Code Online (Sandbox Code Playgroud)
批量使用helper:
actions = [
{
"_index" : "nodes_bulk",
"_type" : "external",
"_id" : str(node['index']),
"_source" : node
}
for node in nodes
]
helpers.bulk(es,actions)
Run Code Online (Sandbox Code Playgroud)
散22列表的速度快了大约几倍343724。
| 归档时间: |
|
| 查看次数: |
3467 次 |
| 最近记录: |