├── .gitignore ├── README.md ├── index.js ├── package.json └── views ├── add.html ├── index.html └── search.html /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ftslite 2 | ======= 3 | 4 | An example of using MongoDB Full Text Search from Node.js 5 | 6 | Read the details on the [MongoHQ Blog](http://blog.mongohq.com/full-text-search-with-mongodb-and-node-js/) 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var bodyParser = require('body-parser'); 3 | var mongodb = require('mongodb'), 4 | MongoClient = mongodb.MongoClient; 5 | var assert = require('assert'); 6 | 7 | var app = express(); 8 | app.use(bodyParser.json()); 9 | app.use(bodyParser.urlencoded({ 10 | extended: true 11 | })); 12 | 13 | var db; 14 | 15 | MongoClient.connect(process.env.MONGOHQ_URL, function(err, database) { 16 | db = database; 17 | db.collection("textstore", {}, function(err, coll) { 18 | if (err != null) { 19 | db.createCollection("textstore", function(err, result) { 20 | assert.equal(null, err); 21 | }); 22 | } 23 | db.ensureIndex("textstore", { 24 | document: "text" 25 | }, function(err, indexname) { 26 | assert.equal(null, err); 27 | }); 28 | app.listen(3000); 29 | }); 30 | }); 31 | 32 | 33 | 34 | app.get("/", function(req, res) { 35 | res.sendfile("./views/index.html"); 36 | }); 37 | 38 | app.get("/add", function(req, res) { 39 | res.sendfile('./views/add.html'); 40 | }); 41 | 42 | app.post("/add", function(req, res) { 43 | db.collection('textstore').insert({ 44 | document: req.body.newDocument, 45 | created: new Date() 46 | }, function(err, result) { 47 | if (err == null) { 48 | res.sendfile("./views/add.html"); 49 | } else { 50 | res.send("Error:" + err); 51 | } 52 | }); 53 | }); 54 | 55 | app.get("/search", function(req, res) { 56 | res.sendfile('./views/search.html'); 57 | }); 58 | 59 | app.post("/search", function(req, res) { 60 | db.collection('textstore').find({ 61 | "$text": { 62 | "$search": req.body.query 63 | } 64 | }, { 65 | document: 1, 66 | created: 1, 67 | _id: 1, 68 | textScore: { 69 | $meta: "textScore" 70 | } 71 | }, { 72 | sort: { 73 | textScore: { 74 | $meta: "textScore" 75 | } 76 | } 77 | }).toArray(function(err, items) { 78 | res.send(pagelist(items)); 79 | }) 80 | }); 81 | 82 | function pagelist(items) { 83 | result = "