sonlexqt
sonlexqt

Reputation: 6469

MeteorJS - MongoDB - Why is Full Text Search only returns the exact matches?

I'm using MeteorJS in associated with MongoDB to create a full text search feature, what I did is that I follow the steps here: http://meteorpedia.com/read/Fulltext_search, and my search feature is kinda "working" now.

The following are some of my important codes:

The server/zip-index.js file:

Meteor.startup(function () {
    var search_index_name = 'my_search_index';
    // Remove old indexes as you can only have one text index and if you add
    // more fields to your index then you will need to recreate it.
    Zips._dropIndex(search_index_name);

    Zips._ensureIndex({
        city: 'text',
        state: 'text'
    }, {
        name: 'my_search_index'
    });
});

The server/lib/search_zips.js file

var _searchZips = function (searchText) {
    var Future = Npm.require('fibers/future');
    var future = new Future();
    MongoInternals.defaultRemoteCollectionDriver().mongo.db.executeDbCommand({
            text: 'zips',
            search: searchText,
            project: {
                id: 1 // Only return the ids
            }
        }
        , function(error, results) {
            if (results && results.documents[0].ok === 1) {
                var x = results.documents[0].results;
                future.return(x);
            }
            else {
                future.return('');
            }
        });
    return future.wait();
};

The problem now is that: say, I have a document with name = Washington, state = DC.

Then, when I submit with search key = "Washington", it returns all the documents with name = Washington; but when I submit search key = "Washing" only, it returns nothing !

So I have my doubt that MongoDB's full text searching requires the search key to be exactly the same as the document's field value ? Can you guys help me improve my searching function, so that it still use MongoDB's full text searching but it's capable of returning the documents event if I submit the in-complete search keys ?

I've been stucking at this for hours. Hope that you guys can help. Thanks you so much in advanced !

Upvotes: 2

Views: 1014

Answers (1)

Philipp
Philipp

Reputation: 69663

MongoDB full text search works by splitting all strings into individual words (using some stemming based on the language of the index). This means you can only search for complete words and can not perform any fuzzy search.

When you want to search for word fragments, you can search with a regular expression. But keep in mind that regular expressions can not use text indexes (but they can under some conditions make limited use of normal indexes, when the regular expression begins with the begin-of-string (^) token).

For example, the query db.Zips.find({ name: /^Washing/ } will find all documents where name begins with "Washing" and will benefit from an index on { name: 1 }. You can also use db.Zips.find({ name: /DC/ } to find all documents where the name contains "DC" anywhere, but it will not benefit from any indexes and will need to perform a full collection scan.

When you need more advanced text searching capabilities, you should look into pairing MongoDB with a specialized solution like Lucene.

Upvotes: 6

Related Questions