我有一个MongoDB集合db.articles,它有两个键,“.I”和“抽象”.I想在这两个keys.For示例上进行文本搜索,搜索文本是“物理”,并且我希望所有文档中的“标题”或“抽象”包含关键字“物理学”是returned.But --如何创建文本索引以满足我的命令--真的让我感到困惑:
我应该为它们创建两个单独的文本索引,如下所示:
db.articles.ensureIndex({title:"text"})
db.articles.ensureIndex({abstract:"text"})或
我是否应该在一个命令中创建一个索引,并赋予相同的权重:
db.articles.ensureIndex(
{
title: "text",
abstract: "text",
},
{
weights: {
title: 1,
abstract:1,
},
name: "TextIndex"
}
)我已经习惯了find()操作,它的查询粒度是键,也就是说,您应该为文本索引指定要查询on.But的键,这似乎是文档粒度,您不能指示要查询的键,而只能指示文档name.So,如果我想在特定键上进行文本搜索,我能做什么?
发布于 2014-03-11 15:26:13
所以,不需要测试结果,我只记得关键的决定性的限制。您只能在集合(参考在这里)上创建一个文本索引,因此您没有真正的选择。此外,MongoDB一次只能使用一个索引来满足查询(直到在2.6中引入索引交集)。
因此,唯一可行的选择是在这两个字段上创建复合索引,作为一个单一索引:
db.articles.ensureIndex(
{
title: "text",
abstract: "text",
},
{
weights: {
title: 1,
abstract:1,
},
name: "TextIndex"
}
)对于3或4个字段也是如此,您必须首先删除现有的索引,然后创建新的索引并包含3/4/other。
发布于 2018-02-11 08:09:16
我想在这两个keys.For示例上进行文本搜索,搜索文本是“物理”,我希望返回其“标题”或“抽象”包含关键字“物理学”的所有文档。
要执行文本搜索,MongoDB使用文本索引和$text运算符。
MongoDB提供文本索引来支持对字符串内容的文本搜索查询。文本索引可以包括值为字符串或字符串元素数组的任何字段。
若要执行文本搜索查询,必须在集合上有文本索引。集合只能有一个文本搜索索引,但该索引可以覆盖多个字段。
根据MongoDB,BOL db.collection.ensureIndex() Deprecated since version 3.0.0: db.collection.ensureIndex()现在是db.collection.createIndex()的别名。
如果指定字段不存在索引,则db.collection.ensureIndex()将在该字段上创建索引。
注意:使用db.collection.createIndex()而不是db.collection.ensureIndex()创建新的索引。
我应该为它们创建两个单独的文本索引,如下所示:
db.articles.ensureIndex({title:"text"})
db.articles.ensureIndex({abstract:"text"})例如,您可以在mongo中运行以下命令,以便对name和comments字段进行文本搜索:
> db.articles.createIndex( { title: "text", comments: "text" } )
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}为了确保您已经通过db.collection.getIndexes()创建了索引,它返回一个数组,该数组包含识别和描述集合中现有索引的文档列表。必须对集合调用db.collection.getIndexes()。
> db.articles.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.articles"
},
{
"v" : 2,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "title_text_comments_text",
"ns" : "test.articles",
"weights" : {
"comments" : 1,
"title" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 3
}
]
>在这里,为这两个字段创建了索引名"title_text_comments_text“。
因此,不需要在指定的两个字段上再次创建文本索引。
我是否应该在一个命令中创建一个索引,并赋予相同的权重:
db.articles.createIndex(
{
title: "text",
comments: "text"
}
)我的回答是肯定的。
让我们在为文本索引创建多个字段之后(如在第二个场景中提到)。
> db.articles.createIndex(
... {
... title: "text",
... comments: "text"
... }
... )
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 2,
"note" : "all indexes already exist",
"ok" : 1
}
>从db.collection.getIndexes()方法验证
> db.articles.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.articles"
},
{
"v" : 2,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "title_text_comments_text",
"ns" : "test.articles",
"weights" : {
"comments" : 1,
"title" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 3
}
]
>在执行mongo shell中的文本索引之后,我们可以在上面的语句中看到像"note" : "all indexes already exist"这样的简短注释。因此,我们可以说,两个scenario -- text index的创建是相同的。
最重要的注意事项:集合最多可以有
one文本索引。
在上面的示例中,数据库名是test,集合名是articles (因为OP在他们的代码中使用过)。
https://dba.stackexchange.com/questions/60622
复制相似问题