forked from pelias/sorting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
279 lines (235 loc) · 11 KB
/
index.js
File metadata and controls
279 lines (235 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const _ = require('lodash');
const geolib = require('geolib');
const populations = {
mega: [4000000, Infinity],
large: [500000, 4000000],
medium: [5000, 500000],
small: [0, 5000]
};
const popularities = {
very_popular: [10000, Infinity],
popular: [1000, 10000],
nonpopular: [0, 1000]
};
function isLayer(layer, result) {
return result.layer === layer;
}
function isLayerAndValueInRange(isLayer, field, range, result) {
return isLayer(result) && _.inRange(_.defaultTo(result[field], 0), range[0], range[1]);
}
const isContinent = isLayer.bind(null, 'continent');
const isCountry = isLayer.bind(null, 'country');
const isDependency = isLayer.bind(null, 'dependency');
const isMacroRegion = isLayer.bind(null, 'macroregion');
const isRegion = isLayer.bind(null, 'region');
const isMacroCounty = isLayer.bind(null, 'macrocounty');
const isCounty = isLayer.bind(null, 'county');
const isLocality = isLayer.bind(null, 'locality');
const isLocaladmin = isLayer.bind(null, 'localadmin');
const isBorough = isLayer.bind(null, 'borough');
const isMacroHood = isLayer.bind(null, 'macrohood');
const isNeighbourhood = isLayer.bind(null, 'neighbourhood');
const isVenue = isLayer.bind(null, 'venue');
const isMegaLocality = isLayerAndValueInRange.bind(null, isLocality, 'population', populations.mega);
const isLargeLocality = isLayerAndValueInRange.bind(null, isLocality, 'population', populations.large);
const isMediumLocality = isLayerAndValueInRange.bind(null, isLocality, 'population', populations.medium);
const isSmallLocality = isLayerAndValueInRange.bind(null, isLocality, 'population', populations.small);
const isMegaLocaladmin = isLayerAndValueInRange.bind(null, isLocaladmin, 'population', populations.mega);
const isLargeLocaladmin = isLayerAndValueInRange.bind(null, isLocaladmin, 'population', populations.large);
const isMediumLocaladmin = isLayerAndValueInRange.bind(null, isLocaladmin, 'population', populations.medium);
const isSmallLocaladmin = isLayerAndValueInRange.bind(null, isLocaladmin, 'population', populations.small);
const isVeryPopularNeighbourhood = isLayerAndValueInRange.bind(null, isNeighbourhood, 'popularity', popularities.very_popular);
const isPopularNeighbourhood = isLayerAndValueInRange.bind(null, isNeighbourhood, 'popularity', popularities.popular);
const isNonPopularNeighbourhood = isLayerAndValueInRange.bind(null, isNeighbourhood, 'popularity', popularities.nonpopular);
function normalizeText(value) {
if (_.isNil(value)) {
return '';
}
return String(value).toLowerCase().trim();
}
function getFirstParentValue(result, key) {
const value = _.get(result, `parent.${key}`);
if (_.isArray(value)) {
return value[0];
}
return value;
}
function mismatchPenalty(clean, result) {
const parsedText = _.get(clean, 'parsed_text', {});
if (_.isNil(parsedText) || _.isNil(result)) {
return 0;
}
let penalty = 0;
const layer = result.layer;
if (layer === 'address' || layer === 'street') {
const queryPostalcode = normalizeText(parsedText.postalcode);
const resultPostalcode = normalizeText(_.get(result, 'address_parts.zip'));
if (queryPostalcode && resultPostalcode && queryPostalcode !== resultPostalcode) {
// If PLZ regions differ (first 2 digits), result is in a completely different
// part of the country — apply a very heavy penalty to push it below local matches.
// Same-region mismatches (e.g. 88212 vs 88214 in Ravensburg) get a mild penalty.
if (queryPostalcode.length >= 2 && resultPostalcode.length >= 2 &&
queryPostalcode.substring(0, 2) !== resultPostalcode.substring(0, 2)) {
penalty += 10000;
} else {
penalty += 35;
}
}
const queryCity = normalizeText(parsedText.city || parsedText.locality);
const resultLocality = normalizeText(getFirstParentValue(result, 'locality'));
const resultLocaladmin = normalizeText(getFirstParentValue(result, 'localadmin'));
if (queryCity && resultLocality && queryCity !== resultLocality && queryCity !== resultLocaladmin) {
penalty += 25;
}
}
// Venue layer: penalize locality mismatch (venues typically lack postalcode data)
if (layer === 'venue') {
const queryCity = normalizeText(parsedText.city || parsedText.locality);
const resultLocality = normalizeText(getFirstParentValue(result, 'locality'));
const resultLocaladmin = normalizeText(getFirstParentValue(result, 'localadmin'));
if (queryCity && resultLocality && queryCity !== resultLocality && queryCity !== resultLocaladmin) {
penalty += 10000;
}
}
return penalty;
}
// Check if a result's PLZ matches the query PLZ
function plzMatches(clean, result) {
const parsedText = _.get(clean, 'parsed_text', {});
const queryPlz = normalizeText(_.get(parsedText, 'postalcode'));
if (!queryPlz) {
return null;
}
const resultPlz = normalizeText(_.get(result, 'address_parts.zip'));
if (!resultPlz) {
return null;
}
return queryPlz === resultPlz;
}
// Check if the result city matches the query city
function cityMatches(clean, result) {
const parsedText = _.get(clean, 'parsed_text', {});
const queryCity = normalizeText(parsedText.city || parsedText.locality);
if (!queryCity) {
return null;
}
const resultLocality = normalizeText(getFirstParentValue(result, 'locality'));
const resultLocaladmin = normalizeText(getFirstParentValue(result, 'localadmin'));
if (!resultLocality) {
return null;
}
return queryCity === resultLocality || queryCity === resultLocaladmin;
}
function adjustedScore(clean, result) {
const baseScore = _.defaultTo(result._score, 0);
return baseScore - mismatchPenalty(clean, result);
}
function resolveByPopulation(isLayer, result1, result2) {
if ([result1, result2].every(isLayer)) {
return _.defaultTo(result2.population, 0) - _.defaultTo(result1.population, 0);
}
return isLayer(result1) ? -1 : 1;
}
function resolveByFocusPointThenOtherField(isLayer, field, result1, result2, focus_point) {
if ([result1, result2].every(isLayer)) {
if (focus_point) {
const lat_lon_1 = parse_lat_lon(result1.center_point, 'lat', 'lon');
const lat_lon_2 = parse_lat_lon(result2.center_point, 'lat', 'lon');
if (lat_lon_1 && lat_lon_2) {
const distance_1 = geolib.getDistance(focus_point, lat_lon_1);
const distance_2 = geolib.getDistance(focus_point, lat_lon_2);
return distance_1 - distance_2;
}
return lat_lon_1 ? -1 : 1;
}
return _.defaultTo(result2[field], 0) - _.defaultTo(result1[field], 0);
}
return isLayer(result1) ? -1 : 1;
}
function resolveByScore(clean, isLayer, result1, result2) {
if ([result1, result2].every(isLayer)) {
return adjustedScore(clean, result2) - adjustedScore(clean, result1);
}
return isLayer(result1) ? -1 : 1;
}
const resolveMegaLocality = resolveByPopulation.bind(null, isMegaLocality);
const resolveMegaLocaladmin = resolveByPopulation.bind(null, isMegaLocaladmin);
const resolveContinent = resolveByPopulation.bind(null, isContinent);
const resolveCountry = resolveByPopulation.bind(null, isCountry);
const resolveDependency = resolveByPopulation.bind(null, isDependency);
const resolveMacroRegion = resolveByPopulation.bind(null, isMacroRegion);
const resolveRegion = resolveByPopulation.bind(null, isRegion);
const resolveMacroCounty = resolveByPopulation.bind(null, isMacroCounty);
const resolveCounty = resolveByPopulation.bind(null, isCounty);
const resolveBorough = resolveByPopulation.bind(null, isBorough);
const resolveMacroHood = resolveByPopulation.bind(null, isMacroHood);
const resolveLargeLocality = resolveByFocusPointThenOtherField.bind(null, isLargeLocality, 'population');
const resolveMediumLocality = resolveByFocusPointThenOtherField.bind(null, isMediumLocality, 'population');
const resolveSmallLocality = resolveByFocusPointThenOtherField.bind(null, isSmallLocality, 'population');
const resolveLargeLocaladmin = resolveByFocusPointThenOtherField.bind(null, isLargeLocaladmin, 'population');
const resolveMediumLocaladmin = resolveByFocusPointThenOtherField.bind(null, isMediumLocaladmin, 'population');
const resolveSmallLocaladmin = resolveByFocusPointThenOtherField.bind(null, isSmallLocaladmin, 'population');
const resolveVeryPopularNeighbourhood = resolveByFocusPointThenOtherField.bind(null, isVeryPopularNeighbourhood, 'popularity');
const resolvePopularNeighbourhood = resolveByFocusPointThenOtherField.bind(null, isPopularNeighbourhood, 'popularity');
const resolveNonPopularNeighbourhood = resolveByFocusPointThenOtherField.bind(null, isNonPopularNeighbourhood, 'popularity');
function parse_lat_lon(obj, lat_field, lon_field) {
if ([lat_field, lon_field].every(_.has.bind(null, obj))) {
return { latitude: obj[lat_field], longitude: obj[lon_field] };
}
}
module.exports = (clean) => {
const focus_point = parse_lat_lon(clean, 'focus.point.lat', 'focus.point.lon');
const resolveVenue = resolveByScore.bind(null, clean, isVenue);
const fallbacks = [
{ either: isMegaLocality, resolve: resolveMegaLocality },
{ either: isMegaLocaladmin, resolve: resolveMegaLocaladmin },
{ either: isContinent, resolve: resolveContinent },
{ either: isCountry, resolve: resolveCountry },
{ either: isDependency, resolve: resolveDependency },
{ either: isLargeLocality, resolve: resolveLargeLocality },
{ either: isLargeLocaladmin, resolve: resolveLargeLocaladmin },
{ either: isMacroRegion, resolve: resolveMacroRegion },
{ either: isRegion, resolve: resolveRegion },
{ either: isBorough, resolve: resolveBorough },
{ either: isVeryPopularNeighbourhood, resolve: resolveVeryPopularNeighbourhood },
{ either: isMediumLocality, resolve: resolveMediumLocality },
{ either: isMediumLocaladmin, resolve: resolveMediumLocaladmin },
{ either: isMacroCounty, resolve: resolveMacroCounty },
{ either: isCounty, resolve: resolveCounty },
{ either: isMacroHood, resolve: resolveMacroHood },
{ either: isVenue, resolve: resolveVenue },
{ either: isPopularNeighbourhood, resolve: resolvePopularNeighbourhood },
{ either: isSmallLocality, resolve: resolveSmallLocality },
{ either: isSmallLocaladmin, resolve: resolveSmallLocaladmin },
{ either: isNonPopularNeighbourhood, resolve: resolveNonPopularNeighbourhood },
{
either: _.constant(true),
resolve: (result1, result2) => {
const score1 = adjustedScore(clean, result1);
const score2 = adjustedScore(clean, result2);
if (isNaN(score1) || isNaN(score2)) {
return -1;
}
if (score1 === score2) {
// Tiebreaker: prefer city match when scores are equal
const city1 = cityMatches(clean, result1);
const city2 = cityMatches(clean, result2);
if (city1 === true && city2 === false) {
return -1;
}
if (city2 === true && city1 === false) {
return 1;
}
return -1;
}
return score1 > score2 ? -1 : 1;
}
}
];
return (result1, result2) => {
const layer = fallbacks.find((layer) => {
return [result1, result2].some(layer.either);
});
return layer.resolve(result1, result2, focus_point);
};
};