How to use MongoDB Free Text Search

This article will show you how to get started with the MongoDB 2.4 Text Search feature (beta).

What is MongoDB Free Text Search?

MongoDB Free Text Search is currently in beta and was one of the most requested features of MongoDB. Free Text Search supports the search of string content in documents of a collection. With the new text index, and supporting, text command you can search text in data stored in MongoDB, using an index that updates in real-time and is always consistent with the data set. For some applications, the basic text search that has been introducing may be sufficient. Further development with more advanced features will arrive in time.

Starting MongoDB
mongod --dbpath D:\MongoDB\data --logpath D:\MongoDB\log\datamongodb.log --rest 

This assumes you have downloaded and upzipped MonogDB

Create a Database in MongoDB

The database will not show until data has been inserted

use library
Inserting data for Free Text Searching
// insert some books
db.books.save( { _id: 1, title: "NoSQL Distilled", description: "A Brief Guide to the Emerging Polyglot Persistence World."} );

db.books.save( { _id: 2, title: "Patterns of Enterprise Application Architecture", description: "Patterns of Enterprise Application Architecture is written in direct response to the stiff challenges that face enterprise application developers."} );

db.books.save( { _id: 3, title: "JavaScript The Good Parts", description: "Most programming languages contain good and bad parts, but JavaScript has more than its share of the bad, having been developed and released in a hurry before it could be refined. This authoritative book scrapes away these bad features to reveal a subset of JavaScript that's more reliable, readable, and maintainable than the language as a whole-a subset you can use to create truly extensible and efficient code."});

Now check that all records are inserted

db.books.count();

Now get one records

db.books.findOne();

Now get all records

db.books.findOne();
Enabling the beta MongoDB Text Search Feature

You can setup MongoDB Free Text Search using the mongo console

db.adminCommand( { setParameter : 1, textSearchEnabled : true } )

or you can setup MongoDB Free Text Search by added this parameter to the start up command

mongod --setParameter textSearchEnabled=true

This parameter was not in the Start MongoDB section but could be appended.

Setup the Text Index on the collection
db.books.ensureIndex( { description: "text" } );
Perform a Free Text Search in MongoDB
db.books.runCommand( "text", { search : "Polyglot" } );

MongoDB Free Text Search Results
{
        "queryDebugString" : "polyglot||||||",
        "language" : "english",
        "results" : [
                {
                        "score" : 0.5833333333333334,
                        "obj" : {
                                "_id" : 1,
                                "title" : "NoSQL Distilled",
                                "description" : "A Brief Guide to the Emerging World of Polyglot Persistence."
                        }
                }
        ],
        "stats" : {
                "nscanned" : 1,
                "nscannedObjects" : 0,
                "n" : 1,
                "nfound" : 1,
                "timeMicros" : 163
        },
        "ok" : 1
}
Stopping MongoDB
mongod --shutdown 


MongoDB Free Text Search Limitations

Not too bad for a beta version of a new feature, but worth knowing.

  • Text indexes have significant storage requirements and performance costs.
  • A collection can have at most one text index.
  • A search for “Poly” will not return a result for a field containing the word Polyglot.
  • Text indexes uses simple language-specific suffix stemming.
  • Text indexes impact insertion throughput.
  • Phrase queries run much more effectively when the entire collection fits in RAM.


Want to know more about MongoDB Free Text Search?

Further reading about MongoDB Free Text Search.



Comments

No comments yet.

Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Robert

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.