Skip to content

Commit 1fdf8e3

Browse files
committed
fix(document-generator): fix for duplicate documents sharing the same ID
a very old PR introduced functionality to generate version of the same document for each hierarchy present. this results in the document being overridden in elasticsearch with the latter. reverts #87
1 parent 54f19a5 commit 1fdf8e3

File tree

3 files changed

+16
-37
lines changed

3 files changed

+16
-37
lines changed

src/hierarchyFinder.js

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
module.exports = {};
2-
3-
var hasName = function(r) {
4-
return r.name;
5-
};
6-
7-
var isDefined = function(r) {
8-
return r;
9-
};
10-
111
/*
122
This function finds all the WOF records associated with a hierarchy
133
@@ -26,24 +16,20 @@ var isDefined = function(r) {
2616
]
2717
2818
lastly, filter out any hierarchy elements that are undefined or w/o a name
29-
3019
*/
31-
function resolveHierarchy(wofRecords, hierarchy) {
32-
return Object.keys(hierarchy).map(function(key) {
33-
return wofRecords[hierarchy[key]];
34-
}).filter(isDefined).filter(hasName);
35-
}
3620

3721
/*
3822
This function returns all the resolved hierarchies for a wofRecord. Each
3923
wofRecord can have multiple hierarchies, so resolve them by looking up the
40-
referenced wofRecord in the big collection of wofRecords.
24+
referenced wofRecord in the big collection of parentRecords.
4125
*/
42-
module.exports = function(wofRecords) {
43-
return function(wofRecord) {
44-
return wofRecord.hierarchies.reduce(function(resolvedHierarchies, hierarchy) {
45-
resolvedHierarchies.push(resolveHierarchy(wofRecords, hierarchy));
46-
return resolvedHierarchies;
47-
}, []);
26+
module.exports = (parentRecords) => {
27+
return (wofRecord) => {
28+
return wofRecord.hierarchies.map(hierarchy => {
29+
return Object.values(hierarchy)
30+
.map(parentId => parentRecords[parentId])
31+
.filter(Boolean)
32+
.filter(r => r.name);
33+
});
4834
};
4935
};

src/peliasDocGenerators.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,15 @@ function setupDocument(record, hierarchy) {
151151

152152
}
153153

154-
module.exports.create = function(hierarchy_finder) {
155-
return through2.obj(function(record, enc, next) {
154+
module.exports.create = (hierarchy_finder) => {
155+
return through2.obj(function(record, _enc, next) {
156156
// if there are no hierarchies, then just return the doc as-is
157-
var hierarchies = hierarchy_finder(record);
157+
const hierarchies = hierarchy_finder(record);
158158

159159
try {
160-
if (hierarchies && hierarchies.length > 0) {
161-
hierarchies.forEach(function(hierarchy) {
162-
this.push(setupDocument(record, hierarchy));
163-
}, this);
164-
160+
// only use the first hierarchy when multiple hierachies exist
161+
if (Array.isArray(hierarchies) && hierarchies.length > 0) {
162+
this.push(setupDocument(record, hierarchies[0]));
165163
} else {
166164
this.push(setupDocument(record));
167165
}

test/peliasDocGeneratorsTest.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ tape('create', function(test) {
579579

580580
});
581581

582-
test.test('a document should be created for each available hierarchy', function(t) {
582+
test.test('a single document should be created when multiple hierarchies exist', function(t) {
583583
var wofRecords = {
584584
1: {
585585
id: 1,
@@ -619,11 +619,6 @@ tape('create', function(test) {
619619
.setCentroid({ lat: 12.121212, lon: 21.212121 })
620620
.addParent( 'neighbourhood', 'neighbourhood name', '1')
621621
.addParent( 'country', 'country name 1', '2'),
622-
new Document( 'whosonfirst', 'neighbourhood', '1')
623-
.setName('default', 'neighbourhood name')
624-
.setCentroid({ lat: 12.121212, lon: 21.212121 })
625-
.addParent( 'neighbourhood', 'neighbourhood name', '1')
626-
.addParent( 'country', 'country name 2', '3')
627622
];
628623

629624
var hierarchies_finder = function() {

0 commit comments

Comments
 (0)