Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query): Use custom query #2

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions controller/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const logger = require('pelias-logger').get('api');
const logging = require( '../helper/logging' );
const retry = require('retry');
const Debug = require('../helper/debug');
const customQuery = require('../query/custom_query');

function isRequestTimeout(err) {
return _.get(err, 'status') === 408;
Expand All @@ -27,7 +28,10 @@ function setup( peliasConfig, esclient, query, should_execute ){
// rendering a query requires passing the `clean` object, which contains
// validated options from query parameters, and the `res` object, since
// some queries use the results of previous queries to Placeholder
const renderedQuery = query(req.clean, res);
// const renderedQuery = query(req.clean, res);
const renderedQuery = customQuery(req.clean);
logger.info('req');
logger.info(JSON.stringify(req.clean));

// if there's no query to call ES with, skip the service
if (_.isUndefined(renderedQuery)) {
Expand Down Expand Up @@ -150,7 +154,7 @@ function setup( peliasConfig, esclient, query, should_execute ){
}
}});
}
logger.debug('[ES response]', docs);
// logger.debug('[ES response]', docs);
if (req.clean.enableElasticDebug) {
debugLog.push(req, { ES_response: _.cloneDeep({ docs, meta, data }) });
}
Expand Down
267 changes: 267 additions & 0 deletions query/custom_query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
function generateQuery (clean) {
const { text, layers, querySize } = clean;
// const { lang: { iso6391: lang } } = clean;
const lang = 'en';

return {
type: 'autocomplete',
body: {
size: querySize,
query: {
bool: {
must: [
{
constant_score: {
filter: {
multi_match: {
query: text,
fields: ['name.default^1.0', `name.${lang}^1.0`],
type: 'phrase',
operator: 'OR',
analyzer: 'peliasQuery',
slop: 3,
prefix_length: 0,
max_expansions: 50,
zero_terms_query: 'NONE',
auto_generate_synonyms_phrase_query: true,
fuzzy_transpositions: true,
boost: 100.0,
},
},
boost: 1.0,
},
},
],
filter: [
{
terms: {
layer: layers,
boost: 1.0,
},
},
{
bool: {
should: [
{
terms: {
layer: layers,
boost: 1.0,
},
},
{
geo_distance: {
center_point: [2.45, 46.61],
distance: 600000.0,
distance_type: 'plane',
validation_method: 'STRICT',
ignore_unmapped: false,
boost: 1.0,
},
},
],
adjust_pure_negative: true,
minimum_should_match: '1',
boost: 1.0,
},
},
],
should: [
{
function_score: {
query: {
match_all: {
boost: 1.0,
},
},
functions: [
{
filter: {
match_all: {
boost: 1.0,
},
},
weight: 15.0,
exp: {
center_point: {
origin: {
lat: 46.61,
lon: 2.45,
},
offset: '0km',
scale: '50km',
decay: 0.5,
},
multi_value_mode: 'MIN',
},
},
],
score_mode: 'avg',
boost_mode: 'replace',
max_boost: 3.4028235e38,
boost: 1.0,
},
},
{
function_score: {
query: {
match_all: {
boost: 1.0,
},
},
functions: [
{
filter: {
match_all: {
boost: 1.0,
},
},
weight: 1.0,
field_value_factor: {
field: 'popularity',
factor: 1.0,
missing: 1.0,
modifier: 'log1p',
},
},
],
score_mode: 'first',
boost_mode: 'replace',
max_boost: 20.0,
boost: 1.0,
},
},
{
function_score: {
query: {
match_all: {
boost: 1.0,
},
},
functions: [
{
filter: {
match_all: {
boost: 1.0,
},
},
weight: 3.0,
field_value_factor: {
field: 'population',
factor: 1.0,
missing: 1.0,
modifier: 'log1p',
},
},
],
score_mode: 'first',
boost_mode: 'replace',
max_boost: 20.0,
boost: 1.0,
},
},
{
function_score: {
query: {
match_all: {
boost: 1.0,
},
},
functions: [
{
filter: {
match: {
source: {
query: 'campings.com',
operator: 'OR',
prefix_length: 0,
max_expansions: 50,
fuzzy_transpositions: true,
lenient: false,
zero_terms_query: 'NONE',
auto_generate_synonyms_phrase_query: true,
boost: 1.0,
},
},
},
weight: 5.0,
},
{
filter: {
match: {
source: {
query: 'whosonfirst',
operator: 'OR',
prefix_length: 0,
max_expansions: 50,
fuzzy_transpositions: true,
lenient: false,
zero_terms_query: 'NONE',
auto_generate_synonyms_phrase_query: true,
boost: 1.0,
},
},
},
weight: 3.0,
},
{
filter: {
match: {
source: {
query: 'geonames',
operator: 'OR',
prefix_length: 0,
max_expansions: 50,
fuzzy_transpositions: true,
lenient: false,
zero_terms_query: 'NONE',
auto_generate_synonyms_phrase_query: true,
boost: 1.0,
},
},
},
weight: 1.0,
},
{
filter: {
match: {
source: {
query: 'openstreetmap',
operator: 'OR',
prefix_length: 0,
max_expansions: 50,
fuzzy_transpositions: true,
lenient: false,
zero_terms_query: 'NONE',
auto_generate_synonyms_phrase_query: true,
boost: 1.0,
},
},
},
weight: 0.0,
},
],
score_mode: 'sum',
boost_mode: 'multiply',
max_boost: 50.0,
min_score: 1.0,
boost: 5.0,
},
},
],
adjust_pure_negative: true,
boost: 1.0,
},
},
sort: [
{
_score: {
order: 'desc',
},
},
],
track_scores: true,
},
};
}

module.exports = generateQuery;
2 changes: 1 addition & 1 deletion test/unit/helper/diffPlaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ module.exports.tests.normalizeString = function (test, common) {

test('diacritics', function (t) {
t.equal(normalizeString('Malmö'), 'malmo');
t.equal(normalizeString('Grolmanstraße'), 'grolmanstrasse');
t.equal(normalizeString('Grolmanstrasse'), 'grolmanstrasse');
t.equal(normalizeString('àáâãäåấắæầằçḉèéêëếḗềḕ'), 'aaaaaaaaaeaacceeeeeeee');
t.end();
});
Expand Down