Added text-index searching

This commit is contained in:
Nathan Leniz
2015-03-07 03:06:38 +09:00
parent b46fb576c3
commit be32cd51b8
2 changed files with 46 additions and 10 deletions

View File

@ -4,7 +4,10 @@ var R = require('ramda'),
Comment = require('./../models/Comment'), Comment = require('./../models/Comment'),
User = require('./../models/User'), User = require('./../models/User'),
moment = require('../public/js/lib/moment/moment.js'), moment = require('../public/js/lib/moment/moment.js'),
resources = require('./resources'); resources = require('./resources'),
mongodb = require('mongodb'),
MongoClient = mongodb.MongoClient,
secrets = require('../config/secrets');
exports.hotJSON = function(req, res, next) { exports.hotJSON = function(req, res, next) {
var story = Story.find({}).sort({'rank': -1}); var story = Story.find({}).sort({'rank': -1});
@ -96,15 +99,47 @@ exports.returnIndividualStory = function(req, res, next) {
}; };
exports.getStories = function(req, res, next) { exports.getStories = function(req, res, next) {
debug('this is data', req.body.data.searchValue); MongoClient.connect(secrets.db, function(err, database) {
Story.find({'headline': new RegExp(req.body.data.searchValue, 'i')}, function(err, results) { db = database;
if (err) { debug('this is data', req.body.data.searchValue);
res.status(500); db.collection('stories').find({
} "$text": {
debug('results are', results); "$search": req.body.data.searchValue
}
res.json(results); }, {
}); headline: 1,
timePosted: 1,
link: 1,
description: 1,
rank: 1,
upVotes: 1,
author: 1,
comments: 1,
image: 1,
storyLink: 1,
textScore: {
$meta: "textScore"
}
}, {
sort: {
textScore: {
$meta: "textScore"
}
}
}).toArray(function(err, items) {
debug('items', items);
return res.json(items);
});
});
//Story.find({'headline': new RegExp(req.body.data.searchValue, 'i')}, function (err, results) {
// if (err) {
// res.status(500);
// }
// debug('results are', results);
//
// res.json(results);
//});
//}a);
}; };
exports.upvote = function(req, res, next) { exports.upvote = function(req, res, next) {

View File

@ -42,6 +42,7 @@
"lusca": "^1.0.2", "lusca": "^1.0.2",
"method-override": "^2.3.0", "method-override": "^2.3.0",
"moment": "^2.8.4", "moment": "^2.8.4",
"mongodb": "^1.4.33",
"mongoose": "^3.8.19", "mongoose": "^3.8.19",
"mongoose-text-search": "0.0.2", "mongoose-text-search": "0.0.2",
"morgan": "^1.5.0", "morgan": "^1.5.0",