文本搜索MongoDB PHP没有结果


No Results with Text Search MongoDB PHP

我正在尝试搜索集合中的文本字段。这是我收藏的一个示例文档:

{
    "_id" : ObjectId("51f9c432573906141dbc9996"),
    "id" : ObjectId("51f9c432573906141dbc9995"),
    "body" : "the",
    "rank" : 0,
    "num_comm" : 0,
    "activity" : 1375323186
}

这就是我搜索的方式。。。

$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);

这就是我得到的结果。。。

{
    error: false,
    results: {
        queryDebugString: "||||the||",
        language: "english",
        results: [ ],
        stats: {
            nscanned: 0,
            nscannedObjects: 0,
            n: 0,
            nfound: 0,
            timeMicros: 66
        },
        ok: 1
    }
}

以下是我对收藏的索引。。。

{
    "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
}

为什么我每次都得到一个空白的结果数组,有什么想法吗?

提前感谢您的帮助!

Nathan

您不能搜索"the",因为它是一个停止词,并且停止词没有索引。你可以在上找到停止语列表https://github.com/mongodb/mongo/blob/master/src/mongo/db/fts/stop_words_english.txt

实际上,您可以在调试字符串中看到试图匹配的内容:

queryDebugString: "||||the||"

第一个元素在这里是空的,这意味着没有匹配。如果你看看'"cat" AND "purple"'发生了什么,调试字符串是:

queryDebugString: "cat|purpl||||cat|purple||"

第一个元素现在是cat|purpl,这表明词干也应用于purple

您在代码上嵌套了引号("字符串文字):

$search_results = $db->command(array('text' => 'trending', 'search' => '"the"'));

尽量不要嵌套报价

$search_results = $db->command(array('text' => 'trending', 'search' => 'the'));