配置索引分区
重要
numPartitions
选项是作为“预览”功能提供的。
对于索引,当每个文档未嵌套在另一个文档中时,Atlas Search 将每个文档计为一个单独的索引对象。对于嵌入式文档,Atlas Search 会根据嵌套级别的数量将每个嵌入式文档计为额外的索引对象。Atlas Search 会停止复制大于 2,100,000,000 个索引对象的索引更改。
如果您在单独的搜索节点上部署了 Atlas Search,您可以通过将索引对象分区为子索引来增加 Atlas Search 索引对象的数量。默认情况下,Atlas Search 支持每个分片一个分区。每个分区最多支持 2 亿个索引对象。通过使用 numPartitions
选项,您最多可以创建六十四 (64
) 个子索引。
当您为索引配置分区时,Atlas Search 会自动以最佳方式在子索引之间分配索引对象。当您对包含子索引的集合运行查询时,Atlas Search 会将查询分散到所有子索引,并收集搜索结果和元数据,以便对结果进行排序、合并和返回。
我们建议在以下情况下对您的索引进行分区:
您的索引对象已达到总限制的 50%。
您收藏中的文档数量已达到二十亿。
由于 Atlas Search 停止复制,您的索引处于
STALE
状态。
当您配置子索引或修改子索引数量时,Atlas Search 会触发索引的重建。
如果您的集群中有多个子索引,则无法删除所有搜索节点并迁移到 mongod
和 mongot
进程在同一个节点上运行的部署模型。
语法
{ "name": "<index-name>", "analyzer": "<analyzer-for-index>", "searchAnalyzer": "<analyzer-for-query>", "mappings": { "dynamic": <boolean>, "fields": { <field-definition> } }, "numPartitions": <integer>, ... }
支持的值
Atlas Search numPartitions
选项采用以下值:
1
- 创建单一索引,不添加其他子索引。这是默认值。2
- 创建最多两个子索引。4
- 创建最多四个子索引。8
- 创建最多八个子索引。16
- 创建最多十六个子索引。32
- 创建最多三十二个子索引。64
- 创建最多六十四个子索引。
例子
以下索引示例使用 sample_mflix.movies
集合来演示如何为集合中的数据配置多达 4
个子索引。您可以使用 Atlas 用户界面中的 JSON 编辑器或可视化编辑器以及 其他支持的客户端 来创建索引。
➤ 使用选择语言下拉菜单设置本节中示例的客户端。
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 中,进入项目的 Clusters 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。
如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。
如果尚未出现,请单击侧边栏中的 Clusters(集群)。
会显示集群页面。
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 Atlas 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 Atlas 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)