python Elasticsearch batch: Malformed action/metadata

python Elasticsearch batch: Malformed action/metadata … here is a solution to the problem.

python Elasticsearch batch: Malformed action/metadata

I wrote a piece of code to inject IRC eggdrop logs into the elasticsearch 6.4 database bulk using python elasticsearch. It was written in python, using version 3.7, and tested on NetBSD, Linux, and Mac OS X.
Some log files were imported, but some failed with the following error:

elasticsearch.exceptions.RequestError: TransportError(400, 
'illegal_argument_exception', 'Malformed action/metadata line [387], 
expected START_OBJECT or END_OBJECT but found [VALUE_STRING]')

I’ve read a lot of elastic.co forum posts and SO answers where OP has formatting errors on its dataset, missing fields, etc., but I can’t find them in mine. Also, not all log files are rejected because of this error.

I’m looking at the encoding error, but everything in that area seems to be a file.

  • Here is an example file that produces this error. It is 100% valid JSON.
  • Here is python code.

Idea?

Solution

In your JSON response, there are line breaks in JSON. One of the values of _source is empty. Python cannot treat them as dictionaries. When forming a bulk request, clean up the complete data and click the bulk API.

JSON at index 192 takes null as the _source.

The JSON at index 47 has a newline character.

Clean up this data when exporting to Elasticsearch.

Hope this helps you.

Related Problems and Solutions