Browse Source

refactor search

pull/229/head
Peter Johnson 9 years ago
parent
commit
6f817938bc
  1. 263
      query/search.js

263
query/search.js

@ -1,188 +1,161 @@
var queries = require('geopipes-elasticsearch-backend').queries, var peliasQuery = require('pelias-query'),
sort = require('../query/sort'), sort = require('../query/sort'),
adminFields = require('../helper/adminFields')(), adminFields = require('../helper/adminFields')();
addressWeights = require('../helper/address_weights');
//------------------------------
// general-purpose search query
//------------------------------
function generate( params ){ var query = new peliasQuery.layout.FilteredBooleanQuery();
var centroid = null;
if ( params.lat && params.lon ){ // mandatory matches
centroid = { query.score( peliasQuery.view.boundary_country, 'must' );
lat: params.lat, query.score( peliasQuery.view.ngrams, 'must' );
lon: params.lon
};
}
var query = queries.distance( centroid, { size: params.size } ); // scoring boost
var input = params.input; query.score( peliasQuery.view.phrase );
query.score( peliasQuery.view.focus );
if (params.bbox) { // address components
query = queries.bbox ( centroid, { size: params.size, bbox: params.bbox } ); query.score( peliasQuery.view.address('housenumber') );
} query.score( peliasQuery.view.address('street') );
query.score( peliasQuery.view.address('postcode') );
query.query.filtered.query = { // admin components
'bool': { query.score( peliasQuery.view.admin('alpha3') );
'must': [], query.score( peliasQuery.view.admin('admin0') );
'should': [] query.score( peliasQuery.view.admin('admin1') );
} query.score( peliasQuery.view.admin('admin1_abbr') );
}; query.score( peliasQuery.view.admin('admin2') );
query.score( peliasQuery.view.admin('local_admin') );
query.score( peliasQuery.view.admin('locality') );
query.score( peliasQuery.view.admin('neighborhood') );
if (params.parsed_input) { // non-scoring hard filters
// update input query.filter( peliasQuery.view.boundary_circle, 'must' );
if (params.parsed_input.number && params.parsed_input.street) { query.filter( peliasQuery.view.boundary_rect, 'must' );
input = params.parsed_input.number + ' ' + params.parsed_input.street;
} else if (params.parsed_input.admin_parts) {
input = params.parsed_input.name;
}
addParsedMatch(query, input, params.parsed_input); // --------------------------------
}
// add search condition to distance query function generate( clean ){
query.query.filtered.query.bool.must.push({
'match': { var vs = new peliasQuery.Vars( peliasQuery.defaults );
'name.default': {
'query': input,
'analyzer': 'peliasOneEdgeGram'
}
}
});
// add phrase matching query // set input text
// note: this is required for shingle/phrase matching vs.var( 'input:name', clean.input );
query.query.filtered.query.bool.should.push({
'match': { // set size
'phrase.default': { if( clean.size ){
'query': input, vs.var( 'size', clean.size );
'analyzer': 'peliasPhrase',
'type': 'phrase',
'slop': 2
} }
// focus point
if( clean.lat && clean.lon ){
vs.set({
'focus:point:lat': clean.lat,
'focus:point:lon': clean.lon
});
} }
// bbox
if( clean.bbox ){
vs.set({
'boundary:rect:top': clean.bbox.top,
'boundary:rect:right': clean.bbox.right,
'boundary:rect:bottom': clean.bbox.bottom,
'boundary:rect:left': clean.bbox.left
}); });
}
query.sort = query.sort.concat( sort( params ) ); // address parsing
if( clean.parsed_input ){
return query; // is it a street address?
} var isStreetAddress = clean.parsed_input.hasOwnProperty('number') && clean.parsed_input.hasOwnProperty('street');
if( isStreetAddress ){
vs.var( 'input:name', clean.parsed_input.number + ' ' + clean.parsed_input.street );
}
/** // I don't understand this
* Traverse the parsed input object, containing all the address parts detected in query string. else if( clean.parsed_input.admin_parts ) {
* Add matches to query for each identifiable component. vs.var( 'input:name', clean.parsed_input.name );
* }
* @param {Object} query
* @param {string} defaultInput
* @param {Object} parsedInput
*/
function addParsedMatch(query, defaultInput, parsedInput) {
query.query.filtered.query.bool.should = query.query.filtered.query.bool.should || [];
// copy expected admin fields so we can remove them as we parse the address
var unmatchedAdminFields = adminFields.slice();
// address // or this..
// number, street, postalcode else {
addMatch(query, unmatchedAdminFields, 'address.number', parsedInput.number, addressWeights.number); console.warn( 'chaos monkey asks: what happens now?' );
addMatch(query, unmatchedAdminFields, 'address.street', parsedInput.street, addressWeights.street); console.log( clean );
addMatch(query, unmatchedAdminFields, 'address.zip', parsedInput.postalcode, addressWeights.zip); try{ throw new Error(); } catch(e){ console.error( e.stack ); } // print a stack trace
}
// city // ==== add parsed matches [address components] ====
// admin2, locality, local_admin, neighborhood
addMatch(query, unmatchedAdminFields, 'admin2', parsedInput.city, addressWeights.admin2);
// state // house number
// admin1, admin1_abbr if( clean.parsed_input.hasOwnProperty('number') ){
addMatch(query, unmatchedAdminFields, 'admin1_abbr', parsedInput.state, addressWeights.admin1_abbr); vs.var( 'input:housenumber', clean.parsed_input.number );
}
// country
// admin0, alpha3
addMatch(query, unmatchedAdminFields, 'alpha3', parsedInput.country, addressWeights.alpha3);
addUnmatchedAdminFieldsToQuery(query, unmatchedAdminFields, parsedInput, defaultInput); // street name
} if( clean.parsed_input.hasOwnProperty('street') ){
vs.var( 'input:street', clean.parsed_input.street );
}
/** // postal code
* Check for additional admin fields in the parsed input, and if any was found if( clean.parsed_input.hasOwnProperty('postalcode') ){
* combine into single string and match against all unmatched admin fields. vs.var( 'input:postcode', clean.parsed_input.postalcode );
*
* @param {Object} query
* @param {Array} unmatchedAdminFields
* @param {Object} parsedInput
* @param {string} defaultInput
*/
function addUnmatchedAdminFieldsToQuery(query, unmatchedAdminFields, parsedInput, defaultInput) {
if (unmatchedAdminFields.length === 0 ) {
return;
} }
var leftovers = []; // ==== add parsed matches [admin components] ====
if (parsedInput.admin_parts) { // city
leftovers.push(parsedInput.admin_parts); if( clean.parsed_input.hasOwnProperty('city') ){
vs.var( 'input:admin2', clean.parsed_input.city );
} }
else if (parsedInput.regions) {
leftovers.push(parsedInput.regions); // state
if( clean.parsed_input.hasOwnProperty('state') ){
vs.var( 'input:admin1_abbr', clean.parsed_input.state );
} }
if (leftovers.length === 0) { // country
return; if( clean.parsed_input.hasOwnProperty('country') ){
vs.var( 'input:alpha3', clean.parsed_input.country );
} }
leftovers = leftovers.join(' '); // ==== deal with the 'leftover' components ====
// @todo: clean up this code
// if there are additional regions/admin_parts found // a concept called 'leftovers' which is just 'admin_parts' plus 'regions'.
if (leftovers !== defaultInput) { var leftovers = [];
unmatchedAdminFields.forEach(function (key) { if( clean.parsed_input.hasOwnProperty('admin_parts') ){
// combine all the leftover parts into one string leftovers.push( clean.parsed_input.admin_parts );
addMatch(query, [], key, leftovers);
});
} }
} else if( clean.parsed_input.hasOwnProperty('regions') ){
leftovers.push( clean.parsed_input.regions );
/**
* Add key:value match to query. Apply boost if specified.
*
* @param {Object} query
* @param {Array} unmatched
* @param {string} key
* @param {string|number|undefined} value
* @param {number|undefined} [boost] optional
*/
function addMatch(query, unmatched, key, value, boost) { // jshint ignore:line
if (typeof value === 'undefined') {
return;
} }
var match = {}; // if we have 'leftovers' then assign them to any fields which
// currently don't have a value assigned.
if( leftovers.length ){
var leftoversString = leftovers.join(' ');
var unmatchedAdminFields = adminFields.slice();
if (boost) { // cycle through fields and set fields which
match[key] = { // are still currently unset
query: value, unmatchedAdminFields.forEach( function( key ){
boost: boost if( !vs.isset( 'input:' + key ) ){
}; vs.var( 'input:' + key, leftoversString );
}
});
} }
else {
match[key] = value;
} }
query.query.filtered.query.bool.should.push({ 'match': match }); var result = query.render( vs );
result.sort = result.sort.concat( sort( clean ) );
removeFromUnmatched(unmatched, key); // @todo: remove this hack
} return JSON.parse( JSON.stringify( result ) );
/**
* If key is found in unmatched list, remove it from the array
*
* @param {Array} unmatched
* @param {string} key
*/
function removeFromUnmatched(unmatched, key) {
var index = unmatched.indexOf(key);
if (index !== -1) {
unmatched.splice(index, 1);
}
} }
module.exports = generate; module.exports = generate;

Loading…
Cancel
Save