From be32cd51b833219c566ed17343d6e30f3ede35de Mon Sep 17 00:00:00 2001 From: Nathan Leniz Date: Sat, 7 Mar 2015 03:06:38 +0900 Subject: [PATCH] Added text-index searching --- controllers/story.js | 55 ++++++++++++++++++++++++++++++++++++-------- package.json | 1 + 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/controllers/story.js b/controllers/story.js index 4c4fad5dbb..394ece6300 100644 --- a/controllers/story.js +++ b/controllers/story.js @@ -4,7 +4,10 @@ var R = require('ramda'), Comment = require('./../models/Comment'), User = require('./../models/User'), 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) { var story = Story.find({}).sort({'rank': -1}); @@ -96,15 +99,47 @@ exports.returnIndividualStory = function(req, res, next) { }; exports.getStories = function(req, res, next) { - debug('this is data', req.body.data.searchValue); - 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); - }); + MongoClient.connect(secrets.db, function(err, database) { + db = database; + debug('this is data', req.body.data.searchValue); + db.collection('stories').find({ + "$text": { + "$search": req.body.data.searchValue + } + }, { + 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) { diff --git a/package.json b/package.json index 94bd29bcd1..5ef6868d00 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "lusca": "^1.0.2", "method-override": "^2.3.0", "moment": "^2.8.4", + "mongodb": "^1.4.33", "mongoose": "^3.8.19", "mongoose-text-search": "0.0.2", "morgan": "^1.5.0",