MongoDB 全文检索和标签功能

来源:互联网 发布:淘宝运营提成 编辑:程序博客网 时间:2024/06/10 22:30

简单翻译。

原文:http://www.mongodb.org/display/DOCS/Full+Text+Search+in+Mongo


MongoDB是通过增加一个tags的数组来实现标签功能(tagging)。


obj = {
  name:     "Apollo"     ,      
  text:  "Some text about Apollo moon landings", 
  tags: [ "moon"  ,  "apollo"    "spaceflight" , ]     
}


建立索引:

db.articles.ensureIndex( { tags: 1 } );


搜索:

//查找一个articles中标签为"apollo"的文档,并输出这个文档的name属性。

> print(db.articles.findOne( { tags: "apollo"} ).name);  
Apollo


全文搜索则是把所有的文本分词后放到一个keywords数组中,实质和tag功能一样:


{ title :  " this    is fun" ,           
  _keywords : ["this", "is" , "fun"]
}


和专门的全文搜索引擎比较:

    MongoDB只是内置功能可以实现全文搜索,它并不是一个专门的全文搜索引擎。

    专门的全文搜索引擎提供以下的功能:

        1.分词

        2.排名(rank)查询(MongoDB可以实现,但需要自已写代码)

        3.bulk index building

    尽管bulk index building可以让索引很快地建立,但是这并不能达到实时的效果,MongoDB有一大好处,可以实时,传统的工具很难达到这样的效果。


实际使用的例子:

The Business Insider web site uses MongoDB for its blog search function in production.
Mark Watson's opinions on Java, Ruby, Lisp, AI, and the Semantic Web - A recipe example in Ruby.
Full text search with MongoDB at Flowdock

原创粉丝点击