

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 基于分面的索引
<a name="indexing_search_facet"></a>

通过基于分面的索引和搜索，您可以只搜索目录的子集，从而优化目录搜索。为此，您可以使用架构*分面*。例如，您不必在目录中的所有用户对象上进行搜索，而是可以只搜索包含员工分面的用户对象。这可以有效地帮助减少查询延迟时间以及所检索的数据量。

使用基于分面的索引，您可以使用 Cloud Directory 索引 API 操作来创建并将索引附加到对象的分面。您还可以列出索引结果，然后根据特定分面筛选这些结果。这可以将搜索范围缩小到只包含特定类型分面的对象，从而有效地缩短查询时间和数据量。

用于 `“facets”` 和 `[CreateIndex](https://docs.aws.amazon.com/clouddirectory/latest/APIReference/API_CreateIndex.html)` API 调用的 `[ListIndex](https://docs.aws.amazon.com/clouddirectory/latest/APIReference/API_ListIndex.html)` 属性，覆盖应用到对象的分面集合。此属性仅可用于 `CreateIndex` 和 `ListIndex` API 调用。如以下示例代码中所示，架构 ARN 使用目录的区域、所有者账户和目录 ID 来引用 Cloud Directory 架构。此服务提供的架构不显示在列表中。

```
String cloudDirectorySchemaArn = String.format(“arn:aws:clouddirectory:%s:%s:directory/%s/schema/CloudDirectory/1.0", region, ownerAccount, directoryId);
```

例如，以下示例代码创建基于分面的索引，特定于您的 AWS 账户和目录，您可以枚举使用分面 `SalesDepartmentFacet` 创建的所有对象。

**注意**  
请确保在以下所示参数中使用“facets”值。示例代码所示的 “facets” 实例引用了由 Cloud Directory 服务提供和控制的值。您可以将这些用于索引，但只能具有只读访问权限。

```
// Create a facet-based index
String cloudDirectorySchemaArn = String.format(“arn:aws:clouddirectory:%s:%s:directory/%s/schema/CloudDirectory/1.0",
    region, ownerAccount, directoryId);

facetIndexResult = clouddirectoryClient.createIndex(new CreateIndexRequest() 
  .withDirectoryArn(directoryArn) 
  .withOrderedIndexedAttributeList(List(new AttributeKey()     
        .withSchemaArn(cloudDirectorySchemaArn)     
        .withFacetName("facets")     
        .withName("facets"))) 
        .withIsUnique(false) 
        .withParentReference("/") 
        .withLinkName("MyFirstFacetIndex"))
facetIndex = facetIndexResult.getObjectIdentifier()

// Attach objects to the facet-based index
clouddirectoryClient.attachToIndex(new AttachToIndexRequest().withDirectoryArn(directoryArn)
  .withIndexReference(facetIndex).withTargetReference(userObj))

// List all objects
val listResults = clouddirectoryClient.listIndex(new ListIndexRequest()
  .withDirectoryArn(directoryArn)
  .withIndexReference(facetIndex)
  .getIndexAttachments()

// List the index results filtering for a certain facet
val filteredResults = clouddirectoryClient.listIndex(new ListIndexRequest()
  .withDirectoryArn(directoryArn)
  .withIndexReference(facetIndex)
  .withRangesOnIndexedValues(new ObjectAttributeRange()
    .withAttributeKey(new AttributeKey()
      .withFacetName("facets")
      .withName("facets")
      .withSchemaArn(cloudDirectorySchemaArn))
    .withRange(new TypedAttributeValueRange()
      .withStartMode(RangeMode.INCLUSIVE)
      .withStartValue("MySchema/1.0/SalesDepartmentFacet")
      .withEndMode(RangeMode.INCLUSIVE)
      .withEndValue("MySchema/1.0/SalesDepartmentFacet")
    )))
```