From 70086c97272814c7422247bed94a9854d1850c06 Mon Sep 17 00:00:00 2001 From: missinglink Date: Fri, 7 Jul 2017 13:14:59 +0200 Subject: [PATCH] feat(refactor): refactor toESDocument() to be more efficient --- Document.js | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/Document.js b/Document.js index 002a241..1ae4c8e 100644 --- a/Document.js +++ b/Document.js @@ -54,16 +54,31 @@ Document.prototype.toJSON = function(){ * Returns an object in exactly the format that Elasticsearch wants for inserts */ Document.prototype.toESDocument = function() { + var doc = { + name: this.name, + phrase: this.phrase, + parent: this.parent, + address_parts: this.address_parts, + center_point: this.center_point, + category: this.category, + source: this.source, + layer: this.layer, + source_id: this.source_id + }; + + // remove empty properties + if( _.isEmpty( doc.name ) ){ delete doc.name; } + if( _.isEmpty( doc.phrase ) ){ delete doc.phrase; } + if( _.isEmpty( doc.parent ) ){ delete doc.parent; } + if( _.isEmpty( doc.address_parts ) ){ delete doc.address_parts; } + if( _.isEmpty( this.center_point ) ){ delete doc.center_point; } + if( _.isEmpty( this.category ) ){ delete doc.category; } + return { _index: config.schema.indexName, _type: this.getType(), _id: this.getId(), - data: JSON.parse( JSON.stringify( this, function( k, v ){ - if((_.isArray(v) || _.isPlainObject(v)) && _.isEmpty(v) ){ - return undefined; - } - return v; - })) + data: doc }; };