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

Adding support for unit field in structured search. #1127

Merged
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
33 changes: 22 additions & 11 deletions controller/structured_libpostal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ const Debug = require('../helper/debug');
const debugLog = new Debug('controller:libpostal');
const logger = require('pelias-logger').get('api');

// if there's a house_number in the libpostal response, return it
// otherwise return the postcode field (which may be undefined)
function findHouseNumberField(response) {
const house_number_field = response.find(f => f.label === 'house_number');

if (house_number_field) {
return house_number_field;
// Find field in libpostal response
function findField(response, field, replacementField) {
const libpostalField = response.find(f => f.label === field);

if (libpostalField) {
return libpostalField;
} else if(replacementField) {
return response.find(f => f.label === replacementField);
} else {
return;
}

return response.find(f => f.label === 'postcode');

}


function setup(libpostalService, should_execute) {
function controller( req, res, next ){
// bail early if req/res don't pass conditions for execution
Expand All @@ -35,7 +36,9 @@ function setup(libpostalService, should_execute) {
// libpostal parses some inputs, like `3370 cobbe ave`, as a postcode+street
// so because we're treating the entire field as a street address, it's safe
// to assume that an identified postcode is actually a house number.
const house_number_field = findHouseNumberField(response);
// if there's a house_number in the libpostal response, return it
// otherwise return the postcode field (which may be undefined)
const house_number_field = findField(response, 'house_number', 'postcode');

// if we're fairly certain that libpostal identified a house number
// (from either the house_number or postcode field), place it into the
Expand All @@ -48,6 +51,14 @@ function setup(libpostalService, should_execute) {
// remove the first instance of the number and trim whitespace
req.clean.parsed_text.street = _.trim(_.replace(req.clean.parsed_text.address, req.clean.parsed_text.number, ''));

// If libpostal have parsed unit then add it for search
const unit_field = findField(response, 'unit');
if(unit_field) {
req.clean.parsed_text.unit = unit_field.value;
// Removing unit from street and trim
req.clean.parsed_text.street = _.trim(_.replace(req.clean.parsed_text.street, req.clean.parsed_text.unit, ''));
}

} else {
// otherwise no house number was identifiable, so treat the entire input
// as a street
Expand Down
49 changes: 49 additions & 0 deletions test/unit/controller/structured_libpostal.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,55 @@ module.exports.tests.success_conditions = (test, common) => {

});

test('service returning house_number and unit should set req.clean.parsed_text.unit', t => {
const service = (req, callback) => {
const response = [
{
label: 'road',
value: 'the street'
},
{
label: 'house_number',
value: '22'
},
{
label: 'unit',
value: '1 th'
}
];

callback(null, response);
};

const controller = libpostal(service, () => true);

const req = {
clean: {
parsed_text: {
address: 'the street 22 1 th'
}
},
errors: []
};

controller(req, undefined, () => {
t.deepEquals(req, {
clean: {
parsed_text: {
street: 'the street',
number: '22',
unit: '1 th'
}
},
errors: []
}, 'req should have been modified');

t.end();

});

});

// test('service returning valid response should convert and append', t => {
// const service = (req, callback) => {
// const response = [
Expand Down