Wednesday, April 6, 2016

Full Text Search on older version of MongoDB

This will be for MongoDB 2.4 version

Steps

use admin
db.runCommand( { setParameter: 1, textSearchEnabled: true } )

db.articles.insert(
   [
     { _id: 1, subject: "coffee", author: "xyz", views: 50 },
     { _id: 2, subject: "Coffee Shopping", author: "efg", views: 5 },
     { _id: 3, subject: "Baking a cake", author: "abc", views: 90  },
     { _id: 4, subject: "baking", author: "xyz", views: 100 },
     { _id: 5, subject: "Café Con Leche", author: "abc", views: 200 },
     { _id: 6, subject: "Сырники", author: "jkl", views: 80 },
     { _id: 7, subject: "coffee and cream", author: "efg", views: 10 },
     { _id: 8, subject: "Cafe con Leche", author: "xyz", views: 10 }
   ]
)

db.articles.createIndex( { subject: "text" } )


db.subject.runCommand( "text", { search: "coffee cream" } ).results  - coffee OR cream

db.subject.runCommand( "text", { search: "\"coffee cream\"" } ).results  - coffee AND cream


db.subject.runCommand( "text", { search: "coffee " } ).results


reference : https://docs.mongodb.org/v2.4/tutorial/search-for-text/

MongoDbB 3.2 and above
https://docs.mongodb.org/manual/reference/operator/query/text/