Python 3.9.5/Elasticsearch Python client 7.14
I'm using the Python Elasticsearch library to run queries against an Elasticsearch 7.10 cluster.
I'm trying to return fields from within a nested field, whereas I can only figure out the syntax to return the values from the entire nested field.
Example:
The ES document I'm returning:
"hits" : [
{
"_index" : "main",
"_type" : "_doc",
"_id" : "123",
"_score" : 1.0,
"_source" : {
"id" : 1000123,
"org_name" : {
"data" : "Acme Inc",
"entityType" : "ORGANIZATION"
}
}
}
]
Right now my Python code looks like this:
from elasticsearch import Elasticsearch
client = Elasticsearch()
response = client.search(
index="main",
body={
"the ES query"
}
)
for hit in response['hits']['hits']:
print(hit['_source']['id'], hit['_source']['org_name'])
which returns:
1000123 {'data': 'Acme Inc', 'entityType': 'ORGANIZATION'}
But what I need to return is:
Either:
1000123 'data': 'Acme Inc'
which I can't figure out the syntax for to modify ['org_name'] in the print line of the Python code.
Or even better, if possible:
1000123 'org_name': 'Acme Inc'
The single quotes around org_name are of no significance, so it doesn't matter if they are there or not in the response, whichever is achievable the most efficiently is fine with me.
I have reviewed all the examples in the docs for this ES library, and none of them seem to address this.
print(hit['_source']['id'], {'org_name': hit['_source']['org_name']['data']})??