diff --git a/Document.js b/Document.js index def3fd2..bb30c84 100644 --- a/Document.js +++ b/Document.js @@ -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 @@ -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, diff --git a/test/document/toESDocument.js b/test/document/toESDocument.js index 6971f39..ce36609 100644 --- a/test/document/toESDocument.js +++ b/test/document/toESDocument.js @@ -20,6 +20,27 @@ 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 = { @@ -27,24 +48,58 @@ module.exports.tests.toESDocument = function(test) { _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) {