Reputation: 109
I have a document named "posts", it is like this :
{
"_id" : ObjectId("5afc22290c06a67f081fa463"),
"title" : "Cool",
"description" : "this is amazing"
}
And I have putted index on title and description :
db.posts.createIndex( { title: "text", description: "text" } )
The problem is when I search and type for example "amaz" it return the data with "this is amazing" above, while it should return data only when I type "amazing"
db.posts.find({ $text: { $search: 'amaz' } }, (err, results) => {
return res.json(results);
});
Upvotes: 2
Views: 105
Reputation: 2505
Credit to @amenadiel for the original data here:
https://stackoverflow.com/a/24316510/7948962
From the MongoDB docs:
https://docs.mongodb.com/manual/core/index-text/
Index Entries
text index tokenizes and stems the terms in the indexed fields for the index entries. text index stores one index entry for each unique stemmed term in each indexed field for each document in the collection. The index uses simple language-specific suffix stemming.
This is to allow you to search partial "stem" terms in the index, and have the database return all related results. In your specific scenario, amaz
is a bit of an odd token as it is a bit irregular compared to other words such as talking
, which is tokenized to the word talk
, or talked
to talk
. Similarly walking
and walked
to walk
.
In your case, the word amazing
in your text will be tokenized as amaz
. If your column contained data such as amazed
, it would receive the same amaz
token as well. And those results would also be returned from a search of amaz
.
Upvotes: 2