pteich / elastic-query-export

🚚 Export Data from ElasticSearch to CSV/JSON using a Lucene Query (e.g. from Kibana) or a raw JSON Query string
MIT License
89 stars 18 forks source link

Re-initialize document inside hit loop to empty it and not retain old data #21

Closed qikiqi closed 1 year ago

qikiqi commented 1 year ago

Because the document is initialized currently in L53 and in L59 json.Unmarshal is performed and as per Go docs;

To unmarshal a JSON object into a map, Unmarshal first establishes a map to use. If the map is nil, Unmarshal allocates a new map. Otherwise Unmarshal reuses the existing map, keeping existing entries. Source

So old data is retained, and this leads to the CSV export being incorrect under certain situations, f.ex. if new document doesn't override all the fields.

To fix, re-initialize document inside the loop rather than outside.