Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

explode name fields #149

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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