关于mongodb的全文检索

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,然后结果其他进行继续查询。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值