1
restaurant-search/server/db/search.js

135 lines
3.1 KiB
JavaScript
Raw Normal View History

2014-07-28 03:10:55 +00:00
'use strict';
2014-07-28 06:41:14 +00:00
var _ = require('underscore');
var db_keywords = require('./keywords.json');
var db_data = require('./data.json');
2014-07-28 03:10:55 +00:00
2014-07-28 07:48:24 +00:00
2014-07-28 03:10:55 +00:00
function innerProduct(values1, values2) {
var result = 0;
2014-07-28 06:41:14 +00:00
2014-07-28 03:10:55 +00:00
for (var feature in values1) {
result += values1[feature] * (values2[feature] || 0.0);
}
return result;
}
2014-07-28 13:00:01 +00:00
function countData(searchParams, minScore) {
var dataCount = 0;
for (var i = 0, count = db_data.length; i < count; ++i) {
var record = db_data[i];
var score = innerProduct(searchParams, record.rating);
if (score >= minScore) {
++dataCount;
}
}
return dataCount;
}
function findData(searchParams, minScore, maxResults) {
2014-07-28 03:10:55 +00:00
var results = [];
2014-07-28 06:41:14 +00:00
for (var i = 0, count = db_data.length; i < count; ++i) {
var record = db_data[i];
2014-07-28 13:00:01 +00:00
var score = innerProduct(searchParams, record.rating);
2014-07-28 03:10:55 +00:00
if (score >= minScore) {
results.push({
2014-07-28 13:00:01 +00:00
name: record.name,
url: 'http://www.tripadvisor.com' + record.relativeUrl,
2014-07-28 03:10:55 +00:00
score: score
});
}
}
results.sort(function(a, b) {
return b.score - a.score;
});
2014-07-28 13:00:01 +00:00
return results.slice(0, maxResults);
2014-07-28 03:10:55 +00:00
}
function searchStepper(range, steps, callback) {
2014-07-28 06:41:14 +00:00
var stepSize = (range.max - range.min) / steps;
2014-07-28 03:10:55 +00:00
for (var i = 0; i < steps; ++i) {
2014-07-28 06:41:14 +00:00
var stepMax = range.max - stepSize * i;
2014-07-28 03:10:55 +00:00
var stepMin = stepMax - stepSize;
var stepMid = (stepMin + stepMax) / 2;
callback(stepMid);
}
}
2014-07-28 06:41:14 +00:00
function searchProjection(searchParams, minScore, feature, range, steps) {
var testParams = _.clone(searchParams);
2014-07-28 03:10:55 +00:00
var results = [];
searchStepper(range, steps, function(position) {
testParams[feature] = position;
results.push({
2014-07-28 06:51:48 +00:00
sample: position,
2014-07-28 13:00:01 +00:00
count: countData(testParams, minScore)
2014-07-28 03:10:55 +00:00
});
});
return results;
}
2014-07-28 06:41:14 +00:00
function searchBuildHints(searchParams, minScore, feature, range, steps) {
2014-07-28 03:10:55 +00:00
var projection = searchProjection(
2014-07-28 06:41:14 +00:00
searchParams,
2014-07-28 03:10:55 +00:00
minScore,
feature,
range,
steps
);
var hints = [];
_.each(projection, function(result) {
hints.push({
2014-07-28 06:51:48 +00:00
sample: result.sample,
2014-07-28 13:00:01 +00:00
count: result.count
2014-07-28 03:10:55 +00:00
});
});
return hints;
}
module.exports.getKeywords = function() {
2014-07-28 06:41:14 +00:00
return _.keys(db_keywords);
2014-07-28 03:10:55 +00:00
}
2014-07-28 06:41:14 +00:00
module.exports.execQuery = function(query) {
2014-07-28 07:21:08 +00:00
var searchParams = query.searchParams || db_keywords[query.keyword];
2014-07-28 13:00:01 +00:00
var searchResults = findData(searchParams, query.minScore, query.maxResults);
var graphColumns = {};
2014-07-28 06:41:14 +00:00
for (var feature in searchParams) {
2014-07-28 06:51:48 +00:00
var searchHints = searchBuildHints(
2014-07-28 06:41:14 +00:00
searchParams,
query.minScore,
feature,
2014-07-28 06:51:48 +00:00
query.searchRange,
2014-07-28 06:41:14 +00:00
query.hintSteps
);
graphColumns[feature] = {
2014-07-28 06:51:48 +00:00
color: '#607080',
value: searchParams[feature],
hints: searchHints,
steps: query.hintSteps
2014-07-28 06:41:14 +00:00
}
}
return {
2014-07-28 06:51:48 +00:00
columns: graphColumns,
params: searchParams,
items: searchResults
2014-07-28 06:41:14 +00:00
};
2014-07-28 03:10:55 +00:00
}