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

categories updates #1273

Merged
merged 2 commits into from
Apr 10, 2019
Merged
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
6 changes: 6 additions & 0 deletions query/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ query.filter( peliasQuery.view.sources );
query.filter( peliasQuery.view.layers );
query.filter( peliasQuery.view.boundary_rect );
query.filter( peliasQuery.view.boundary_country );
query.filter( peliasQuery.view.categories );
query.filter( peliasQuery.view.boundary_gid );

// --------------------------------
Expand Down Expand Up @@ -135,6 +136,11 @@ function generateQuery( clean ){
});
}

// categories
if (clean.categories && clean.categories.length) {
vs.var('input:categories', clean.categories);
}

// run the address parser
if( clean.parsed_text ){
textParser( clean, vs );
Expand Down
2 changes: 1 addition & 1 deletion query/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function generateQuery( clean ){
}

// categories
if (clean.categories) {
if (clean.categories && clean.categories.length) {
vs.var('input:categories', clean.categories);
}

Expand Down
2 changes: 1 addition & 1 deletion query/search_original.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function generateQuery( clean ){
}

// categories
if (clean.categories) {
if (clean.categories && clean.categories.length) {
vs.var('input:categories', clean.categories);
}

Expand Down
49 changes: 19 additions & 30 deletions sanitizer/_categories.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,44 @@
var check = require('check-types');
var categoryTaxonomy = require('pelias-categories');

var ERRORS = {
empty: 'Categories parameter cannot be left blank. See documentation of service for valid options.',
invalid: 'Invalid categories parameter value(s). See documentation of service for valid options.'
var WARNINGS = {
empty: 'Categories parameter left blank, showing results from all categories.'
};

// validate inputs, convert types and apply defaults
function _sanitize( raw, clean, categories ) {

function _sanitize (raw, clean, categories) {
categories = categories || categoryTaxonomy;

// error & warning messages
var messages = {errors: [], warnings: []};
var messages = { errors: [], warnings: [] };

// it's not a required parameter, so if it's not provided just move on
if (!raw.hasOwnProperty('categories')) {
return messages;
}

if (!check.nonEmptyString(raw.categories)) {
messages.errors.push(ERRORS.empty);
return messages;
}

// if categories string has been set
// map input categories to valid format
try {
clean.categories = raw.categories.split(',')
.map(function (cat) {
return cat.toLowerCase().trim(); // lowercase inputs
})
.filter(function (cat) {
if (check.nonEmptyString(cat) && categories.isValidCategory(cat)) {
return true;
}
throw new Error('Empty string value');
});
} catch (err) {
// remove everything from the list if there was any error
delete clean.categories;
}

if (check.undefined(clean.categories) || check.emptyArray(clean.categories)) {
messages.errors.push(ERRORS.invalid);
clean.categories = raw.categories.split(',')
.map(cat => {
return cat.toLowerCase().trim(); // lowercase inputs
})
.filter(cat => {
if (check.nonEmptyString(cat) && categories.isValidCategory(cat)) {
return true;
}
return false;
});

if( !clean.categories.length ){
// display a warning that the input was empty
messages.warnings.push(WARNINGS.empty);
}

return messages;
}

function _expected() {
function _expected () {
return [{ name: 'categories' }];
}
// export function
Expand Down
66 changes: 66 additions & 0 deletions test/unit/fixture/autocomplete_with_category_filtering.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module.exports = {
'query': {
'bool': {
'must': [{
'constant_score': {
'query': {
'match': {
'name.default': {
'analyzer': 'peliasQueryPartialToken',
'cutoff_frequency': 0.01,
'boost': 100,
'query': 'test',
'type': 'phrase',
'operator': 'and',
'slop': 3
}
}
}
}
}],
'should': [{
'function_score': {
'query': {
'match_all': {}
},
'max_boost': 20,
'score_mode': 'first',
'boost_mode': 'replace',
'functions': [{
'field_value_factor': {
'modifier': 'log1p',
'field': 'popularity',
'missing': 1
},
'weight': 1
}]
}
}, {
'function_score': {
'query': {
'match_all': {}
},
'max_boost': 20,
'score_mode': 'first',
'boost_mode': 'replace',
'functions': [{
'field_value_factor': {
'modifier': 'log1p',
'field': 'population',
'missing': 1
},
'weight': 3
}]
}
}],
'filter': [{
'terms': {
'category': ['retail', 'food']
}
}]
}
},
'sort': ['_score'],
'size': 20,
'track_scores': true
};
19 changes: 19 additions & 0 deletions test/unit/query/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,25 @@ module.exports.tests.query = function(test, common) {
t.end();
});

test('valid categories filter', function (t) {
var clean = {
text: 'test',
tokens: ['test'],
tokens_complete: [],
tokens_incomplete: ['test'],
categories: ['retail', 'food']
};

var query = generate(clean);

var compiled = JSON.parse( JSON.stringify( query ) );
var expected = require('../fixture/autocomplete_with_category_filtering');

t.deepEqual(compiled.type, 'autocomplete', 'query type set');
t.deepEqual(compiled.body, expected, 'valid autocomplete query with category filtering');
t.end();
});

test('single character street address', function(t) {
var query = generate({
text: 'k road, laird',
Expand Down
53 changes: 34 additions & 19 deletions test/unit/sanitizer/_categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ module.exports.tests.no_categories = function(test, common) {
clean: { }
};

var expected_error = 'Categories parameter cannot be left blank. See documentation of service for valid options.';
var expected_warning = 'Categories parameter left blank, showing results from all categories.';

var messages = sanitizer.sanitize(req.query, req.clean);

t.equal(req.clean.categories, undefined, 'no categories should be defined');
t.deepEqual(messages.errors.length, 1, 'error returned');
t.deepEqual(messages.errors[0], expected_error, 'error returned');
t.deepEqual(messages.warnings, [], 'no warnings returned');
t.deepEqual(req.clean.categories, [], 'empty categories array should be defined');
t.deepEqual(messages.errors, [], 'no errors returned');
t.deepEqual(messages.warnings.length, 1, 'warning returned');
t.deepEqual(messages.warnings[0], expected_warning, 'warning returned');
t.end();
});

Expand All @@ -44,13 +44,28 @@ module.exports.tests.no_categories = function(test, common) {
clean: { }
};

var expected_error = 'Invalid categories parameter value(s). See documentation of service for valid options.';
var expected_warning = 'Categories parameter left blank, showing results from all categories.';

var messages = sanitizer.sanitize(req.query, req.clean);

t.equal(req.clean.categories, undefined, 'no categories should be defined');
t.deepEqual(messages.errors.length, 1, 'error returned');
t.deepEqual(messages.errors[0], expected_error, 'error returned');
t.deepEqual(req.clean.categories, [], 'empty categories array should be defined');
t.deepEqual(messages.errors, [], 'no errors returned');
t.deepEqual(messages.warnings.length, 1, 'warning returned');
t.deepEqual(messages.warnings[0], expected_warning, 'warning returned');
t.end();
});

test('categories is mix of valid categories and empty strings', function (t) {
var req = {
query: {
categories: ',food,'
},
clean: {}
};

var messages = sanitizer.sanitize(req.query, req.clean);
t.deepEqual(req.clean.categories, ['food'], 'junk trimmed');
t.deepEqual(messages.errors, [], 'no errors returned');
t.deepEqual(messages.warnings, [], 'no warnings returned');
t.end();
});
Expand Down Expand Up @@ -124,16 +139,16 @@ module.exports.tests.invalid_categories = function(test, common) {
clean: { }
};
var expected_messages = {
errors: [
'Invalid categories parameter value(s). See documentation of service for valid options.'
],
warnings: []
errors: [],
warnings: [
'Categories parameter left blank, showing results from all categories.'
]
};

var messages = sanitizer.sanitize(req.query, req.clean, validCategories);

t.deepEqual(messages, expected_messages, 'error with message returned');
t.equal(req.clean.categories, undefined, 'clean.categories should remain empty');
t.deepEqual(req.clean.categories, [], 'empty categories array should be defined');
t.end();
});

Expand All @@ -145,16 +160,16 @@ module.exports.tests.invalid_categories = function(test, common) {
clean: { }
};
var expected_messages = {
errors: [
'Invalid categories parameter value(s). See documentation of service for valid options.'
],
warnings: []
errors: [],
warnings: [
'Categories parameter left blank, showing results from all categories.'
]
};

var messages = sanitizer.sanitize(req.query, req.clean, validCategories);

t.deepEqual(messages, expected_messages, 'error with message returned');
t.equal(req.clean.categories, undefined, 'clean.categories should remain empty');
t.deepEqual(req.clean.categories, [], 'empty categories array should be defined');
t.end();
});

Expand Down