Docs 菜单
Docs 主页
/ /

配置索引分区

对于索引,当每个文档未嵌套在另一个文档中时, 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 会触发索引的重建。

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

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"
}
}'
  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

您可以从 Search & Vector Search 选项或 Data Explorer 转到MongoDB搜索页面。

2
3

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

Search Type

选择 MongoDB 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.

重要提示:

默认下, MongoDB Search索引名为 default。如果保留此名称,则该索引将是任何未在运算符中指定其他 index 选项的MongoDB搜索查询的默认搜索索引。如果您要创建多个索引,我们建议您在所有索引之间保持一致的描述性命名约定。

4
  1. 单击 Refine Your Index(连接)。

  2. 切换 Index Partitions 以启用它。

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

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

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

5

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

6

新创建的索引会出现在 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 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)

后退

同义词映射

在此页面上