From 1d70b8bf7704961646e3d9b33cb2cd79315bc5db Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 17 Nov 2014 17:14:41 +0900 Subject: [PATCH] Adding geo preprocessing step --- server/search.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/server/search.js b/server/search.js index cbf960e..94eb264 100644 --- a/server/search.js +++ b/server/search.js @@ -64,19 +64,14 @@ function countRecords(data, features, minScore) { return count; } -function findRecords(data, geo, features, minScore) { +function findRecords(data, features, minScore) { var results = []; walkMatches(data, features, minScore, function(record, score) { - var distance = 0.0; - if (geo !== null) { - distance = geolib.getDistance(record.geo, geo) / 1000.0; - } - results.push({ name: record.name, url: 'http://www.tripadvisor.com' + record.relativeUrl, score: score, - distance: distance, + distance: record.distance, id: record.id }); }); @@ -221,9 +216,19 @@ function getRecords(callback) { }); } -function getData(callback) { +function computeRecordGeo(records, geo) { + _.each(records, function(record) { + record.distance = 0.0; + if (geo !== null) { + record.distance = geolib.getDistance(record.geo, geo) / 1000.0; + } + }); +} + +function getData(geo, callback) { getKeywords(function(keywords) { getRecords(function(records) { + computeRecordGeo(records, geo); callback({ keywords: keywords, records: records @@ -234,15 +239,14 @@ function getData(callback) { function getParameters(callback) { getKeywords(function(keywords) { - callback({ keywords: keywords }); + callback({keywords: keywords}); }); } function execQuery(query, callback) { - getData(function(data) { + getData(query.geo, function(data) { var searchResults = findRecords( data, - query.geo, query.features, query.minScore );