0

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

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

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']
4

1 回答 1

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
于 2017-03-29T17:47:13.630 回答