diff --git a/lib/Connection.js b/lib/Connection.js index af470eb8c..a51c51aa9 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -32,6 +32,9 @@ class Connection { request (params, callback) { params.url = resolve(this.host.href, params.path) + if (params.querystring != null && params.querystring.length > 0) { + params.url += '?' + params.querystring + } params.agent = this._agent debug('Starting a new request', params) return makeRequest(params, callback) diff --git a/lib/Serializer.js b/lib/Serializer.js index 52125ea29..97516a2c0 100644 --- a/lib/Serializer.js +++ b/lib/Serializer.js @@ -1,5 +1,6 @@ 'use strict' +const { stringify } = require('querystring') const debug = require('debug')('elasticsearch') const { SerializationError, DeserializationError } = require('./errors') @@ -35,6 +36,19 @@ class Serializer { } return ndjson } + + qserialize (object) { + debug('qserialize', object) + // arrays should be serialized as comma separated list + const keys = Object.keys(object) + for (var i = 0, len = keys.length; i < len; i++) { + var key = keys[i] + if (Array.isArray(object[key]) === true) { + object[key] = object[key].join(',') + } + } + return stringify(object) + } } module.exports = Serializer diff --git a/lib/Transport.js b/lib/Transport.js index 6885d143c..420e65767 100644 --- a/lib/Transport.js +++ b/lib/Transport.js @@ -42,28 +42,37 @@ class Transport { // handle json body if (params.body != null) { - try { - params.body = this.serializer.serialize(params.body) - } catch (err) { - return callback(err) + if (typeof params.body !== 'string') { + try { + params.body = this.serializer.serialize(params.body) + } catch (err) { + return callback(err) + } } params.headers = params.headers || {} params.headers['Content-Type'] = 'application/json' params.headers['Content-Length'] = '' + Buffer.byteLength(params.body) // handle ndjson body } else if (params.bulkBody != null) { - try { - params.body = this.serializer.ndserialize(params.bulkBody) - } catch (err) { - return callback(err) + if (typeof params.bulkBody !== 'string') { + try { + params.body = this.serializer.ndserialize(params.bulkBody) + } catch (err) { + return callback(err) + } + } else { + params.body = params.bulkBody } params.headers = params.headers || {} params.headers['Content-Type'] = 'application/x-ndjson' params.headers['Content-Length'] = '' + Buffer.byteLength(params.body) } + // serializes the querystring + params.querystring = this.serializer.qserialize(params.querystring) params.timeout = params.timeout || this.requestTimeout this.emit('request', params) + const request = connection.request(params, (err, response) => { if (err != null) { this.connectionPool.markDead(connection)