Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explanation field if meta is enabled #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/EXAMPLES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Very long queries can be read from file

meta-fields
-----------
Selecting meta-fields: _id, _index, _score, _type
Selecting meta-fields: _id, _index, _score, _type, _explanation. If the selected meta-field is not available than we use `NaN` as a default.

.. code-block:: bash

Expand Down
5 changes: 3 additions & 2 deletions es2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
CONNECTION_TIMEOUT = 120
TIMES_TO_TRY = 3
RETRY_DELAY = 60
META_FIELDS = [u'_id', u'_index', u'_score', u'_type']
META_FIELDS = [u'_id', u'_index', u'_score', u'_type', u'_explanation']
NaN = "NaN"


# Retry decorator for functions with exceptions
Expand Down Expand Up @@ -198,7 +199,7 @@ def is_dict(arg):

with codecs.open(self.tmp_file, mode='a', encoding='utf-8') as tmp_file:
for hit in hit_list:
out = {field: hit[field] for field in META_FIELDS} if self.opts.meta_fields else {}
out = {field: hit.get(field, NaN) for field in META_FIELDS} if self.opts.meta_fields else {}
if '_source' in hit and len(hit['_source']) > 0:
to_keyvalue_pairs(hit['_source'])
tmp_file.write('{}\n'.format(json.dumps(out)))
Expand Down