I am trying to search a text field in my collection. This is an example document in my collection:
{
"_id" : ObjectId("51f9c432573906141dbc9996"),
"id" : ObjectId("51f9c432573906141dbc9995"),
"body" : "the",
"rank" : 0,
"num_comm" : 0,
"activity" : 1375323186
}
This is how I am searching...
$mongo = new MongoClient("mongodb://127.0.0.1");
$db = $mongo->requestry;
try
{
$search_results = $db->command(array('text' => 'trending', 'search' => '"the"'));
}
catch (MongoCursorException $e)
{
return array('error' => true, 'msg' => $e->getCode());
}
return array('error' => false, 'results' => $search_results);
And this is the result that I get...
{
error: false,
results: {
queryDebugString: "||||the||",
language: "english",
results: [ ],
stats: {
nscanned: 0,
nscannedObjects: 0,
n: 0,
nfound: 0,
timeMicros: 66
},
ok: 1
}
}
Below are my indexes on the collection...
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "requestry.trending",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"ns" : "requestry.trending",
"name" : "body_text",
"weights" : {
"body" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 1
}
Any ideas on why I get a blank results array every time?
Thanks in advance for any help!
Nathan
You can not search for "the" because it is a stop-word, and stop-words are not indexed. You can find a list of stop-words at https://github.com/mongodb/mongo/blob/master/src/mongo/db/fts/stop_words_english.txt
You can actually see what is being tried to match in the debug string:
queryDebugString: "||||the||"
The first element is empty here, which means no match is done. If you look what happens for '"cat" AND "purple"'
, the debug string is:
queryDebugString: "cat|purpl||||cat|purple||"
The first element(s) are now cat|purpl
- this shows that stemming has also been applied for purple
.
You have nested quotes on your code ('the' string literal):
$search_results = $db->command(array('text' => 'trending', 'search' => '"the"'));
Try not nesting the quotes
$search_results = $db->command(array('text' => 'trending', 'search' => 'the'));