def es_read(self, keys, index, doc_type):
"""
Read from an ElasticSearch index and return a DataFrame
:param keys: a list of keys to extract in elasticsearch
:param index: the ElasticSearch index to read
:param doc_type: the ElasticSearch doc_type to read
"""
self.successful_ = 0
self.failed_ = 0
# Collect records for all of the keys
records = []
for key in keys:
try:
record = self.client.get(index=index, doc_type=doc_type, id=key)
self.successful_ += 1
if '_source' in record:
records.append(record['_source'])
except NotFoundError as nfe:
print('Key not found: %s' % nfe)
self.failed_ += 1
# Prepare the records into a single DataFrame
df = None
if records:
df = pd.DataFrame(records).fillna(value=np.nan)
df = df.reindex_axis(sorted(df.columns), axis=1)
return df
评论列表
文章目录