Mongodb 简明教程
MongoDB - Text Search
从 2.4 版本开始,MongoDB 开始支持文本索引以搜索字符串内容。 Text Search 使用词干提取技巧在字符串字段中查找指定词语,方法是省略诸如 a, an, the, 这样的词干停用词。目前,MongoDB 支持约 15 种语言。
Starting from version 2.4, MongoDB started supporting text indexes to search inside string content. The Text Search uses stemming techniques to look for specified words in the string fields by dropping stemming stop words like a, an, the, etc. At present, MongoDB supports around 15 languages.
Enabling Text Search
最初,文本搜索作为一个实验功能,但从 2.6 版本开始,该配置默认启用。
Initially, Text Search was an experimental feature but starting from version 2.6, the configuration is enabled by default.
Creating Text Index
考虑以下 posts 集合中的文档,它包含文章文本及其标签——
Consider the following document under posts collection containing the post text and its tags −
> db.posts.insert({
"post_text": "enjoy the mongodb articles on tutorialspoint",
"tags": ["mongodb", "tutorialspoint"]
}
{
"post_text" : "writing tutorials on mongodb",
"tags" : [ "mongodb", "tutorial" ]
})
WriteResult({ "nInserted" : 1 })
我们将在 post_text 字段上创建一个文本索引以便我们可以在文章的文本中进行搜索——
We will create a text index on post_text field so that we can search inside our posts' text −
>db.posts.createIndex({post_text:"text"})
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
Using Text Index
现在我们在 post_text 字段上创建了文本索引,我们将在其文本中搜索所有包含单词 tutorialspoint 的文章。
Now that we have created the text index on post_text field, we will search for all the posts having the word tutorialspoint in their text.
> db.posts.find({$text:{$search:"tutorialspoint"}}).pretty()
{
"_id" : ObjectId("5dd7ce28f1dd4583e7103fe0"),
"post_text" : "enjoy the mongodb articles on tutorialspoint",
"tags" : [
"mongodb",
"tutorialspoint"
]
}
上面的命令返回了以下结果文档,它们在文章文本中包含单词 tutorialspoint ——
The above command returned the following result documents having the word tutorialspoint in their post text −
{
"_id" : ObjectId("53493d14d852429c10000002"),
"post_text" : "enjoy the mongodb articles on tutorialspoint",
"tags" : [ "mongodb", "tutorialspoint" ]
}
Deleting Text Index
要删除现有文本索引,首先使用以下查询查找索引的名称——
To delete an existing text index, first find the name of index using the following query −
>db.posts.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.posts"
},
{
"v" : 2,
"key" : {
"fts" : "text",
"ftsx" : 1
},
"name" : "post_text_text",
"ns" : "mydb.posts",
"weights" : {
"post_text" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 3
}
]
>
在上面的查询中获取索引的名称后,运行以下命令。在这里, post_text_text 是索引的名称。
After getting the name of your index from above query, run the following command. Here, post_text_text is the name of the index.
>db.posts.dropIndex("post_text_text")
{ "nIndexesWas" : 2, "ok" : 1 }