Browse Source

fixed the intersection query to support sorting by distance

pull/1058/head
Alex Loyko 7 years ago
parent
commit
56fd4780f3
  1. 2
      controller/intersections.js
  2. 80
      query/search_intersections.js

2
controller/intersections.js

@ -18,7 +18,7 @@ function setup(should_execute) {
if (_.has(parsed_text, 'country') && iso3166.is2(_.toUpper(parsed_text.country))) { if (_.has(parsed_text, 'country') && iso3166.is2(_.toUpper(parsed_text.country))) {
parsed_text.country = iso3166.to3(_.toUpper(parsed_text.country)); parsed_text.country = iso3166.to3(_.toUpper(parsed_text.country));
} }
req.clean.parsed_text = parsed_text; req.clean.parsed_text = parsed_text;
} }

80
query/search_intersections.js

@ -120,9 +120,7 @@ function generateQuery( clean ){
textParser( clean.parsed_text, vs ); textParser( clean.parsed_text, vs );
} }
var q = getQuery(vs,clean); var q = getQuery(vs, clean);
//console.log(JSON.stringify(q, null, 2));
if (q !== undefined) { if (q !== undefined) {
logger.info(logStr); logger.info(logStr);
@ -135,11 +133,10 @@ function generateQuery( clean ){
} }
function getQuery(vs, clean) { function getQuery(vs, clean) {
logger.info(`[query:search] [search_input_type:${determineQueryType(vs)}]`); logger.info(`[query:search] [search_input_type:${determineQueryType(vs)}]`);
if (hasIntersection(vs)) { if (hasIntersection(vs)) {
return generateQuery(clean); return generateIntersectionQuery(vs,clean);
} }
// returning undefined is a signal to a later step that the addressit-parsed // returning undefined is a signal to a later step that the addressit-parsed
@ -173,30 +170,57 @@ function hasIntersection(vs) {
return vs.isset('input:street1') && vs.isset('input:street2'); return vs.isset('input:street1') && vs.isset('input:street2');
} }
function generateQuery(clean) { function shouldSort(vs) {
if (vs.isset('focus:point:lat') && vs.isset('focus:point:lon')) {
return true;
}
return false;
}
function generateIntersectionQuery(vs,clean) {
let sort = {};
if (shouldSort(vs)) {
sort = {
"_geo_distance": {
"order": "asc",
"distance_type": "plane",
"center_point": {
"lat": clean['focus.point.lat'],
"lon": clean['focus.point.lon']
}
}
}
}
return { return {
type: 'fallback', type: 'fallback',
body: { body: {
'size': 2, 'size': 2,
'query': { 'query': {
'or': [{ 'or': [
'bool' : { {
'must': [ 'bool': {
{ 'match': { 'layer' : 'intersection'} }, 'must': [
{ 'match': { 'address_parts.street1' : clean.parsed_text.street1} }, { 'match': { 'layer': 'intersection'} },
{ 'match': { 'address_parts.street2' : clean.parsed_text.street2} } { 'match': { 'address_parts.street1': clean.parsed_text.street1} },
] { 'match': { 'address_parts.street2': clean.parsed_text.street2} }
} }, { ]
'bool' : { }
'must': [ },
{ 'match': { 'layer' : 'intersection'} }, {
{ 'match': { 'address_parts.street1' : clean.parsed_text.street2} }, 'bool': {
{ 'match': { 'address_parts.street2' : clean.parsed_text.street1} } 'must': [
] { 'match': { 'layer': 'intersection'} },
} } { 'match': { 'address_parts.street1': clean.parsed_text.street2} },
] { 'match': { 'address_parts.street2': clean.parsed_text.street1} }
} ]
} }
}
]
},
'sort': [sort]
}
}; };
} }

Loading…
Cancel
Save