Skip to content

Commit

Permalink
feat(refactor): refactor toESDocument() to be more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
missinglink committed Jul 7, 2017
1 parent a5293ce commit 70086c9
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
};

Expand Down

0 comments on commit 70086c9

Please sign in to comment.