I have a VPS with MongoDB 4.4.x and PHP7.x and use the MongoDB PHP library to connect with MongoDB.
My documents (approx. 10 million) look like:
collection: tenants, fulltext index on answers.answer
"survey_id": {
"$numberLong": "424221"
},
"account_id": 20014,
"feedback_id": 738857,
"date_reg": "2021-04-22",
"time_reg": "21:15",
"source": 2,
"trigg": 1,
"read_status": 0,
.... etc...,
"answers": {
"0": {
"answer": "Feedback answer 1 lorem ipsum gmail doramet",
"type": 1,
"qid": 4241,
"datatype": 24,
"additional": "",
"q": "What do you think about..."
},
"1": {
"answer": "Feedback answer steve lorem ipsum",
"type": 2,
"qid": 21,
"datatype": 2,
"additional": "",
"q": "What was your experience with..."
},
... etc...
Then I search like this:
$manager = new
MongoDB\Driver\Manager("mongodb://user:pass@localhost:27017");
$filter = ['$text' => ['$search' => "steve"]];
$options = ['limit' => 10];
$query = new MongoDB\Driver\Query($filter,$options);
$cursor = $manager->executeQuery('db.tenants', $query);
foreach($cursor as $row){
echo '<pre>';
print_r($row);
echo '</pre><br><br>';
$i++;
}
This doesn't return the answer with "steve" in it.
However, if I search for "gmail" that does work. So why not for "steve"?
Both are no stop words. What am I doing wrong?
My Answer:
The issue you are facing might be related to the way MongoDB handles text search. By default, MongoDB text search uses a language-specific stop words list and stemming algorithm to tokenize and search text.
In your case, the word "steve" might not be matching because it could be stemming to a different form that is not an exact match. You can try using the $language option in your search query to specify the language as "none" to disable language-specific stemming and stop words:
php
$filter = ['$text' => ['$search' => 'steve', '$language' => 'none']];
This should allow you to search for the exact word "steve" without any stemming or stop words interference.
Additionally, make sure that your text index is created with the default language set to "none" to ensure consistent behavior:
javascript
db.tenants.createIndex({ 'answers.answer': 'text' }, { default_language: 'none' });
By setting the language to "none" in both the index creation and search query, you should be able to search for specific words like "steve" successfully.
Rate this post
5 of 5 based on 9386 votesComments