Can a $text search perform a partial match

Viewed 10984

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.

2 Answers

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);
});
Related