Docs 菜单

配置索引分区

重要

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 会触发索引的重建。

如果您的集群中有多个子索引,则无法删除所有搜索节点并迁移到 mongodmongot 进程在同一个节点上运行的部署模型。

{
"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"
}
}'
  1. 创建一个名为 indexDef.json 的文件,类似于以下内容:

    {
    "collectionName": "movies",
    "database": "sample_mflix",
    "definition": {
    "mappings": {
    "dynamic": true
    },
    },
    "name": "partitioned_index",
    "numPartitions": 4
    }
  2. 运行以下命令以创建索引。

    atlas deployments search indexes create --file indexDef.json
1
  1. 如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。

  2. 如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。

  3. 如果尚未出现,请单击侧边栏中的 Clusters(集群)。

    会显示集群页面。

2

您可以从侧边栏、 Data Explorer 或集群详细信息页面转到 Atlas Search 页面。

  1. 在侧边栏中,单击 Services 标题下的 Atlas Search

    注意

    如果您没有集群,请单击 Create cluster 来创建一个。如需了解详情,请参阅 创建集群。

  2. Select data source 下拉菜单中选择您的集群并单击 Go to Atlas Search

    将显示 Atlas Search 页面。

  1. 单击集群的对应 Browse Collections 按钮。

  2. 展开数据库并选择集合。

  3. 单击该集合的 Search Indexes 标签页。

    将显示 Atlas Search 页面。

  1. 单击集群的名称。

  2. 单击 Atlas Search 标签页。

    将显示 Atlas Search 页面。

3
4

在页面上进行以下选择,然后单击 Next

Search Type

选择 Atlas Search 索引类型。

Index Name and Data Source

指定以下信息:

  • Index Name: partitioned_index

  • Database and Collection:

    • sample_mflix database

    • movies 集合

Configuration Method

For a guided experience, select Visual Editor.

To edit the raw index definition, select JSON Editor.
5
  1. 单击 Refine Your Index(连接)。

  2. 切换 Index Partitions 以启用它。

  3. Number of partitions下拉列表中选择4,然后单击Save Changes

  1. 将默认索引定义替换为以下内容:

    {
    "mappings": {
    "dynamic": true
    },
    "numPartitions": 4
    }
  2. 单击 Next(连接)。

6

Atlas会显示 Toast(简短的非交互式通知),让您知道索引正在构建中。

7

新创建的索引会出现在 Atlas Search 标签页上。在构建索引期间,Status 字段显示为 Build in Progress。索引构建完成后,Status 字段将显示为Active

注意

较大的集合需要较长的索引时间。索引构建完成后,您将收到电子邮件通知。

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)