Skip to content

Commit

Permalink
feat(explode-fields): Explode name fields such that no field contains…
Browse files Browse the repository at this point in the history
… more than a single value
  • Loading branch information
missinglink committed Apr 19, 2022
1 parent d4cfa21 commit d2cb81d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
26 changes: 26 additions & 0 deletions post/explode_fields.js
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 9 additions & 1 deletion test/document/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand Down
8 changes: 6 additions & 2 deletions test/document/toESDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
};
Expand Down

0 comments on commit d2cb81d

Please sign in to comment.