To perform text search, MongoDB uses a text index and the $text
operator.
插入用例数据
> 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" : [ ]
})
文字索引
MongoDB provides text indexes to support text search queries on string content. text
indexes can include any field whose value is a string or an array of string elements.
To perform text search queries, you must have a text
index on your collection. A collection can only have one text search index, but that index can cover multiple fields.
For example you can run the following in a mongo
shell to allow text search over the name
and description
fields:
> db.stores.createIndex( { name: "text", description: "text" } )
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
>
$text运算符
使用$text
查询运算符对具有文本索引的集合执行文本搜索。
$text
将使用空格和大多数标点符号作为分隔符来OR
标记搜索字符串,并对搜索字符串中的所有此类标记执行逻辑运算。
例如,您可以使用以下查询从列表“ coffee”,“ shop”和“ java”中查找包含任何术语的所有商店:
db.stores.find( { $text: { $search: "java coffee shop" } } )
精确匹配
您还可以通过将它们括在双引号中来搜索确切的短语
以下将查找包含“coffee shop”的所有文档:
db.stores.find( { $text: { $search: "\"coffee shop\"" } } )
排除单词
要排除一个单词,可以在前面加上一个“ -”字符。例如,要查找所有包含“ java”或“ shop”但不包含“ coffee”的商店,请使用以下命令:
> db.stores.find( { $text: { $search: "java shop -coffee" } } )
{ "_id" : 5, "name" : "Java Shopping", "description" : "Indonesian goods" }
排序
默认情况下,MongoDB将以未排序的顺序返回结果。但是,文本搜索查询将为每个文档计算相关性分数,以指定文档与查询的匹配程度。
要按相关性得分的顺序对结果进行排序,必须显式投影该字段并对其进行排序:$meta
textScore
field
> db.stores.find(
... { $text: { $search: "java coffee shop" } },
... { score: { $meta: "textScore" } }
... ).sort( { score: { $meta: "textScore" } } )
{ "_id" : 3, "name" : "Coffee Shop", "description" : "Just coffee", "score" : 2.25 }
{ "_id" : 1, "name" : "Java Hut", "description" : "Coffee and cakes", "score" : 1.5 }
{ "_id" : 5, "name" : "Java Shopping", "description" : "Indonesian goods", "score" : 1.5 }
>