-
-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathsearch.js
More file actions
182 lines (148 loc) · 4.83 KB
/
search.js
File metadata and controls
182 lines (148 loc) · 4.83 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
const _ = require('lodash');
const peliasQuery = require('pelias-query');
const defaults = require('./search_defaults');
const textParser = require('./text_parser');
//------------------------------
// general-purpose search query
//------------------------------
var fallbackQuery = new peliasQuery.layout.FallbackQuery();
// scoring boost
fallbackQuery.score( peliasQuery.view.focus_only_function( ) );
fallbackQuery.score( peliasQuery.view.popularity_only_function );
fallbackQuery.score( peliasQuery.view.population_only_function );
// --------------------------------
// non-scoring hard filters
fallbackQuery.filter( peliasQuery.view.leaf.multi_match('boundary_country') );
fallbackQuery.filter( peliasQuery.view.boundary_circle );
fallbackQuery.filter( peliasQuery.view.boundary_rect );
fallbackQuery.filter( peliasQuery.view.sources );
fallbackQuery.filter( peliasQuery.view.layers );
fallbackQuery.filter( peliasQuery.view.categories );
fallbackQuery.filter( peliasQuery.view.boundary_gid );
// --------------------------------
/**
map request variables to query variables for all inputs
provided by this HTTP request.
**/
function generateQuery( clean ){
const vs = new peliasQuery.Vars( defaults );
// input text
vs.var( 'input:name', clean.text );
// 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);
}
// categories
if (clean.categories && !_.isEmpty(clean.categories)) {
vs.var('input:categories', clean.categories);
}
// size
if( clean.querySize ) {
vs.var( 'size', clean.querySize );
}
// 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 country
if( _.isArray(clean['boundary.country']) && !_.isEmpty(clean['boundary.country']) ){
vs.set({
'multi_match:boundary_country:input': clean['boundary.country'].join(' ')
});
}
// boundary gid
if ( _.isString(clean['boundary.gid']) ){
vs.set({
'boundary:gid': clean['boundary.gid']
});
}
// run the address parser
if( clean.parsed_text ){
textParser( clean.parsed_text, vs );
}
var q = getQuery(vs);
//console.log(JSON.stringify(q, null, 2));
return q;
}
function getQuery(vs) {
if (hasStreet(vs) || isPostalCodeOnly(vs) || isPostalCodeWithAdmin(vs)) {
return {
type: 'search_fallback',
body: fallbackQuery.render(vs)
};
}
// returning undefined is a signal to a later step that a fallback parser
// query should be queried for
return undefined;
}
function determineQueryType(vs) {
if (vs.isset('input:housenumber') && vs.isset('input:street')) {
return 'address';
}
else if (vs.isset('input:street')) {
return 'street';
}
else if (vs.isset('input:query')) {
return 'venue';
}
else if (['neighbourhood', 'borough', 'postcode', 'county', 'region','country'].some(
layer => vs.isset(`input:${layer}`)
)) {
return 'admin';
}
return 'other';
}
function hasStreet(vs) {
return vs.isset('input:street');
}
function isPostalCodeOnly(vs) {
var isSet = layer => vs.isset(`input:${layer}`);
var allowedFields = ['postcode'];
var disallowedFields = ['query', 'category', 'housenumber', 'street',
'neighbourhood', 'borough', 'county', 'region', 'country'];
return allowedFields.every(isSet) &&
!disallowedFields.some(isSet);
}
function isPostalCodeWithAdmin(vs) {
var isSet = (layer) => {
return vs.isset(`input:${layer}`);
};
var disallowedFields = ['query', 'category', 'housenumber', 'street'];
return isSet('postcode') &&
!disallowedFields.some(isSet);
}
module.exports = generateQuery;