Mongodb全文检索
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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全文检索的主要内容,如果未能解决你的问题,请参考以下文章
MongoDB——索引类型之全文索引(Text Indexes)