使用PHP进行MongoDB全文搜索

时间:2014-05-09 19:13:31

标签: php mongodb nosql

似乎我在寻找如何做到这一点会带来一年前的结果,显然已经不再有效了,或者我没有做正确的事情。

通过MongoDB命令行,我已将其运行以进行索引:

db.collectiontest.ensureIndex({" $ **":" text"},{name:" TextIndex"})

这是基于

的例子吗?

http://docs.mongodb.org/manual/tutorial/create-text-index-on-multiple-fields/

执行该操作时,我不会收到错误。

以下是我尝试过的2个Stackoverflow帖子:

How to search in fulltext index using php in mongodb

MongoDB Collection runCommand from PHP

最后一篇文章,我尝试了接受的答案,除了确保索引,因为我通过命令行和海报的解决方案也做到了。这两种方法都会返回一条消息,显示"数组([errmsg] =>没有这样的cmd:text [bad cmd] =>"。

我正在使用最新版本的PHP和MongoDB运行vm的Ubuntu Server 12。

谢谢,

詹姆斯

1 个答案:

答案 0 :(得分:0)

如果您使用的是最新版本的mongodb,则正确的语法是

db.collection.ensureIndex(
                           {
                             subject: "text",
                             content: "text"
                           }
                         )

用于索引单个集合中的多个文本字段,以及

 db.collection.ensureIndex(
                               {
                                 content: "text"
                               }
                             )

表示单个字段。尝试修改您的ensureIndex()调用以匹配上述模式之一,看看它是否适合您。

可以在此处找到正确的查询语法:http://docs.mongodb.org/manual/reference/operator/query/text/

db.articles.find( { $text: { $search: "coffee" } } )