From da4f0010474bd4cb552eb6a5921587d2100401b2 Mon Sep 17 00:00:00 2001 From: missinglink Date: Tue, 19 Apr 2022 14:34:32 +0200 Subject: [PATCH] feat(explode-fields): Explode name fields such that no field contains more than a single value --- Document.js | 1 + post/explode_fields.js | 26 ++++++++++++++++++++++++++ test/document/post.js | 10 +++++++++- test/document/toESDocument.js | 8 ++++++-- 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 post/explode_fields.js diff --git a/Document.js b/Document.js index 46bee41..1a060e4 100644 --- a/Document.js +++ b/Document.js @@ -45,6 +45,7 @@ function Document( source, layer, source_id ){ this.addPostProcessingScript( require('./post/deduplication') ); this.addPostProcessingScript( require('./post/language_field_trimming') ); this.addPostProcessingScript( require('./post/popularity') ); + this.addPostProcessingScript( require('./post/explode_fields') ); // mandatory properties this.setSource( source ); diff --git a/post/explode_fields.js b/post/explode_fields.js new file mode 100644 index 0000000..80ec4ae --- /dev/null +++ b/post/explode_fields.js @@ -0,0 +1,26 @@ +/** + * Explode name fields such that no field contains more than a + * single value. + */ + +const _ = require('lodash'); +const prefix = 'name'; + +function explode(doc) { + let field = doc[prefix]; + if (!_.isPlainObject(field)) { return; } + + _.each(field, (values, subfield) => { + if (_.isArray(values) && _.size(values) > 1) { + _.each(values, (value, i) => { + if (i === 0) { + doc[prefix][subfield] = value; + } else { + doc[prefix][`${subfield}_${i}`] = value; + } + }); + } + }); +} + +module.exports = explode; diff --git a/test/document/post.js b/test/document/post.js index 67c53e7..a3c57c8 100644 --- a/test/document/post.js +++ b/test/document/post.js @@ -5,7 +5,15 @@ const seperable_street_names = require('../../post/seperable_street_names').post const deduplication = require('../../post/deduplication'); const language_field_trimming = require('../../post/language_field_trimming'); const popularity = require('../../post/popularity'); -const DEFAULT_SCRIPTS = [intersections, seperable_street_names, deduplication, language_field_trimming, popularity]; +const explode_fields = require('../../post/explode_fields'); +const DEFAULT_SCRIPTS = [ + intersections, + seperable_street_names, + deduplication, + language_field_trimming, + popularity, + explode_fields +]; module.exports.tests = {}; diff --git a/test/document/toESDocument.js b/test/document/toESDocument.js index 567a410..e54cb1b 100644 --- a/test/document/toESDocument.js +++ b/test/document/toESDocument.js @@ -109,10 +109,14 @@ module.exports.tests.toESDocument = function(test) { layer: 'mylayer', source_id: 'myid', name: { - myprop: [ 'myname', 'myname2', 'myname3' ] + myprop: 'myname', + myprop_1: 'myname2', + myprop_2: 'myname3' }, phrase: { - myprop: [ 'myname', 'myname2', 'myname3' ] + myprop: 'myname', + myprop_1: 'myname2', + myprop_2: 'myname3' } } };