Skip to content

Commit

Permalink
Merge pull request #68 from pelias/refactor
Browse files Browse the repository at this point in the history
feat(refactor): refactor toESDocument() to be more efficient
  • Loading branch information
missinglink authored Jul 7, 2017
2 parents fd3f9e0 + 059e453 commit 8048884
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
30 changes: 24 additions & 6 deletions Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,34 @@ 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( !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,
_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
3 changes: 3 additions & 0 deletions test/document/toESDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down

0 comments on commit 8048884

Please sign in to comment.