Skip to content

Commit 6478430

Browse files
Removed column sorting, wildcard support in fields naming
1 parent cd12059 commit 6478430

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

es2csv.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def next_scroll(scroll_id):
102102

103103
if self.opts.doc_types:
104104
search_args['doc_type'] = self.opts.doc_types
105-
105+
106106
if self.opts.query.startswith('@'):
107107
query_file = self.opts.query[1:]
108108
if os.path.exists(query_file):
@@ -124,7 +124,8 @@ def next_scroll(scroll_id):
124124
search_args['q'] = query
125125

126126
if '_all' not in self.opts.fields:
127-
search_args['fields'] = ','.join(self.opts.fields)
127+
search_args['_source_include'] = ','.join(self.opts.fields)
128+
self.csv_headers.extend([field for field in self.opts.fields if '*' not in field])
128129

129130
if self.opts.debug_mode:
130131
print('Using these indices: %s' % ', '.join(self.opts.index_prefixes))
@@ -138,7 +139,7 @@ def next_scroll(scroll_id):
138139

139140
print('Found %s results' % self.num_results)
140141
if self.opts.debug_mode:
141-
print(res)
142+
print(json.dumps(res))
142143

143144
if self.num_results > 0:
144145
open(self.opts.output_file, 'w').close()
@@ -209,19 +210,15 @@ def is_dict(arg):
209210
with open(self.tmp_file, 'a') as tmp_file:
210211
for hit in hit_list:
211212
out = {field: hit[field] for field in META_FIELDS} if self.opts.meta_fields else {}
212-
if '_source' in hit:
213+
if '_source' in hit and len(hit['_source']) > 0:
213214
to_keyvalue_pairs(hit['_source'])
214215
tmp_file.write('%s\n' % json.dumps(out))
215-
elif 'fields' in hit:
216-
to_keyvalue_pairs(hit['fields'])
217-
tmp_file.write('%s\n' % json.dumps(out))
218216
tmp_file.close()
219217

220218
def write_to_csv(self):
221219
if self.num_results > 0:
222220
self.num_results = sum(1 for line in open(self.tmp_file, 'r'))
223221
if self.num_results > 0:
224-
self.csv_headers.sort()
225222
output_file = open(self.opts.output_file, 'a')
226223
csv_writer = csv.DictWriter(output_file, fieldnames=self.csv_headers, delimiter=self.opts.delimiter)
227224
csv_writer.writeheader()

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[bdist_wheel]
2+
python-tag = py27

0 commit comments

Comments
 (0)