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))) {
parsed_text.country = iso3166.to3(_.toUpper(parsed_text.country));
}
req.clean.parsed_text = parsed_text;
}

80
query/search_intersections.js

@ -120,9 +120,7 @@ function generateQuery( clean ){
textParser( clean.parsed_text, vs );
}
var q = getQuery(vs,clean);
//console.log(JSON.stringify(q, null, 2));
var q = getQuery(vs, clean);
if (q !== undefined) {
logger.info(logStr);
@ -135,11 +133,10 @@ function generateQuery( clean ){
}
function getQuery(vs, clean) {
logger.info(`[query:search] [search_input_type:${determineQueryType(vs)}]`);
if (hasIntersection(vs)) {
return generateQuery(clean);
return generateIntersectionQuery(vs,clean);
}
// 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');
}
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 {
type: 'fallback',
body: {
'size': 2,
'query': {
'or': [{
'bool' : {
'must': [
{ 'match': { 'layer' : 'intersection'} },
{ '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} },
{ 'match': { 'address_parts.street2' : clean.parsed_text.street1} }
]
} }
]
}
}
type: 'fallback',
body: {
'size': 2,
'query': {
'or': [
{
'bool': {
'must': [
{ 'match': { 'layer': 'intersection'} },
{ '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} },
{ 'match': { 'address_parts.street2': clean.parsed_text.street1} }
]
}
}
]
},
'sort': [sort]
}
};
}

Loading…
Cancel
Save