本文主要是介绍关于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,然后结果其他进行继续查询。
这篇关于关于mongodb的全文检索的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!