I have query result from elasticsearch in following format:
[
{
"_index": "product",
"_type": "_doc",
"_id": "23234sdf",
"_score": 2.2295187,
"_source": {
"SERP_KEY": "",
"r_variant_info": "",
"s_asin": "",
"pid": "394",
"r_gtin": "00838128000547",
"additional_attributes_remarks": "publisher:0|size:0",
"s_gtin": "",
"r_category": "",
"confidence_score": "2.4545",
"title_match": "45.45"
}
},
{
"_index": "product",
"_type": "_doc",
"_id": "23234sdf",
"_score": 2.2295187,
"_source": {
"SERP_KEY": "",
"r_variant_info": "",
"s_asin": "",
"pid": "394",
"r_gtin": "00838128000547",
"additional_attributes_remarks": "publisher:0|size:0",
"s_gtin": "",
"r_category": "",
"confidence_score": "2.4545",
"title_match": "45.45"
}
},
]
I am trying to load _source field along with _id also into dataframe.
I tried this:
def fetch_records_from_elasticsearch_index(index, filter_json):
search_param = prepare_es_body(filter_json_dict=filter_json)
response = settings.ES.search(index=index, body=search_param, size=10)
if len(response['hits']['hits']) > 0:
import pandas as pd
all_hits = response['hits']['hits']
# return all_hits
# export es hits to pandas dataframe
df = pd.concat(map(pd.DataFrame.from_dict, all_hits), axis=1)['_source'].T
return df
else:
return 0
df contains _source field only, but I also want to add _id field to it.
Here's the df output format:
{
"AdminEdit": [
"False",
"False",
"False",
"False",
],
"Group": [
"Grp2",
"Grp2",
"Grp2",
"Grp2"
],
}
How can I add _id to it?
from pandas.io import json; print (json.json_normalize(response))?