使用PHP的MongoDB全文搜索 [英] MongoDB full text search using PHP
问题描述
似乎,我对如何执行此操作的搜索会带回一年的结果,并且显然不再起作用,或者我做的事情不正确.
It seems that my searching for how to do this brings back results that are a year old and apparently no longer work or I am not doing something correctly.
通过MongoDB命令行,我已经运行了该工具以建立索引:
Through the MongoDB command line, I have run this for indexing:
db.collectiontest.ensureIndex({"$ **":文本"},{名称:"TextIndex"})
db.collectiontest.ensureIndex( { "$**": "text" }, { name: "TextIndex" } )
基于以下示例进行了
http://docs.mongodb. org/manual/tutorial/create-text-index-on-multiple-fields/
执行该错误后,我没有收到错误消息.
I do not receive an error when that is executed.
以下是我尝试过的2个Stackoverflow帖子:
Here are 2 of the Stackoverflow postings I have tried:
最后一篇文章,我尝试了接受的答案,但确保索引除外,因为我通过命令行和发布者的解决方案进行了此操作.两种方法都返回一条消息,显示"Array([errmsg] => no such cmd:text [bad cmd] =>".
The last post, I tried the accepted answer except the ensure index since I did that via command line and the poster's solution as well. Both methods returns a message showing "Array ( [errmsg] => no such cmd: text [bad cmd] =>" .
我正在运行具有最新版本的PHP和MongoDB的Ubuntu Server 12虚拟机.
I'm running a vm of Ubuntu Server 12 with latest release versions of PHP and MongoDB.
谢谢
詹姆斯
推荐答案
如果您使用的是最新版本的mongodb,则正确的语法是
If you are using the latest version of mongodb, the correct syntax is
db.collection.ensureIndex(
{
subject: "text",
content: "text"
}
)
用于为单个集合中的多个文本字段建立索引,并且
for indexing multiple text fields in a single collection, and
db.collection.ensureIndex(
{
content: "text"
}
)
(用于单个字段).尝试修改您的sureIndex()调用以匹配上述模式之一,然后查看是否适合您.
for a single field. Try modifying your ensureIndex() call to match one of the above patterns and see if that works for you.
可以在此处找到正确的查询语法: http://docs. mongodb.org/manual/reference/operator/query/text/
The correct query syntax can be found here: http://docs.mongodb.org/manual/reference/operator/query/text/
db.articles.find( { $text: { $search: "coffee" } } )
这篇关于使用PHP的MongoDB全文搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!