您的位置:首页 > 数据库 > Mongodb

关于mongodb的全文检索

2015-02-09 16:08 260 查看
1.在fulltextserach 需要用到切词,切词和语言有关,所以需要设置语言,目前不支持中文,只支持如下的:
http://blog.csdn.net/terry_water/article/details/43671749
2.在使用前,在mongodb需要用命令行设置text索引

设置单个字段索引:

db.catalog_product.ensureIndex({description: "text"})


设置多个二级字段全文索引:(同时设置权重)

db.catalog_product.ensureIndex({'name.en_name':'text' ,'description.en_description': 'text'}, {weights: {'name.en_name': 10,'description.en_description':5} });
设置语言:

db.de.ensureIndex( {txt: "text"}, {default_language: "german"} )


在这里需要注意的是,一个表只能设置一种语言,如果在一个表中,如果存在多个语言的字段,需要先拆分到几个分表,然后设置text

参考:http://www.oschina.net/translate/mongodb-text-search-tutorial

对于我的需求,需要的设置为:

db.catalog_product.ensureIndex({'name.en_name':'text' ,'description.en_description': 'text'}, {weights: {'name.en_name': 10,'description.en_description':5} },{default_language: "german"});


这种只能对一种语言搜索,因此需要通过脚本,保存到另外一个表:

fulltextsearch_en_catalog_product

fulltextsearch_fr_catalog_product

fulltextsearch_de_catalog_product

fulltextsearch_es_catalog_product

fulltextsearch_it_catalog_product

因此,把_id,name和description保存到对应的表中,对这几个表查询即可。

通过yii2 mongodb模块的方法:

$mongodb = Yii::$app->mongodb;

        $data = $mongodb->getCollection("catalog_product")->fullTextSearch("iphone",[],['_id']);

这个方式执行查询产品即可,通过返回的product_id,然后结果其他进行继续查询。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: