简体   繁体   中英

How to send request with bulk? Elasticsearch

I have an object with data, and i want to send this data to elasticsearch container

      for(let key in params)
               {
                  bulk.push(JSON.stringify({
                index: {
                    _id: params[ key ][ 'id' ],
                    _type: 'id',
                    _index: 'geo'
                }
            }));
            bulk.push(JSON.stringify(params[key]));
        }
        let bulks = bulk.join("\n") + "\n";

I made request

let cat =  request(
    {
        'method' : 'PUT',
        'uri'    : 'http://dev4.int10h.net:40024/_bulk',
        'body'   : bulks ,
        'json'   : true,
        'headers':
            [
                'Content-Type: application/x-ndjson'
            ],
        'agent'  : false
    }
);

but has error

Unhandled rejection StatusCodeError: 400 - {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\\n]"}],"type":"illegal_argument_exception", "reason":"The bulk request must be terminated by a newline [\\n]"},"status":400 } at new StatusCodeError (/usr/lib/node_modules/request-promise/node_modules/request-promise-core/lib/errors.js:32:15) at Request.plumbing.callback (/usr/lib/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:104:33) at Request.RP$callback [as _callback] (/usr/lib/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:46:31) at Request.self.callback (/usr/lib/node_modules/request/request.js:185:22) at Request.emit (events.js:182:13) at Request. (/usr/lib/node_modules/request/request.js:1161:10) at Request.emit (events.js:182:13) at IncomingMessage. (/usr/lib/node_modules/request/request.js:1083:12) at Object.onceWrapper (events.js:273:13) at IncomingMessage.emit (events.js:187:15) at endReadableNT (_stream_readable.js:1098:12) at process.internalTickCallback (internal/process/next_tick.js:72:19)

How to properly send bulk?

bulks type string 在此处输入图片说明

Not sure if this helps you but here I found similar problem with _bulk and JSON.stringify .

The answer is:

It looks like the meta characters in your payload does not get translated into newlines. If you instead used the elasticsearch.js client, it would handle this for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM