Skip to content

Commit

Permalink
fix: Merge pull request #69 from pelias/refactoring-fixes
Browse files Browse the repository at this point in the history
expose missing things from toESDocument
  • Loading branch information
orangejulius authored Jul 7, 2017
2 parents 8048884 + 2ee48b9 commit a09271d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
21 changes: 20 additions & 1 deletion Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ Document.prototype.toESDocument = function() {
category: this.category,
source: this.source,
layer: this.layer,
source_id: this.source_id
source_id: this.source_id,
bounding_box: this.bounding_box,
popularity: this.popularity,
population: this.population,
polygon: this.shape
};

// remove empty properties
Expand All @@ -76,6 +80,21 @@ Document.prototype.toESDocument = function() {
if( !( this.category || [] ).length ){
delete doc.category;
}
if (!this.bounding_box) {
delete doc.bounding_box;
}
if( !Object.keys( doc.center_point || {} ).length ){
delete doc.center_point;
}
if (!this.population) {
delete doc.population;
}
if (!this.popularity) {
delete doc.popularity;
}
if( !Object.keys( doc.polygon || {} ).length ){
delete doc.polygon;
}

return {
_index: config.schema.indexName,
Expand Down
63 changes: 59 additions & 4 deletions test/document/toESDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,86 @@ module.exports.tests.toESDocument = function(test) {
var Document = proxyquire('../../Document', { 'pelias-config': fakeConfig });

var doc = new Document('mysource','mylayer','myid');
doc.setName('myprop', 'myname');
doc.setAddress('name', 'address name');
doc.setAddress('number', 'address number');
doc.setAddress('street', 'address street');
doc.setAddress('zip', 'address zip');
doc.setBoundingBox({
upperLeft: {
lat: 13.131313,
lon: 21.212121
},
lowerRight: {
lat: 12.121212,
lon: 31.313131
}
});
doc.setPopulation(123);
doc.setPopularity(456);
doc.setPolygon({ key: 'value' });
doc.addCategory('category 1');
doc.addCategory('category 2');

var esDoc = doc.toESDocument();

var expected = {
_index: 'pelias',
_type: 'mylayer',
_id: 'myid',
data: {
center_point: {},
layer: 'mylayer',
name: {},
phrase: {},
name: {
myprop: 'myname'
},
phrase: {
myprop: 'myname'
},
address_parts: {
name: 'address name',
number: 'address number',
street: 'address street',
zip: 'address zip'
},
source: 'mysource',
source_id: 'myid'
source_id: 'myid',
bounding_box: '{"min_lat":12.121212,"max_lat":13.131313,"min_lon":21.212121,"max_lon":31.313131}',
population: 123,
popularity: 456,
polygon: {
key: 'value'
},
category: [
'category 1',
'category 2'
]
}
};

t.deepEqual(esDoc, expected, 'creates correct elasticsearch document');
t.end();

});

test('unset properties should not output in toESDocument', (t) => {
const Document = proxyquire('../../Document', { 'pelias-config': fakeConfig });

const esDoc = new Document('mysource','mylayer','myid').toESDocument();

// test that empty arrays/object are stripped from the doc before sending it
// downstream to elasticsearch.
t.false(esDoc.data.hasOwnProperty('address_parts'), 'does not include empty top-level maps');
t.false(esDoc.data.hasOwnProperty('category'), 'does not include empty top-level arrays');
t.false(esDoc.data.hasOwnProperty('parent'), 'does not include empty parent arrays');
t.false(esDoc.data.hasOwnProperty('bounding_box'), 'should not include bounding_box');
t.false(esDoc.data.hasOwnProperty('center_point'), 'should not include center');
t.false(esDoc.data.hasOwnProperty('population'), ' should not include population');
t.false(esDoc.data.hasOwnProperty('popularity'), ' should not include popularity');
t.false(esDoc.data.hasOwnProperty('polygon'), ' should not include polygon');
t.end();

});

};

module.exports.tests.toESDocumentWithCustomConfig = function(test) {
Expand Down

0 comments on commit a09271d

Please sign in to comment.