使用python中的弹性搜索dsl访问单个字段

时间:2017-03-28 23:43:21

标签: elasticsearch elasticsearch-dsl elasticsearch-dsl-py

以下是准确的还是应该是其他的?

我正在获得预期的结果,只是检查这是否是访问单个(嵌套)字段的最有效方式。

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q

import json
client = Elasticsearch('my_server')

policy_number = 'POLICY1234'
s = Search(using=client, index = "my_index").query("term",policyNumber=policy_number.lower())
es_response = s.execute()

for hits in es_response:
   print hits['policyNumber']
   print hits.party[0]['fullName']
   print hits.party[0].partyAddress[0]['address1']
   print hits.party[0].partyAddress[0]['city']
   print hits.party[0].phoneList[0]['phoneNumber']

1 个答案:

答案 0 :(得分:2)

您不需要手动拨打execute,也不必使用[]按名称访问字段,您只需使用属性访问权限:

for hit in s:
   print hit.policyNumber
   print hit.party[0].fullName
   print hit.party[0].partyAddress[0].address1
   print hit.party[0].partyAddress[0].city
   print hit.party[0].phoneList[0].phoneNumber