From 9d1c507d5fd7d52ad754510c6267e0fba634c931 Mon Sep 17 00:00:00 2001 From: sweco-semhul Date: Thu, 2 Nov 2017 12:01:45 +0100 Subject: [PATCH] Adding unit attribute for addresses in api search --- query/autocomplete_defaults.js | 4 + query/reverse_defaults.js | 4 + query/search_defaults.js | 4 + query/search_original.js | 1 + query/text_parser.js | 5 + query/text_parser_addressit.js | 5 + .../search_full_address_original_with_unit.js | 161 ++++++++++++++++++ test/unit/query/search_original.js | 23 +++ test/unit/query/text_parser.js | 5 + 9 files changed, 212 insertions(+) create mode 100644 test/unit/fixture/search_full_address_original_with_unit.js diff --git a/query/autocomplete_defaults.js b/query/autocomplete_defaults.js index 34f396ae..b90d547d 100644 --- a/query/autocomplete_defaults.js +++ b/query/autocomplete_defaults.js @@ -36,6 +36,10 @@ module.exports = _.merge({}, peliasQuery.defaults, { 'function_score:score_mode': 'avg', 'function_score:boost_mode': 'replace', + 'address:unit:analyzer': 'peliasUnit', + 'address:unit:field': 'address_parts.unit', + 'address:unit:boost': 2, + 'address:housenumber:analyzer': 'peliasHousenumber', 'address:housenumber:field': 'address_parts.number', 'address:housenumber:boost': 2, diff --git a/query/reverse_defaults.js b/query/reverse_defaults.js index bce72057..bb3aa9bb 100644 --- a/query/reverse_defaults.js +++ b/query/reverse_defaults.js @@ -37,6 +37,10 @@ module.exports = _.merge({}, peliasQuery.defaults, { 'function_score:score_mode': 'avg', 'function_score:boost_mode': 'replace', + 'address:unit:analyzer': 'peliasUnit', + 'address:unit:field': 'address_parts.unit', + 'address:unit:boost': 2, + 'address:housenumber:analyzer': 'peliasHousenumber', 'address:housenumber:field': 'address_parts.number', 'address:housenumber:boost': 2, diff --git a/query/search_defaults.js b/query/search_defaults.js index c0d2a6a0..c6fa8b21 100644 --- a/query/search_defaults.js +++ b/query/search_defaults.js @@ -36,6 +36,10 @@ module.exports = _.merge({}, peliasQuery.defaults, { 'function_score:score_mode': 'avg', 'function_score:boost_mode': 'replace', + 'address:unit:analyzer': 'peliasUnit', + 'address:unit:field': 'address_parts.unit', + 'address:unit:boost': 2, + 'address:housenumber:analyzer': 'peliasHousenumber', 'address:housenumber:field': 'address_parts.number', 'address:housenumber:boost': 2, diff --git a/query/search_original.js b/query/search_original.js index a6b666d6..0be40480 100644 --- a/query/search_original.js +++ b/query/search_original.js @@ -30,6 +30,7 @@ query.score( peliasQuery.view.popularity( peliasQuery.view.phrase ) ); query.score( peliasQuery.view.population( peliasQuery.view.phrase ) ); // address components +query.score( peliasQuery.view.address('unit') ); query.score( peliasQuery.view.address('housenumber') ); query.score( peliasQuery.view.address('street') ); query.score( peliasQuery.view.address('postcode') ); diff --git a/query/text_parser.js b/query/text_parser.js index fd07cd0d..8db93502 100644 --- a/query/text_parser.js +++ b/query/text_parser.js @@ -19,6 +19,11 @@ function addParsedVariablesToQueryVariables( parsed_text, vs ){ vs.var( 'input:address', parsed_text.address ); } + // unit - an apartment, unit, office, lot, or other secondary unit designator + if( ! _.isEmpty(parsed_text.unit) ){ + vs.var( 'input:unit', parsed_text.unit ); + } + // house number if( ! _.isEmpty(parsed_text.number) ){ vs.var( 'input:housenumber', parsed_text.number ); diff --git a/query/text_parser_addressit.js b/query/text_parser_addressit.js index 84979c15..2359416e 100644 --- a/query/text_parser_addressit.js +++ b/query/text_parser_addressit.js @@ -36,6 +36,11 @@ function addParsedVariablesToQueryVariables( parsed_text, vs ){ // ==== add parsed matches [address components] ==== + // unit + if( parsed_text.hasOwnProperty('unit') ){ + vs.var( 'input:unit', parsed_text.unit ); + } + // house number if( parsed_text.hasOwnProperty('number') ){ vs.var( 'input:housenumber', parsed_text.number ); diff --git a/test/unit/fixture/search_full_address_original_with_unit.js b/test/unit/fixture/search_full_address_original_with_unit.js new file mode 100644 index 00000000..679b2303 --- /dev/null +++ b/test/unit/fixture/search_full_address_original_with_unit.js @@ -0,0 +1,161 @@ +var vs = require('../../../query/search_defaults'); + +module.exports = { + 'query': { + 'bool': { + 'must': [ + { + 'match': { + 'name.default': { + 'analyzer': 'peliasQueryFullToken', + 'boost': 1, + 'query': '431 St Kilda Rd' + } + } + } + ], + 'should': [ + { + 'match': { + 'phrase.default': { + 'analyzer': 'peliasPhrase', + 'type': 'phrase', + 'boost': 1, + 'slop': 2, + 'query': '431 St Kilda Rd' + } + } + }, + { + 'function_score': { + 'query': { + 'match': { + 'phrase.default': { + 'analyzer': 'peliasPhrase', + 'type': 'phrase', + 'boost': 1, + 'slop': 2, + 'query': '431 St Kilda Rd' + } + } + }, + 'max_boost': 20, + 'functions': [ + { + 'field_value_factor': { + 'modifier': 'log1p', + 'field': 'popularity', + 'missing': 1 + }, + 'weight': 1 + } + ], + 'score_mode': 'first', + 'boost_mode': 'replace' + } + }, + { + 'function_score': { + 'query': { + 'match': { + 'phrase.default': { + 'analyzer': 'peliasPhrase', + 'type': 'phrase', + 'boost': 1, + 'slop': 2, + 'query': '431 St Kilda Rd' + } + } + }, + 'max_boost': 20, + 'functions': [ + { + 'field_value_factor': { + 'modifier': 'log1p', + 'field': 'population', + 'missing': 1 + }, + 'weight': 2 + } + ], + 'score_mode': 'first', + 'boost_mode': 'replace' + } + }, + { + 'match': { + 'address_parts.unit': { + 'analyzer': vs['address:unit:analyzer'], + 'boost': vs['address:unit:boost'], + 'query': '8' + } + } + }, + { + 'match': { + 'address_parts.number': { + 'analyzer': vs['address:housenumber:analyzer'], + 'boost': vs['address:housenumber:boost'], + 'query': '431' + } + } + }, + { + 'match': { + 'address_parts.street': { + 'analyzer': vs['address:street:analyzer'], + 'boost': vs['address:street:boost'], + 'query': 'St Kilda Rd' + } + } + }, + { + 'match': { + 'parent.region_a': { + 'analyzer': vs['admin:region_a:analyzer'], + 'boost': vs['admin:region_a:boost'], + 'query': 'Melbourne' + } + } + }, + { + 'multi_match': { + 'fields': [ + 'parent.country^1', + 'parent.region^1', + 'parent.county^1', + 'parent.localadmin^1', + 'parent.locality^1', + 'parent.borough^1', + 'parent.neighbourhood^1', + 'parent.region_a^1' + ], + 'query': 'Melbourne', + 'analyzer': 'peliasAdmin' + } + } + ], + 'filter': [ + { + 'terms': { + 'layer': [ + 'address', + 'venue', + 'country', + 'region', + 'county', + 'neighbourhood', + 'locality', + 'localadmin' + ] + } + } + ] + } + }, + 'size': 10, + 'track_scores': true, + 'sort': [ + '_score' + ] +}; diff --git a/test/unit/query/search_original.js b/test/unit/query/search_original.js index 5e035679..c0dd1b53 100644 --- a/test/unit/query/search_original.js +++ b/test/unit/query/search_original.js @@ -113,6 +113,29 @@ module.exports.tests.query = function(test, common) { t.end(); }); + test('valid query with a full valid address with unit', function(t) { + var query = generate({ text: 'Shop 8, 431 St Kilda Rd Melbourne', + layers: [ 'address', 'venue', 'country', 'region', 'county', 'neighbourhood', 'locality', 'localadmin' ], + querySize: 10, + parsed_text: { + unit: '8', + number: '431', + street: 'St Kilda Rd', + regions: [ 'Melbourne' ] + } + }); + + var compiled = JSON.parse( JSON.stringify( query ) ); + + var expected = require('../fixture/search_full_address_original_with_unit'); + console.log(JSON.stringify( expected , null, 2)); + console.log('----'); + console.log(JSON.stringify( compiled.body , null, 2)); + t.deepEqual(compiled.type, 'original', 'query type set'); + t.deepEqual(compiled.body, expected, 'search_full_address'); + t.end(); + }); + test('valid query with partial address', function(t) { var query = generate({ text: 'soho grand, new york', layers: [ 'address', 'venue', 'country', 'region', 'county', 'neighbourhood', 'locality', 'localadmin' ], diff --git a/test/unit/query/text_parser.js b/test/unit/query/text_parser.js index 86d443fc..2b641818 100644 --- a/test/unit/query/text_parser.js +++ b/test/unit/query/text_parser.js @@ -19,6 +19,7 @@ module.exports.tests.query = function(test, common) { t.false(vs.isset('input:query')); t.false(vs.isset('input:category')); + t.false(vs.isset('input:unit')); t.false(vs.isset('input:housenumber')); t.false(vs.isset('input:street')); t.false(vs.isset('input:address')); @@ -37,6 +38,7 @@ module.exports.tests.query = function(test, common) { var parsed_text = { query: 'query value', category: 'category value', + unit: 'unit value', number: 'number value', street: 'street value', address: 'address value', @@ -54,6 +56,7 @@ module.exports.tests.query = function(test, common) { t.equals(vs.var('input:query').toString(), 'query value'); t.equals(vs.var('input:category').toString(), 'category value'); + t.equals(vs.var('input:unit').toString(), 'unit value'); t.equals(vs.var('input:housenumber').toString(), 'number value'); t.equals(vs.var('input:street').toString(), 'street value'); t.equals(vs.var('input:address').toString(), 'address value'); @@ -162,6 +165,7 @@ module.exports.tests.empty_values = function(test, common) { var parsed_text = { query: '', category: '', + unit: '', number: '', street: '', address: '', @@ -183,6 +187,7 @@ module.exports.tests.empty_values = function(test, common) { t.false(vs.isset('input:query')); t.false(vs.isset('input:category')); + t.false(vs.isset('input:unit')); t.false(vs.isset('input:housenumber')); t.false(vs.isset('input:street')); t.false(vs.isset('input:address'));