首页 > 代码库 > Mongodb全文检索
Mongodb全文检索
- //插入测试数据 有列name和description
- > db.stores.insert(
- ... [
- ... { _id: 1, name: "Java Hut", description: "Coffee and cakes" },
- ... { _id: 2, name: "Burger Buns", description: "Gourmet hamburgers" },
- ... { _id: 3, name: "Coffee Shop", description: "Just coffee" },
- ... { _id: 4, name: "Clothes Clothes Clothes", description: "Discount clothing" },
- ... { _id: 5, name: "Java Shopping", description: "Indonesian goods" }
- ... ]
- ... )
- BulkWriteResult({
- "writeErrors" : [ ],
- "writeConcernErrors" : [ ],
- "nInserted" : 5,
- "nUpserted" : 0,
- "nMatched" : 0,
- "nModified" : 0,
- "nRemoved" : 0,
- "upserted" : [ ]
- })
- //在stores上建立所以 包含name列和description都是文本
- > db.stores.createIndex( { name: "text", description: "text" } )
- {
- "createdCollectionAutomatically" : false,
- "numIndexesBefore" : 1,
- "numIndexesAfter" : 2,
- "ok" : 1
- }
- //执行全文检索 会将关键字分词 然后匹配结果还可以 由于数据量小 速度就测不出来了
- > db.stores.find( { $text: { $search: "java coffee shop" } } )
- { "_id" : 3, "name" : "Coffee Shop", "description" : "Just coffee" }
- { "_id" : 1, "name" : "Java Hut", "description" : "Coffee and cakes" }
- { "_id" : 5, "name" : "Java Shopping", "description" : "Indonesian goods" }
优势:实时的全文检索。
不知道性能如何,不支持高亮这种展示,只有在3.2+的版本才支持中文分词。
大致了解下,暂时不会用到,以后用到可以详细看手册:
https://docs.mongodb.com/manual/text-search/
Mongodb全文检索
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。