1
restaurant-search/server/search.js

311 lines
8.6 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
2014-10-03 02:43:56 +00:00
/*
2015-01-05 06:10:10 +00:00
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2014-10-03 02:43:56 +00:00
'use strict';
2014-11-17 06:58:32 +00:00
var _ = require('underscore');
var geolib = require('geolib');
var mysql = require('mysql');
var pool = null;
function innerProduct(values1, values2) {
var result = 0.0;
2014-11-17 08:48:53 +00:00
console.assert(_.keys(values1).length == _.keys(values2).length);
for (var feature in values1) {
2014-11-17 08:48:53 +00:00
result += values1[feature] * values2[feature];
}
return result;
}
function walkMatches(data, features, minScore, callback) {
for (var i = 0, count = data.records.length; i < count; ++i) {
var record = data.records[i];
2014-11-17 08:43:27 +00:00
var score = innerProduct(features, record.features);
if (score >= minScore) {
2014-09-19 00:38:58 +00:00
callback(record, score);
}
}
2014-09-19 00:38:58 +00:00
}
function countRecords(data, features, minScore) {
2014-09-19 00:38:58 +00:00
var count = 0;
walkMatches(data, features, minScore, function(record, score) {
2014-09-19 00:38:58 +00:00
++count;
});
return count;
}
2014-11-17 08:14:41 +00:00
function findRecords(data, features, minScore) {
2014-09-19 00:38:58 +00:00
var results = [];
walkMatches(data, features, minScore, function(record, score) {
2014-09-19 00:38:58 +00:00
results.push({
name: record.name,
url: 'http://www.tripadvisor.com' + record.relativeUrl,
score: score,
distanceToUser: record.distanceToUser / 1000.0,
distanceToStn: record.distanceToStn / 1000.0,
closestStn: record.closestStn,
id: record.id
2014-09-19 00:38:58 +00:00
});
});
results.sort(function(a, b) {
return b.score - a.score;
});
return results;
}
function step(range, steps, callback) {
var stepSize = (range.max - range.min) / steps;
for (var i = 0; i < steps; ++i) {
var stepMax = range.max - stepSize * i;
var stepMin = stepMax - stepSize;
var stepMid = (stepMin + stepMax) / 2;
callback(stepMid);
}
}
function project(data, features, feature, minScore, range, steps) {
var sample = _.clone(features);
var results = [];
step(range, steps, function(position) {
sample[feature] = position;
results.push({
sample: position,
count: countRecords(data, sample, minScore)
});
});
return results;
}
function buildHints(data, features, feature, minScore, range, steps) {
var projection = project(
data,
features,
feature,
minScore,
range,
steps
);
var hints = [];
_.each(projection, function(result) {
hints.push({
sample: result.sample,
count: result.count
});
});
return hints;
}
function loadDb(params) {
2014-10-04 09:35:22 +00:00
pool = mysql.createPool(params);
}
function addKeyword(query, callback) {
2014-11-10 10:45:07 +00:00
if (!/^[a-zA-Z0-9\s\-]+$/.test(query.keyword)) {
2014-11-17 08:48:53 +00:00
callback({
keyword: query.keyword,
success: false
});
2014-11-08 14:23:56 +00:00
return;
}
getKeywords(function(keywords) {
var values = [
query.keyword,
2015-01-05 06:10:10 +00:00
query.features.delicious,
query.features.accomodating,
query.features.affordable,
query.features.atmospheric,
query.features.nearby,
query.features.accessible
2014-11-08 14:23:56 +00:00
];
2015-01-05 06:10:10 +00:00
pool.query('INSERT INTO keywords VALUES(?, ?, ?, ?, ?, ?, ?)', values, function(err) {
2014-11-17 08:43:27 +00:00
callback({
keyword: query.keyword,
success: err === null
});
2014-11-08 14:23:56 +00:00
});
});
}
function removeKeyword(query, callback) {
2014-10-04 09:35:22 +00:00
pool.query('DELETE FROM keywords WHERE name=? AND name NOT IN (SELECT name FROM presets)', [query.keyword], function(err, fields) {
callback({
keyword: query.keyword,
success: err === null && fields.affectedRows > 0
});
});
}
function getKeywords(callback) {
2014-10-04 09:35:22 +00:00
pool.query('SELECT * FROM keywords', function(err, rows) {
if (err) {
throw err;
}
var keywords = {};
for (var i = 0, count = rows.length; i < count; ++i) {
var row = rows[i];
keywords[row.name] = {
2015-01-05 06:10:10 +00:00
delicious: row.delicious,
accomodating: row.accomodating,
affordable: row.affordable,
atmospheric: row.atmospheric,
nearby: row.nearby,
accessible: row.access
};
}
callback(keywords);
});
}
2014-11-17 08:43:27 +00:00
function getRecords(geo, callback) {
2014-10-04 09:35:22 +00:00
pool.query('SELECT * FROM reviews', function(err, rows) {
if (err) {
throw err;
}
var records = _.map(rows, function(row) {
return {
name: row.name,
id: row.id,
relativeUrl: row.url,
closestStn: row.closestStn,
distanceToStn: row.distanceToStn,
geo: {
latitude: row.latitude,
longitude: row.longitude
},
2014-11-17 08:43:27 +00:00
features: {
2015-01-05 06:10:10 +00:00
delicious: row.delicious,
accomodating: row.accomodating,
affordable: row.affordable,
atmospheric: row.atmospheric
},
};
});
2015-01-05 06:36:27 +00:00
computeRecordGeo(records, geo, 1000.0);
callback(records);
});
}
2015-01-05 06:36:27 +00:00
function computeRecordGeo(records, geo, accessDist) {
var distUserMin = Number.MAX_VALUE;
var distUserMax = Number.MIN_VALUE;
2014-11-17 08:43:27 +00:00
2014-11-17 08:14:41 +00:00
_.each(records, function(record) {
2015-01-05 06:36:27 +00:00
record.distanceToUser = 0.0;
if (geo) {
2015-01-05 06:36:27 +00:00
record.distanceToUser = geolib.getDistance(record.geo, geo);
2014-11-17 08:14:41 +00:00
}
2014-11-17 08:43:27 +00:00
2015-01-05 06:36:27 +00:00
distUserMin = Math.min(distUserMin, record.distanceToUser);
distUserMax = Math.max(distUserMax, record.distanceToUser);
2014-11-17 08:43:27 +00:00
});
2015-01-05 06:36:27 +00:00
var distUserRange = distUserMax - distUserMin;
2014-11-17 08:43:27 +00:00
_.each(records, function(record) {
2015-01-05 06:36:27 +00:00
record.features.nearby = -((record.distanceToUser - distUserMin) / distUserRange - 0.5) * 2.0;
record.features.accessible = 1.0 - (record.distanceToStn / accessDist);
2015-01-05 06:36:27 +00:00
record.features.accessible = Math.min(record.features.accessible, 1.0);
record.features.accessible = Math.max(record.features.accessible, -1.0);
2014-11-17 08:14:41 +00:00
});
}
function getData(geo, callback) {
getKeywords(function(keywords) {
2014-11-17 08:43:27 +00:00
getRecords(geo, function(records) {
callback({
keywords: keywords,
records: records
});
});
});
}
2014-11-08 05:33:36 +00:00
function getParameters(callback) {
getKeywords(function(keywords) {
2014-11-17 08:14:41 +00:00
callback({keywords: keywords});
2014-11-08 05:33:36 +00:00
});
}
function execQuery(query, callback) {
2014-11-17 08:14:41 +00:00
getData(query.geo, function(data) {
var searchResults = findRecords(
data,
query.features,
2014-09-26 04:17:43 +00:00
query.minScore
);
var graphColumns = {};
for (var feature in query.features) {
var searchHints = buildHints(
data,
query.features,
feature,
2014-09-26 04:17:43 +00:00
query.minScore,
query.range,
query.hintSteps
);
graphColumns[feature] = {
value: query.features[feature],
hints: searchHints,
steps: query.hintSteps
};
}
callback({
columns: graphColumns,
items: searchResults.slice(0, query.maxResults),
count: searchResults.length
});
});
}
module.exports = {
loadDb: loadDb,
addKeyword: addKeyword,
removeKeyword: removeKeyword,
getParameters: getParameters,
execQuery: execQuery
};