1

Distance now being used in search

This commit is contained in:
Alex Yatskov 2014-11-17 17:43:27 +09:00
parent 1d70b8bf77
commit 0106a36ae5

View File

@ -47,7 +47,7 @@ function innerProduct(values1, values2) {
function walkMatches(data, features, minScore, callback) { function walkMatches(data, features, minScore, callback) {
for (var i = 0, count = data.records.length; i < count; ++i) { for (var i = 0, count = data.records.length; i < count; ++i) {
var record = data.records[i]; var record = data.records[i];
var score = innerProduct(features, record.rating); var score = innerProduct(features, record.features);
if (score >= minScore) { if (score >= minScore) {
callback(record, score); callback(record, score);
@ -151,8 +151,11 @@ function addKeyword(query, callback) {
query.features.proximity query.features.proximity
]; ];
pool.query('INSERT INTO keywords VALUES(?, ?, ?, ?, ?)', values, function(err) { pool.query('INSERT INTO keywords VALUES(?, ?, ?, ?, ?, ?)', values, function(err) {
callback({ keyword: query.keyword, success: err === null }); callback({
keyword: query.keyword,
success: err === null
});
}); });
}); });
} }
@ -188,7 +191,7 @@ function getKeywords(callback) {
}); });
} }
function getRecords(callback) { function getRecords(geo, callback) {
pool.query('SELECT * FROM reviews', function(err, rows) { pool.query('SELECT * FROM reviews', function(err, rows) {
if (err) { if (err) {
throw err; throw err;
@ -203,7 +206,7 @@ function getRecords(callback) {
latitude: row.latitude, latitude: row.latitude,
longitude: row.longitude longitude: row.longitude
}, },
rating: { features: {
food: row.food, food: row.food,
service: row.service, service: row.service,
value: row.value, value: row.value,
@ -212,23 +215,35 @@ function getRecords(callback) {
}; };
}); });
computeRecordGeo(records, geo);
callback(records); callback(records);
}); });
} }
function computeRecordGeo(records, geo) { function computeRecordGeo(records, geo) {
var distMin = Number.MAX_VALUE;
var distMax = Number.MIN_VALUE;
_.each(records, function(record) { _.each(records, function(record) {
record.distance = 0.0; record.distance = 0.0;
if (geo !== null) { if (geo !== null) {
record.distance = geolib.getDistance(record.geo, geo) / 1000.0; record.distance = geolib.getDistance(record.geo, geo) / 1000.0;
} }
distMin = Math.min(distMin, record.distance);
distMax = Math.max(distMax, record.distance);
});
var distRange = distMax - distMin;
_.each(records, function(record) {
record.features.proximity = -((record.distance - distMin) / distRange - 0.5) * 2.0;
}); });
} }
function getData(geo, callback) { function getData(geo, callback) {
getKeywords(function(keywords) { getKeywords(function(keywords) {
getRecords(function(records) { getRecords(geo, function(records) {
computeRecordGeo(records, geo);
callback({ callback({
keywords: keywords, keywords: keywords,
records: records records: records