Full text search in MongoDB

use testdb.adminCommand( { setParameter : "*", textSearchEnabled : true } );tc = db.testtc.save( { _id: 1, title: "Olivia Shakespear",text: "Olivia Shakespear (born Olivia Tucker; 17 March 1863 – 3 October 1938) was a British novelist, playwright, and patron of the arts. She wrote six books that are described as \"marriage problem\" novels. Her works sold poorly, sometimes only a few hundred copies. Her last novel, Uncle Hilary, is considered her best. She wrote two plays in collaboration with Florence Farr." } );
tc.save( { _id: 2, title: "Linn-Kristin Riegelhuth Koren", text: "Linn-Kristin Riegelhuth Koren (born 1 August 1984, in Ski) is a Norwegian handballer playing for Larvik HK and the Norwegian national team. She is commonly known as Linka. Outside handball she is a qualified nurse." } );
Then we can create a new index on the title field:tc.ensureIndex( { "title": "text" } );> res = tc.runCommand( "text", { search: "Olivia" } );
{
"queryDebugString" : "olivia||||||",
"language" : "english",
"results" : [
{
"score" : 0.75,
"obj" : {
"_id" : 1,
"title" : "Olivia Shakespear",
"text" : "Olivia Shakespear (born Olivia Tucker; 17 March 1863 – 3 October 1938) was a British novelist, playwright, and patron of the arts. She wrote six books that are described as \"marriage problem\" novels. Her works sold poorly, sometimes only a few hundred copies. Her last novel, Uncle Hilary, is considered her best. She wrote two plays in collaboration with Florence Farr."
}
}
],
"stats" : {
"nscanned" : 1,
"nscannedObjects" : 0,
"n" : 1,
"timeMicros" : 128
},
"ok" : 1
}
tc.dropIndexes()tc.ensureIndex( { "title": "text", "text": "text" } );
res = tc.runCommand( "text", { search: "novelists" } );
{
"queryDebugString" : "novelist||||||",
"language" : "english",
"results" : [
{
"score" : 0.5116279069767442,
"obj" : {
"_id" : 1,
"title" : "Olivia Shakespear",
"text" : "Olivia Shakespear (born Olivia Tucker; 17 March 1863 – 3 October 1938) was a British novelist, playwright, and patron of the arts. She wrote six books that are described as \"marriage problem\" novels. Her works sold poorly, sometimes only a few hundred copies. Her last novel, Uncle Hilary, is considered her best. She wrote two plays in collaboration with Florence Farr."
}
}
],
"stats" : {
"nscanned" : 1,
"nscannedObjects" : 0,
"n" : 1,
"timeMicros" : 90
},
"ok" : 1
}
tc.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "test.test",
"name" : "_id_"
},
{
"v" : 0,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"ns" : "test.test",
"name" : "title_text_text_text",
"weights" : {
"text" : 1,
"title" : 1
},
"default_language" : "english",
"language_override" : "language"
}
]
tc.ensureIndex( { "title": "text", "text": "text" }, {weights: { title: 10 }, default_language: "norwegian" } );

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store