-
-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathautocomplete.js
More file actions
213 lines (181 loc) · 7.94 KB
/
Copy pathautocomplete.js
File metadata and controls
213 lines (181 loc) · 7.94 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
const _ = require('lodash');
const peliasQuery = require('pelias-query');
const defaults = require('./autocomplete_defaults');
const textParser = require('./text_parser_pelias');
const config = require('pelias-config').generate();
const placeTypes = require('../helper/placeTypes');
const toSingleField = require('./view/helper').toSingleField;
// additional views (these may be merged in to pelias/query at a later date)
var views = {
custom_boosts: require('./view/boost_sources_and_layers'),
ngrams_strict: require('./view/ngrams_strict'),
ngrams_fuzzy: require('./view/ngrams_fuzzy'),
ngrams_last_token_only: require('./view/ngrams_last_token_only'),
ngrams_last_token_only_multi: require('./view/ngrams_last_token_only_multi'),
admin_multi_match_first: require('./view/admin_multi_match_first'),
admin_multi_match_last: require('./view/admin_multi_match_last'),
phrase_first_tokens_only: require('./view/phrase_first_tokens_only'),
boost_exact_matches: require('./view/boost_exact_matches'),
boost_exact_match_first_tokens_only: require('./view/boost_exact_match_first_tokens_only'),
boost_exact_match_last_tokens_only: require('./view/boost_exact_match_last_tokens_only'),
max_character_count_layer_filter: require('./view/max_character_count_layer_filter'),
focus_point_filter: require('./view/focus_point_distance_filter')
};
// add abbrevations for the fields pelias/parser is able to detect.
var adminFields = placeTypes.concat(['locality_a', 'region_a', 'country_a']);
// add some name field(s) to the admin fields in order to improve venue matching
// note: this is a bit of a hacky way to add a 'name' field to the list
// of multimatch fields normally reserved for admin subquerying.
// in some cases we are not sure if certain tokens refer to admin components
// or are part of the place name (such as some venue names).
// the variable name 'add_name_to_multimatch' is arbitrary, it can be any value so
// long as there is a corresponding 'admin:*:field' variable set which defines
// the name of the field to use.
// this functionality is not enabled unless the 'input:add_name_to_multimatch'
// variable is set to a non-empty value at query-time.
adminFields = adminFields.concat(['add_name_to_multimatch', 'add_name_lang_to_multimatch']);
//------------------------------
// autocomplete query
//------------------------------
var query = new peliasQuery.layout.FilteredBooleanQuery();
// mandatory matches
query.score( views.phrase_first_tokens_only, 'must' );
query.score( views.ngrams_last_token_only_multi( adminFields ), 'must' );
// admin components
query.score( views.admin_multi_match_first( adminFields ), 'must');
query.score( views.admin_multi_match_last( adminFields ), 'must');
// scoring boost
query.score( views.boost_exact_match_first_tokens_only );
query.score( views.boost_exact_match_last_tokens_only );
query.score( peliasQuery.view.focus( peliasQuery.view.leaf.match_all ) );
query.score( peliasQuery.view.popularity( peliasQuery.view.leaf.match_all ) );
query.score( peliasQuery.view.population( peliasQuery.view.leaf.match_all ) );
query.score( views.custom_boosts( config.get('api.customBoosts') ) );
// non-scoring hard filters
query.filter( views.max_character_count_layer_filter(['address'], config.get('api.autocomplete.exclude_address_length' ) ) );
query.filter( peliasQuery.view.sources );
query.filter( peliasQuery.view.layers );
query.filter( peliasQuery.view.boundary_rect );
query.filter( peliasQuery.view.boundary_circle );
query.filter( peliasQuery.view.boundary_country );
query.filter( peliasQuery.view.categories );
query.filter( peliasQuery.view.boundary_gid );
query.filter( views.focus_point_filter );
// --------------------------------
/**
map request variables to query variables for all inputs
provided by this HTTP request.
**/
function generateQuery( clean ){
const vs = new peliasQuery.Vars( defaults );
// sources
if( _.isArray(clean.sources) && !_.isEmpty(clean.sources) ){
vs.var( 'sources', clean.sources );
}
// layers
if (_.isArray(clean.layers) && !_.isEmpty(clean.layers) ){
vs.var( 'layers', clean.layers);
}
// boundary country
if( _.isArray(clean['boundary.country']) && !_.isEmpty(clean['boundary.country']) ){
vs.set({
'boundary:country': clean['boundary.country'].join(' ')
});
}
// pass the input tokens to the views so they can choose which tokens
// are relevant for their specific function.
if( _.isArray( clean.tokens ) ){
vs.var( 'input:name:tokens', clean.tokens );
vs.var( 'input:name:tokens_complete', clean.tokens_complete );
vs.var( 'input:name:tokens_incomplete', clean.tokens_incomplete );
}
// input text
vs.var( 'input:name', clean.text );
// if the tokenizer has run then we set 'input:name' to as the combination of the
// 'complete' tokens with the 'incomplete' tokens, the resuting array differs
// slightly from the 'input:name:tokens' array as some tokens might have been
// removed in the process; such as single grams which are not present in then
// ngrams index.
if( _.isArray( clean.tokens_complete ) && _.isArray( clean.tokens_incomplete ) ){
var combined = clean.tokens_complete.concat( clean.tokens_incomplete );
if( combined.length ){
vs.var( 'input:name', combined.join(' ') );
}
}
// focus point
if( _.isFinite(clean['focus.point.lat']) &&
_.isFinite(clean['focus.point.lon']) ){
vs.set({
'focus:point:lat': clean['focus.point.lat'],
'focus:point:lon': clean['focus.point.lon']
});
}
// boundary rect
if( _.isFinite(clean['boundary.rect.min_lat']) &&
_.isFinite(clean['boundary.rect.max_lat']) &&
_.isFinite(clean['boundary.rect.min_lon']) &&
_.isFinite(clean['boundary.rect.max_lon']) ){
vs.set({
'boundary:rect:top': clean['boundary.rect.max_lat'],
'boundary:rect:right': clean['boundary.rect.max_lon'],
'boundary:rect:bottom': clean['boundary.rect.min_lat'],
'boundary:rect:left': clean['boundary.rect.min_lon']
});
}
// boundary circle
// @todo: change these to the correct request variable names
if( _.isFinite(clean['boundary.circle.lat']) &&
_.isFinite(clean['boundary.circle.lon']) ){
vs.set({
'boundary:circle:lat': clean['boundary.circle.lat'],
'boundary:circle:lon': clean['boundary.circle.lon']
});
if( _.isFinite(clean['boundary.circle.radius']) ){
vs.set({
'boundary:circle:radius': Math.round( clean['boundary.circle.radius'] ) + 'km'
});
}
}
// boundary gid
if( _.isString(clean['boundary.gid']) ){
vs.set({
'boundary:gid': clean['boundary.gid']
});
}
// categories
if (clean.categories && clean.categories.length) {
vs.var('input:categories', clean.categories);
}
// size
if( clean.querySize ) {
vs.var( 'size', clean.querySize );
}
// run the address parser
if( clean.parsed_text ){
textParser( clean, vs );
}
if (clean.fuzziness) {
vs.var('fuzzy:fuzziness', clean.fuzziness);
if (clean.max_expansions) {
vs.var('fuzzy:max_expansions', clean.max_expansions);
}
}
// set the 'add_name_to_multimatch' variable only in the case where one
// or more of the admin variables are set.
// the value 'enabled' is not relevant, it just needs to be any non-empty
// value so that the associated field is added to the multimatch query.
// see code comments above for additional information.
let isAdminSet = adminFields.some(field => vs.isset('input:' + field));
if ( isAdminSet ){ vs.var('input:add_name_to_multimatch', 'enabled'); }
// Search in the user lang
if(clean.lang && _.isString(clean.lang.iso6391)) {
vs.var('lang', clean.lang.iso6391);
const field = toSingleField(vs.var('admin:add_name_lang_to_multimatch:field').get(), clean.lang.iso6391);
vs.var('admin:add_name_lang_to_multimatch:field', field);
}
return {
type: 'autocomplete',
body: query.render(vs)
};
}
module.exports = generateQuery;