对于索引,当每个文档未嵌套在另一个文档中时, MongoDB Search 将其计为单个索引对象。对于嵌入式文档, MongoDB Search 根据嵌套级别数将每个嵌入式文档计为附加索引对象。MongoDB Search 停止复制大于 2、100、000、000索引对象的索引更改。
如果在单独的搜索节点上部署了MongoDB搜索,则可以通过对索引分区来增加MongoDB搜索索引对象的数量。默认下, MongoDB Search 支持每个分片一个分区。每个分区最多支持 2 十亿个索引对象。通过使用 numPartitions 选项,您可以创建最多具有四个 (4) 个分区的索引。这些分区表示单个索引,每个集群或分片最多支持8B 个文档。
为索引配置分区时, MongoDB Search 会自动以最佳方式在分区之间分配索引对象。当您对具有索引分区的集合运行查询时, MongoDB Search 会将查询分散到所有分区,并收集搜索结果和元数据以对结果进行排序、合并和返回。
我们建议在以下情况下对您的索引进行分区:
您的索引对象已达到总限制的 50%。
集合中的文档数量已达到 20 亿。
您的索引最多将包含 80 亿份文档。
您的索引处于
STALE状态,因为MongoDB Search 已停止复制。
当您配置分区或修改分区数量时, MongoDB Search 会触发索引的重建。
如果集群中有多个分区,则无法删除所有搜索节点并迁移到 mongod 和 mongot 进程在同一节点上运行的部署模型。
语法
1 { 2 "numPartitions": <integer> 3 }
支持的值
MongoDB Search numPartitions 选项采用以下值:
1— 创建单个索引,无需额外分区。这是默认值。2— 为最多 40 亿份文档创建最多两个分区。4— 为最多 80 亿个文档创建最多四个分区。
例子
以下索引示例使用 sample_mflix.movies集合演示如何为集合中的数据配置最多 4 个分区。您可以使用Atlas用户界面中的可视化编辑器或 JSON编辑器以及 其他受支持的客户端来创建索引。
➤ 使用 Select your language(选择语言)下拉菜单设置本节中示例的客户端。
curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ --header "Accept: application/json" \ --header "Content-Type: application/json" \ --include \ --request POST "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes" \ --data ' { "collectionName": "movies", "database": "sample_mflix", "name": "partitioned_index", "type": "search", "definition": { "analyzer": "lucene.standard", "mappings": { "dynamic": true, }, "numPartitions": 4, "searchAnalyzer": "lucene.standard" } }'
创建一个名为
indexDef.json的文件,类似于以下内容:{ "collectionName": "movies", "database": "sample_mflix", "definition": { "mappings": { "dynamic": true }, }, "name": "partitioned_index", "numPartitions": 4 } 运行以下命令以创建索引。
atlas deployments search indexes create --file indexDef.json
在Atlas中,转到集群的Search & Vector Search 页面。
您可以从 Search & Vector Search 选项或 Data Explorer 转到MongoDB搜索页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Search & Vector Search。
如果没有集群,则请单击 Create cluster 来创建一个。要了解更多信息,请参阅创建集群。
如果您的项目有多个集群,请从 Select cluster 下拉列表中选择要使用的集群,然后单击 Go to Atlas Search。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Data Explorer。
展开数据库并选择集合。
单击该集合的 Indexes 标签页。
将显示 Atlas Search 页面。
开始您的索引配置。
在页面上进行以下选择,然后单击 Next。
Search Type | 选择 MongoDB Search 索引类型。 |
Index Name and Data Source | 指定以下信息:
|
Configuration Method | For a guided experience, select Visual Editor. To edit the raw index definition, select JSON Editor. |
重要提示:
默认下, MongoDB Search索引名为 default。如果保留此名称,则该索引将是任何未在运算符中指定其他 index 选项的MongoDB搜索查询的默认搜索索引。如果您要创建多个索引,我们建议您在所有索引之间保持一致的描述性命名约定。
db.movies.createSearchIndex( "search-index", { mappings: { dynamic: true }, "numPartitions": 4 } )
using MongoDB.Bson; using MongoDB.Driver; // connect to your Atlas deployment var uri = "<connection-string>"; var client = new MongoClient(uri); var db = client.GetDatabase("sample_mflix"); var collection = db.GetCollection<BsonDocument>("movies"); // define your MongoDB Search index var index = new BsonDocument { { "mappings", new BsonDocument { { "dynamic", true } } }, { "numPartitions", 4 } }; var result = collection.SearchIndexes.CreateOne(index, "partitioned_index"); Console.WriteLine(result);
import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; public class CreateIndex { public static void main(String[] args) { // connect to your Atlas cluster String uri = "<connection-string>"; try (MongoClient mongoClient = MongoClients.create(uri)) { // set namespace MongoDatabase database = mongoClient.getDatabase("sample_mflix"); MongoCollection<Document> collection = database.getCollection("movies"); Document index = new Document() .append("mappings", new Document() .append("dynamic", true) ) .append("numPartitions", 4); collection.createSearchIndex("partitioned_index", index); } } }
import { MongoClient } from "mongodb"; // connect to your Atlas deployment const uri = "<connection-string>"; const client = new MongoClient(uri); async function run() { try { const database = client.db("sample_mflix"); const collection = database.collection("movies"); // define your MongoDB Search index const index = { name: "partitioned_index", definition: { /* search index definition fields */ "mappings": { "dynamic": true }, "numPartitions": 4 } } // run the helper method const result = await collection.createSearchIndex(index); console.log(result); } finally { await client.close(); } } run().catch(console.dir);
from pymongo.mongo_client import MongoClient from pymongo.operations import SearchIndexModel def create_index(): # Connect to your Atlas deployment uri = "<connectionString>" client = MongoClient(uri) # Access your database and collection database = client["sample_mflix"] collection = database["movies"] # Create your index model, then create the search index search_index_model = SearchIndexModel( definition={ "mappings": { "dynamic": True }, "numPartitions": 4 }, name="partitioned_index", ) result = collection.create_search_index(model=search_index_model) print(result)