【问题标题】:No Results with Text Search MongoDB PHP文本搜索没有结果 MongoDB PHP
【发布时间】:2013-08-01 02:28:45
【问题描述】:

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

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

关于为什么我每次都得到一个空白结果数组的任何想法?

提前感谢您的帮助!

内森

【问题讨论】:

    标签: php mongodb


    【解决方案1】:

    您不能搜索“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。

    【讨论】:

      【解决方案2】:

      您的代码中有嵌套引号('the' 字符串文字):

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

      尽量不要嵌套引号

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

      【讨论】:

      • 我相信 '"the"' 是有效的 FTS 语法
      • 语法没问题。如果你在查看 PHP 手册,那里的语法是错误的。
      猜你喜欢
      • 1970-01-01
      • 2012-06-13
      • 2020-05-04
      • 1970-01-01
      • 2010-10-28
      • 2018-11-06
      • 2017-04-02
      • 2011-08-18
      相关资源
      最近更新 更多