Mongodb全文检索
时间:2021-07-01 10:21:17
帮助过:23人阅读
> 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全文检索
标签:res size 检索 odi doc find mongo div coff