forked from pelias/schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.js
313 lines (299 loc) · 10.1 KB
/
settings.js
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
'use strict';
var fs = require('fs');
var path = require('path');
var merge = require('lodash.merge');
var peliasConfig = require('pelias-config');
var punctuation = require('./punctuation');
var synonymFile = require('./synonyms/parser');
// load synonyms from disk
var synonyms = fs.readdirSync(path.join(__dirname, 'synonyms'))
.sort()
.filter( f => f.match(/\.txt$/) )
.reduce(( acc, cur ) => {
acc[cur.replace('.txt','')] = synonymFile(
path.join(__dirname, 'synonyms', cur)
);
return acc;
}, {});
require('./configValidation').validate(peliasConfig.generate());
function generate(){
var config = peliasConfig.generate();
// Default settings
var settings = {
"analysis": {
"tokenizer": {
"peliasNameTokenizer": {
"type": "pattern",
"pattern": "[\\s,/\\\\]+"
},
"peliasStreetTokenizer": {
"type": "pattern",
"pattern": "[,/\\\\]+"
}
},
"analyzer": {
"peliasAdmin": {
"type": "custom",
"tokenizer": "peliasNameTokenizer",
"char_filter" : ["punctuation", "nfkc_normalizer"],
"filter": [
"lowercase",
"icu_folding",
"trim",
"word_delimiter",
"custom_admin",
"notnull"
]
},
"peliasIndexOneEdgeGram" : {
"type": "custom",
"tokenizer" : "peliasNameTokenizer",
"char_filter" : ["punctuation", "nfkc_normalizer"],
"filter": [
"lowercase",
"icu_folding",
"trim",
"custom_name",
"full_token_address_suffix_expansion",
"ampersand",
"remove_ordinals",
"removeAllZeroNumericPrefix",
"surround_single_characters_with_word_markers",
"house_number_word_delimiter",
"remove_single_characters",
"surround_house_numbers_with_word_markers",
"peliasOneEdgeGramFilter",
"eliminate_tokens_starting_with_word_marker",
"remove_encapsulating_word_markers",
"unique",
"notnull"
]
},
"peliasQueryPartialToken" : {
"type": "custom",
"tokenizer" : "peliasNameTokenizer",
"char_filter" : ["punctuation", "nfkc_normalizer"],
"filter": [
"lowercase",
"icu_folding",
"trim",
"partial_token_address_suffix_expansion",
"ampersand",
"remove_ordinals",
"removeAllZeroNumericPrefix",
"unique",
"notnull"
]
},
"peliasQueryFullToken" : {
"type": "custom",
"tokenizer" : "peliasNameTokenizer",
"char_filter" : ["punctuation", "nfkc_normalizer"],
"filter": [
"lowercase",
"icu_folding",
"trim",
"remove_ordinals",
"full_token_address_suffix_expansion",
"ampersand",
"removeAllZeroNumericPrefix",
"unique",
"notnull"
]
},
"peliasPhrase": {
"type": "custom",
"tokenizer":"peliasNameTokenizer",
"char_filter" : ["punctuation", "nfkc_normalizer"],
"filter": [
"lowercase",
"icu_folding",
"trim",
"custom_name",
"ampersand",
"street_suffix_contractions",
"directionals",
"unique",
"notnull"
]
},
"peliasZip": {
"type": "custom",
"tokenizer":"keyword",
"char_filter" : ["alphanumeric"],
"filter": [
"lowercase",
"trim"
]
},
"peliasUnit": {
"type": "custom",
"tokenizer":"keyword",
"char_filter" : ["alphanumeric"],
"filter": [
"lowercase",
"trim"
]
},
"peliasHousenumber": {
"type": "custom",
"tokenizer":"standard",
"char_filter" : ["numeric"]
},
"peliasStreet": {
"type": "custom",
"tokenizer":"peliasStreetTokenizer",
"char_filter" : ["punctuation", "nfkc_normalizer"],
"filter": [
"lowercase",
"icu_folding",
"remove_duplicate_spaces",
"custom_street",
].concat( synonyms.street_suffix_contractions.map( function( synonym ){
return "keyword_street_suffix_" + synonym.split(' ')[0];
})).concat( synonyms.directionals.map( function( synonym ){
return "keyword_compass_" + synonym.split(' ')[0];
})).concat([
"remove_ordinals",
"trim"
])
}
},
"filter" : {
"notnull" :{
"type" : "length",
"min" : 1
},
"peliasOneEdgeGramFilter": {
"type" : "edgeNGram",
"min_gram" : 1,
"max_gram" : 24
},
"peliasTwoEdgeGramFilter": {
"type" : "edgeNGram",
"min_gram" : 2,
"max_gram" : 24
},
"removeAllZeroNumericPrefix" :{
"type" : "pattern_replace",
"pattern" : "^(0*)",
"replacement" : ""
},
"remove_ordinals" : {
"type" : "pattern_replace",
"pattern": "(?i)((^| )((1)st?|(2)nd?|(3)rd?|([4-9])th?)|(([0-9]*)(1[0-9])th?)|(([0-9]*[02-9])((1)st?|(2)nd?|(3)rd?|([04-9])th?))($| ))",
"replacement": "$2$4$5$6$7$9$10$12$14$15$16$17$18"
},
"remove_duplicate_spaces" : {
"type" : "pattern_replace",
"pattern": " +",
"replacement": " "
},
// START OF COMPLICATED FILTERS TO ANALYZE HOUSE NUMBERS
// @see: https://github.com/pelias/schema/pull/133
// note: we use \x02 (start-of-text) and \x03 (end-of-text) characters to mark word borders
"surround_single_characters_with_word_markers":{
"description": "wraps single characters with markers, needed to protect valid single characters and not those extracted from house numbers (14a creates an 'a' token)",
"type": "pattern_replace",
"pattern": "^(.{1})$",
"replacement": "\x02$1\x03"
},
"house_number_word_delimiter": {
"description": "splits on letter-to-number transition and vice versa, splits 14a -> [14, 14a, a]",
"type": "word_delimiter",
"split_on_numerics": "true",
"preserve_original": "true"
},
"remove_single_characters": {
"description": "removes single characters created from house_number_word_delimiter, removes the letter portion of a house number",
"type": "length",
"min": 2
},
"surround_house_numbers_with_word_markers": {
"description": "surrounds house numbers with markers, needed to protect whole house numbers from elimination step after prefix n-gramming",
"type": "pattern_replace",
"pattern": "^([0-9]+[a-z]?)$",
"replacement": "\x02$1\x03"
},
"eliminate_tokens_starting_with_word_marker": {
"description": "remove tokens starting but not ending with markers, saves whole house numbers wrapped in markers",
"type": "pattern_replace",
"pattern": "^\x02(.*[^\x03])?$",
"replacement": ""
},
"remove_encapsulating_word_markers": {
"description": "extract the stuff between the markers, extract 14 from \x0214\x03 since we're done the prefix n-gramming step",
"type": "pattern_replace",
"pattern": "^\x02(.*)\x03$",
"replacement": "$1"
}
// END OF COMPLICATED FILTERS TO ANALYZE HOUSE NUMBERS
// more generated below
},
"char_filter": {
"punctuation" : {
"type" : "mapping",
"mappings" : punctuation.blacklist.map(function(c){
return c + '=>';
})
},
"alphanumeric" : {
"type" : "pattern_replace",
"pattern": "[^a-zA-Z0-9]",
"replacement": ""
},
"numeric" : {
"type" : "pattern_replace",
"pattern": "[^0-9]",
"replacement": " "
},
"nfkc_normalizer": {
"type": "icu_normalizer",
"name": "nfkc",
"mode": "compose"
}
}
}
};
// dynamically create filters for all synonym files in the ./synonyms directory.
// each filter is given the same name as the file, minus the extension.
for( var key in synonyms ){
settings.analysis.filter[key] = {
"type": "synonym",
"synonyms": !!synonyms[key].length ? synonyms[key] : ['']
};
}
// dynamically create filters which can replace text *inside* a token.
// we are not able to re-use the synonym functionality in elasticsearch
// because it only matches whole tokens, not strings *within* tokens.
// eg. synonyms are capable of ['street'] => ['st'] but not
// ['sesame street'] => ['sesame st']
// street suffix filters (replace text inside tokens)
// based off synonym list
synonyms.street_suffix_contractions.forEach( function( synonym ){
var split = synonym.split(' ');
settings.analysis.filter[ "keyword_street_suffix_" + split[0] ] = {
"type": "pattern_replace",
"pattern": " " + split[0],
"replacement": " " + split[2]
};
});
// compass prefix filters (replace text inside tokens)
// based off directionals list
synonyms.directionals.forEach( function( synonym ){
var split = synonym.split(' ');
settings.analysis.filter[ "keyword_compass_" + split[0] ] = {
"type": "pattern_replace",
"pattern": split[0],
"replacement": split[2]
};
});
// Merge settings from pelias/config
if( 'object' === typeof config &&
'object' === typeof config.elasticsearch &&
'object' === typeof config.elasticsearch.settings ){
return merge({}, settings, config.elasticsearch.settings);
}
return settings;
}
module.exports = generate;