craigmiller160
craigmiller160

Reputation: 6263

Can a $text search perform a partial match

I'm very confused by this behavior. It seems inconsistent and strange, especially since I've read that Mongo isn't supposed to support partial search terms in full text search. I'm using version 3.4.7 of Mongo DB Community Server. I'm doing these tests from the Mongo shell.

So, I have a Mongo DB collection with a text index assigned. I created the index like this:

db.submissions.createIndex({"$**":"text"})

There is a document in this collection that contains these two values:

"Craig"

"Dr. Bob".

My goal is to do a text search for a document that has multiple matching terms in it.

So, here are tests I've run, and their inconsistent output:

SINGLE TERM, COMPLETE

db.submissions.find({"$text":{"$search":"\"Craig\""}})

Result: Gets me the document with this value in it.

SINGLE TERM, PARTIAL

db.submissions.find({"$text":{"$search":"\"Crai\""}})

Result: Returns nothing, because this partial search term doesn't exactly match anything in the document.

MULTIPLE TERMS, COMPLETE

db.submissions.find({"$text":{"$search":"\"Craig\" \"Dr. Bob\""}})

Result: Returns the document with both of these terms in it.

MULTIPLE TERMS, ONE PARTIAL

db.submissions.find({"$text":{"$search":"\"Craig\" \"Dr. Bo\""}})

Result: Returns the document with both terms in it, despite the fact that one term is partial. There is nothing in the document that matches "Dr. Bo"

MULTIPLE TERMS, BOTH PARTIAL

db.submissions.find({"$text":{"$search":"\"Crai\" \"Dr. Bo\""}})

Result: Returns the document with both terms in it, despite the fact that both terms are partial and incomplete. There is nothing in the document that matches either "Crai" or "Dr. Bo".

Question

So, it all boils down to: why? Why is it, when I do a text search with a partial term with only a single value, nothing gets returned. When I do a text search with two partial terms, I get the matching result? It just seems so strange and inconsistent.

Upvotes: 20

Views: 15338

Answers (2)

Maaz Abdullah
Maaz Abdullah

Reputation: 1

You can do partial searching in mongoose database using mongoose external library called mongoose-fuzzy-search where the search text is broken in various anagrams. for more information visit this link

User.fuzzySearch('jo').sort({ age: -1 }).exec(function (err, users) {
      console.error(err);
      console.log(users);
});

Upvotes: 0

glytching
glytching

Reputation: 47895

MongoDB $text searches do not support partial matching. MongoDB allows text search queries on string content with support for case insensitivity, delimiters, stop words and stemming. And the terms in your search string are, by default, OR'ed.

Taking your (very useful :) examples one by one:

SINGLE TERM, PARTIAL

// returns nothing because there is no world word with the value `Crai` in your
// text index and there is no whole word for which `Crai` is a recognised stem
db.submissions.find({"$text":{"$search":"\"Crai\""}})

MULTIPLE TERMS, COMPLETE

// returns the document because it contains all of these words
// note in the text index Dr. Bob is not a single entry since "." is a delimiter
db.submissions.find({"$text":{"$search":"\"Craig\" \"Dr. Bob\""}})

MULTIPLE TERMS, ONE PARTIAL

// returns the document because it contains the whole word "Craig" and it 
// contains the whole word "Dr" 
db.submissions.find({"$text":{"$search":"\"Craig\" \"Dr. Bo\""}})

MULTIPLE TERMS, BOTH PARTIAL

// returns the document because it contains the whole word "Dr"
db.submissions.find({"$text":{"$search":"\"Crai\" \"Dr. Bo\""}})

Bear in mind that the $search string is ...

A string of terms that MongoDB parses and uses to query the text index. MongoDB performs a logical OR search of the terms unless specified as a phrase.

So, if at least one term in your $search string matches then MongoDB matches that document.

To verify this behaviour, if you edit your document changing Dr. Bob to DrBob then the following queries will return no documents:

db.submissions.find({"$text":{"$search":"\"Craig\" \"Dr. Bo\""}})
db.submissions.find({"$text":{"$search":"\"Crai\" \"Dr. Bo\""}})

These now return no matches because Dr is no longer a whole word in your text index because it is not followed by the . delimiter.

Upvotes: 20

Related Questions