From 70086c97272814c7422247bed94a9854d1850c06 Mon Sep 17 00:00:00 2001 From: missinglink Date: Fri, 7 Jul 2017 13:14:59 +0200 Subject: [PATCH 1/3] 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 }; }; From 682c81d41d7717ecf0c42f9d2d75fa07fa329a67 Mon Sep 17 00:00:00 2001 From: missinglink Date: Fri, 7 Jul 2017 13:25:40 +0200 Subject: [PATCH 2/3] feat(refactor): don't strip mandatory properties --- Document.js | 3 --- test/document/toESDocument.js | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Document.js b/Document.js index 1ae4c8e..0a97008 100644 --- a/Document.js +++ b/Document.js @@ -67,11 +67,8 @@ Document.prototype.toESDocument = function() { }; // 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 { diff --git a/test/document/toESDocument.js b/test/document/toESDocument.js index c05c4a6..6971f39 100644 --- a/test/document/toESDocument.js +++ b/test/document/toESDocument.js @@ -27,7 +27,10 @@ module.exports.tests.toESDocument = function(test) { _type: 'mylayer', _id: 'myid', data: { + center_point: {}, layer: 'mylayer', + name: {}, + phrase: {}, source: 'mysource', source_id: 'myid' } From 059e4533de1000b03f8fea36865da230af1c1078 Mon Sep 17 00:00:00 2001 From: missinglink Date: Fri, 7 Jul 2017 19:20:45 +0200 Subject: [PATCH 3/3] feat(Document): refactor to use vanilla js instead of lodash for empty checks --- Document.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Document.js b/Document.js index 0a97008..def3fd2 100644 --- a/Document.js +++ b/Document.js @@ -67,9 +67,15 @@ Document.prototype.toESDocument = function() { }; // remove empty properties - if( _.isEmpty( doc.parent ) ){ delete doc.parent; } - if( _.isEmpty( doc.address_parts ) ){ delete doc.address_parts; } - if( _.isEmpty( this.category ) ){ delete doc.category; } + if( !Object.keys( doc.parent || {} ).length ){ + delete doc.parent; + } + if( !Object.keys( doc.address_parts || {} ).length ){ + delete doc.address_parts; + } + if( !( this.category || [] ).length ){ + delete doc.category; + } return { _index: config.schema.indexName,