从elasticsearch结果中提取键并存储在其他列表中

时间:2016-10-13 04:53:18

标签: python dictionary

我有一个存储在dict中的ES返回列表

res = es.search(index="instances-i*",body=doc)

res返回的结果如下:

{
"took": 34,
"timed_out": false,
"_shards": {
"total": 1760,
"successful": 1760,
"failed": 0
},
"hits": {
"total": 551,
"max_score": 0.30685282,
"hits": [
{
"_index": "instances-i-0034438e-2016.10.10-08:23:51",
"_type": "type_name",
"_id": "1",
"_score": 0.30685282,
"_source": {
"ansible.isv_alias": "ruiba",
"ansible": {
"isv_alias": "ruiba",
"WEB_URL": "sin01-cloud.trial.sentinelcloud.com/ruiba"
}
}
}
,
{
"_index": "instances-i-0034438e-2016.10.11-08:23:54",
"_type": "type_name",
"_id": "1",
"_score": 0.30685282,
"_source": {
"ansible.isv_alias": "aike3",
"ansible": {
"isv_alias": "aike3",
"WEB_URL": "sin01-cloud.trial.cloud.com/aike3"
}
}
}
,
{
"_index": "instances-i-883sf38e-2016.10.12-08:23:45",
"_type": "type_name",
"_id": "1",
"_score": 0.30685282,
"_source": {
"ansible.isv_alias": "beijing",
"ansible": {
"isv_alias": "beijing",
"WEB_URL": "sin01-cloud.trial.cloud.com/beijing"
}
}
}
.
.
.
.
so on

我需要从返回的dict中提取WEB_URL并将其存储在LIST中。

我很好奇,如果我们确实可以确保没有重复输入正在填充的LIST中的WEB_URL的值{这是第二部分虽然}

1 个答案:

答案 0 :(得分:3)

要存储WEB_URL的值,您需要迭代字典 -

data = response_from_es['hits']
list_of_urls = []
for item in data['hits']:
    list_of_urls.append(item['_source']['ansible']['WEB_URL'])

print list_of_urls

并制作list_of_urls unique - > set(list_of_urls)