Skip to content

Commit 8db483a

Browse files
committed
[fixes #2] fix the case when _source is missing and store all meta-fields
1 parent 2611318 commit 8db483a

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

pandasticsearch/dataframe.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class DataFrame(object):
3535
def __init__(self, **kwargs):
3636
self._client = kwargs.get('client', None)
3737
self._mapping = kwargs.get('mapping', None)
38-
3938
self._index = list(self._mapping.keys())[0] if self._mapping else None
4039
self._doc_type = DataFrame._get_doc_type(self._mapping) if self._mapping else None
4140
self._columns = sorted(DataFrame._get_cols(self._mapping)) if self._mapping else None
@@ -281,7 +280,8 @@ def _execute(self):
281280

282281
res_dict = self._client.post(data=self._build_query())
283282
if self._aggregation is None and self._groupby is None:
284-
query = Select.from_dict(res_dict)
283+
query = Select()
284+
query.explain_result(res_dict)
285285
else:
286286
query = Agg.from_dict(res_dict)
287287
return query
@@ -319,6 +319,7 @@ def count(self):
319319
[2, 1]
320320
"""
321321
df = DataFrame(client=self._client,
322+
include_meta_fields=self._include_meta_fields,
322323
mapping=self._mapping,
323324
filter=self._filter,
324325
groupby=self._groupby,
@@ -348,14 +349,18 @@ def show(self, n=10000, truncate=15):
348349
assert n > 0
349350

350351
if self._aggregation:
351-
raise TypeError('show() is not allowed for aggregation. use collect() instead')
352+
raise DataFrameException('show() is not allowed for aggregation. use collect() instead')
352353

353354
query = self._execute()
354355

355356
if self._projection:
356357
cols = [col.field_name() for col in self._projection]
357358
else:
358359
cols = self.columns
360+
361+
if cols is None:
362+
raise _unbound_index_err
363+
359364
sys.stdout.write(query.result_as_tabular(cols, n, truncate))
360365
sys.stdout.write('time: {0}ms\n'.format(query.millis_taken))
361366

pandasticsearch/queries.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,16 @@ def __init__(self):
7676

7777
def explain_result(self, result=None):
7878
super(Select, self).explain_result(result)
79-
self._values = [hit['_source'] for hit in self._result_dict['hits']['hits']]
79+
rows = []
80+
for hit in self._result_dict['hits']['hits']:
81+
row = {}
82+
for k in hit.keys():
83+
if k == '_source':
84+
row.update(hit['_source'])
85+
elif k.startswith('_'):
86+
row[k] = hit[k]
87+
rows.append(row)
88+
self._values = rows
8089

8190
def to_pandas(self):
8291
try:

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name='pandasticsearch',
12-
version='0.2.0',
12+
version='0.2.1',
1313
author='onesuper',
1414
author_email='[email protected]',
1515
packages=['pandasticsearch'],

0 commit comments

Comments
 (0)