From ab19a12e1c63ee39ddd2580565f6b18747e4a9cd Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Mon, 4 Apr 2016 11:45:53 -0400 Subject: [PATCH] Use multi_match query for admin fields --- query/search.js | 18 ++++-- test/unit/fixture/search_full_address.js | 65 ++++--------------- test/unit/fixture/search_partial_address.js | 71 +++++---------------- test/unit/fixture/search_regions_address.js | 65 ++++--------------- 4 files changed, 50 insertions(+), 169 deletions(-) diff --git a/query/search.js b/query/search.js index 77fcb3f5..5689d9ec 100644 --- a/query/search.js +++ b/query/search.js @@ -3,6 +3,13 @@ var peliasQuery = require('pelias-query'), textParser = require('./text_parser'), check = require('check-types'), geolib = require('geolib'); +var placeTypes = require('../helper/placeTypes'); + +// region_a is also an admin field. addressit tries to detect +// region_a, in which case we use a match query specifically for it. +// but address it doesn't know about all of them so it helps to search +// against this with the other admin parts as a fallback +var adminFields = placeTypes.concat(['region_a']); //------------------------------ // general-purpose search query @@ -25,15 +32,12 @@ query.score( peliasQuery.view.address('street') ); query.score( peliasQuery.view.address('postcode') ); // admin components -query.score( peliasQuery.view.admin('country') ); +// country_a and region_a are left as matches here because the text-analyzer +// can sometimes detect them, in which case a query more specific than a +// multi_match is appropriate. query.score( peliasQuery.view.admin('country_a') ); -query.score( peliasQuery.view.admin('region') ); query.score( peliasQuery.view.admin('region_a') ); -query.score( peliasQuery.view.admin('county') ); -query.score( peliasQuery.view.admin('borough') ); -query.score( peliasQuery.view.admin('localadmin') ); -query.score( peliasQuery.view.admin('locality') ); -query.score( peliasQuery.view.admin('neighbourhood') ); +query.score( peliasQuery.view.admin_multi_match(adminFields), 'peliasAdmin' ); // non-scoring hard filters query.filter( peliasQuery.view.boundary_circle ); diff --git a/test/unit/fixture/search_full_address.js b/test/unit/fixture/search_full_address.js index dfd64e34..c7ba7abc 100644 --- a/test/unit/fixture/search_full_address.js +++ b/test/unit/fixture/search_full_address.js @@ -99,14 +99,6 @@ module.exports = { 'analyzer': vs['address:postcode:analyzer'] } } - }, { - 'match': { - 'parent.country': { - 'query': 'new york', - 'boost': vs['admin:country:boost'], - 'analyzer': vs['admin:country:analyzer'] - } - } }, { 'match': { 'parent.country_a': { @@ -115,14 +107,6 @@ module.exports = { 'analyzer': vs['admin:country_a:analyzer'] } } - }, { - 'match': { - 'parent.region': { - 'query': 'new york', - 'boost': vs['admin:region:boost'], - 'analyzer': vs['admin:region:analyzer'] - } - } }, { 'match': { 'parent.region_a': { @@ -132,44 +116,19 @@ module.exports = { } } }, { - 'match': { - 'parent.county': { - 'query': 'new york', - 'boost': vs['admin:county:boost'], - 'analyzer': vs['admin:county:analyzer'] - } - } - }, { - 'match': { - 'parent.borough': { - 'query': 'new york', - 'boost': vs['admin:borough:boost'], - 'analyzer': vs['admin:borough:analyzer'] - } - } - }, { - 'match': { - 'parent.localadmin': { - 'query': 'new york', - 'boost': vs['admin:localadmin:boost'], - 'analyzer': vs['admin:localadmin:analyzer'] - } - } - }, { - 'match': { - 'parent.locality': { - 'query': 'new york', - 'boost': vs['admin:locality:boost'], - 'analyzer': vs['admin:locality:analyzer'] - } - } - }, { - 'match': { - 'parent.neighbourhood': { + 'multi_match': { + 'fields': [ + 'parent.country^4', + 'parent.region^3', + 'parent.county^2', + 'parent.localadmin^1', + 'parent.locality^1', + 'parent.borough^1', + 'parent.neighbourhood^1', + 'parent.region_a^3' + ], 'query': 'new york', - 'boost': vs['admin:neighbourhood:boost'], - 'analyzer': vs['admin:neighbourhood:analyzer'] - } + 'analyzer': 'peliasAdmin' } }] } diff --git a/test/unit/fixture/search_partial_address.js b/test/unit/fixture/search_partial_address.js index 746899b7..98fa8167 100644 --- a/test/unit/fixture/search_partial_address.js +++ b/test/unit/fixture/search_partial_address.js @@ -75,69 +75,28 @@ module.exports = { 'weight': 2 }] } - },{ - 'match': { - 'parent.country': { - 'query': 'new york', - 'boost': vs['admin:country:boost'], - 'analyzer': vs['admin:country:analyzer'] - } - } - }, { - 'match': { - 'parent.region': { - 'query': 'new york', - 'boost': vs['admin:region:boost'], - 'analyzer': vs['admin:region:analyzer'] - } - } }, { 'match': { 'parent.region_a': { - 'query': 'new york', - 'boost': vs['admin:region_a:boost'], - 'analyzer': vs['admin:region_a:analyzer'] - } - } - }, { - 'match': { - 'parent.county': { - 'query': 'new york', - 'boost': vs['admin:county:boost'], - 'analyzer': vs['admin:county:analyzer'] + 'analyzer': 'peliasAdmin', + 'boost': 3, + 'query': 'new york' } } }, { - 'match': { - 'parent.borough': { + 'multi_match': { + 'fields': [ + 'parent.country^4', + 'parent.region^3', + 'parent.county^2', + 'parent.localadmin^1', + 'parent.locality^1', + 'parent.borough^1', + 'parent.neighbourhood^1', + 'parent.region_a^3' + ], 'query': 'new york', - 'boost': vs['admin:borough:boost'], - 'analyzer': vs['admin:borough:analyzer'] - } - } - }, { - 'match': { - 'parent.localadmin': { - 'query': 'new york', - 'boost': vs['admin:localadmin:boost'], - 'analyzer': vs['admin:localadmin:analyzer'] - } - } - }, { - 'match': { - 'parent.locality': { - 'query': 'new york', - 'boost': vs['admin:locality:boost'], - 'analyzer': vs['admin:locality:analyzer'] - } - } - }, { - 'match': { - 'parent.neighbourhood': { - 'query': 'new york', - 'boost': vs['admin:neighbourhood:boost'], - 'analyzer': vs['admin:neighbourhood:analyzer'] - } + 'analyzer': 'peliasAdmin' } }] } diff --git a/test/unit/fixture/search_regions_address.js b/test/unit/fixture/search_regions_address.js index 0a8b199d..264d5eea 100644 --- a/test/unit/fixture/search_regions_address.js +++ b/test/unit/fixture/search_regions_address.js @@ -91,22 +91,6 @@ module.exports = { 'analyzer': vs['address:street:analyzer'] } } - }, { - 'match': { - 'parent.country': { - 'query': 'manhattan', - 'boost': vs['admin:country:boost'], - 'analyzer': vs['admin:country:analyzer'] - } - } - }, { - 'match': { - 'parent.region': { - 'query': 'manhattan', - 'boost': vs['admin:region:boost'], - 'analyzer': vs['admin:region:analyzer'] - } - } }, { 'match': { 'parent.region_a': { @@ -116,44 +100,19 @@ module.exports = { } } }, { - 'match': { - 'parent.county': { - 'query': 'manhattan', - 'boost': vs['admin:county:boost'], - 'analyzer': vs['admin:county:analyzer'] - } - } - }, { - 'match': { - 'parent.borough': { - 'query': 'manhattan', - 'boost': vs['admin:borough:boost'], - 'analyzer': vs['admin:borough:analyzer'] - } - } - }, { - 'match': { - 'parent.localadmin': { - 'query': 'manhattan', - 'boost': vs['admin:localadmin:boost'], - 'analyzer': vs['admin:localadmin:analyzer'] - } - } - }, { - 'match': { - 'parent.locality': { - 'query': 'manhattan', - 'boost': vs['admin:locality:boost'], - 'analyzer': vs['admin:locality:analyzer'] - } - } - }, { - 'match': { - 'parent.neighbourhood': { + 'multi_match': { + 'fields': [ + 'parent.country^4', + 'parent.region^3', + 'parent.county^2', + 'parent.localadmin^1', + 'parent.locality^1', + 'parent.borough^1', + 'parent.neighbourhood^1', + 'parent.region_a^3' + ], 'query': 'manhattan', - 'boost': vs['admin:neighbourhood:boost'], - 'analyzer': vs['admin:neighbourhood:analyzer'] - } + 'analyzer': 'peliasAdmin' } }] }