Full text search in mongodb

Reading Time : ~ .

Full text search is a custom implementation created by the MongoDB developers as a specific index type

It has features such as:

Full text search as an index type when creating new indexes, just like any other.
         1.Indexing of multiple fields, with weighting to give different fields higher priority.

         2.Support for Latin based languages initially, with plans for other character sets later. Initially this will be: Danish, Dutch,

           English, Finnish, French, German, Hungarian, Italian, Norwegian, Portuguese, Romanian, Russian, Spanish, Swedish.

         3.Support for advanced queries, similar to the Google search syntax e.g. negation and phrase matching.

         4.Stemming, to deal with plurals.

         5.Stop words (see the list here).

This looks like a good, general purpose full text search engine which goes along well with how MongoDB is developing into a good multi-purpose database. It may well never reach the complexity of “proper” search products like Elastic Search or Solr, but that is probably not the goal.

use test

db.adminCommand( { setParameter : "*", textSearchEnabled : true } );

tc = db.test

tc.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
}

In mongodb,we have 1 full text index.We can create compound index as 

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
}

 

We can see the indexes using getIndexes() method

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"
    }
]

You can specify the weight and default_language options when creating the index e.g.

tc.ensureIndex( { "title": "text", "text": "text" }, {weights: { title: 10 }, default_language: "norwegian" } );
    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
group() vs aggregation framework vs MapReduce in mongodb Ashwin Kumar

The group() command, Aggregation Framework and MapReduce are collectively aggregation features of MongoDB. group(): Group Performs simple aggregation operations on a collection documents. Group is ...

Continue Reading...
MongoDB CRUD operations with Python (Pymongo) Rakesh babu Podishetty

MongoDB with Python - Connection establishment, Create, Update, Retrieve and Delete operations explained with sample code.

Continue Reading...
Advanced Querying in MongoDB Ramya Ambati

Advanced Queries of MongoDB: Inserting records to the database and retrieving data from database.
1. Wrapped Queries: Like, sort, limit, count.
2. Query Using Modifiers: ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties