定义同义词

在引用某种概念或事物时,组织通常会采用独特的术语或多种引用方式。建议您定义同义词以建立等效术语,从而帮助用户在搜索时找到相关项。

如需定义同义词,您可以使用 _dictionaryEntry 常用架构将项编入索引。

_dictionaryEntry 类型的项可以具有以下属性:

媒体资源 类型 说明 是否必需?
_term string 需要定义的术语。推荐值是不带连字符的字词或不带标点符号的短语。 必需
_synonym string (repeated) 需要包含在与 _term 中定义的字符串匹配的查询中的替代术语。 必需
_onlyApplicableForAttachedSearchApplications boolean 允许您按数据源和搜索应用对同义词进行分组。如需了解详情,请参阅定义特定于数据源的同义词 选填

当用户在查询中添加 _term 属性的值时,有效查询将变为“字词 OR 同义词”。例如,如果使用同义词 "science fiction" 定义字词 "scifi",则包含 "scifi" 一词的查询与包含 "scifi""science fiction." 的项匹配。

同义词只能单向应用。如果查询是针对 "science fiction," 的,则 Cloud Search 不会为查询应用任何同义词。该查询仅匹配包含 "science fiction." 的项,包含 "scifi" 的项被省略。

要使两个术语可以互换,请分别定义每个术语:

术语 同义词
scifi science fiction
science fiction scifi

在查询处理期间,系统会在应用同义词之前移除连字符和其他标点符号。用户查询 "sci-fi"_term "sci fi." 匹配。如需为用户可能用连字符连接的术语创建同义词,请先将 _term 标准化,以使用空格代替连字符。

继续上面的示例,以下用户查询定义将 "sci-fi," "sci fi," "scifi,""science fiction" 视为可互换:

术语 同义词
scifi science fiction, sci fi
sci fi science fiction, scifi
science fiction scifi, sci fi

默认情况下,任何数据源中的同义词都会应用于整个网域。具体而言,同义词可应用于所有搜索应用,而不考虑数据源。如果您需要特定于数据源的同义词,请参阅定义特定于数据源的同义词

使用 Cloud Search SDK 定义全局同义词

您可以使用 Content Connector SDK 定义术语及其同义词。有关构建连接器的说明,请参阅创建内容连接器

以下代码段展示了如何根据 CSV 文件记录构建表示术语和同义词的 RepositoryDoc

DictionaryConnector.java
/**
 * Creates a document for indexing.
 *
 * For this connector sample, the created document is domain public
 *  searchable. The content is a simple text string.
 *
 * @param record The current CSV record to convert
 * @return the fully formed document ready for indexing
 */
private ApiOperation buildDocument(CSVRecord record) {
  // Extract term and synonyms from record
  String term = record.get(0);
  List<String> synonyms = StreamSupport.stream(record.spliterator(), false)
      .skip(1) // Skip term
      .collect(Collectors.toList());

  Multimap<String, Object> structuredData = ArrayListMultimap.create();
  structuredData.put("_term", term);
  structuredData.putAll("_synonym", synonyms);

  if (Configuration.getBoolean("dictionary.attachedToSearchApp", false).get()) {
    structuredData.put("_onlyApplicableForAttachedSearchApplications", true);
  }

  String itemName = String.format("dictionary/%s", term);

  // Using the SDK item builder class to create the item
  Item item =
      IndexingItemBuilder.fromConfiguration(itemName)
          .setItemType(IndexingItemBuilder.ItemType.CONTENT_ITEM)
          .setObjectType("_dictionaryEntry")
          .setValues(structuredData)
          .setAcl(DOMAIN_PUBLIC_ACL)
          .build();

  // Create the fully formed document
  return new RepositoryDoc.Builder()
      .setItem(item)
      .build();
}

定义同义词时,请注意以下事项:

  • 同义词条目必须在网域范围内公开。在上述示例中,这是通过将 ACL 设置为 DOMAIN_PUBLIC_ACL 来实现的。
  • 不应为配置文件定义以下属性,因为它们会覆盖代码中的网域公开设置:
    • defaultAcl.mode=FALLBACK
    • defaultAcl.public=true

定义搜索应用专用同义词

默认情况下,同义词会应用于所有搜索应用中的所有数据源。

但是,假设您的组织有单独的工程团队和销售团队,您希望为每个团队提供不同的搜索体验,包括职位特定的同义词。在这种情况下,您可以使用工程专用数据源和同义词来创建搜索应用,并使用销售专用数据源和同义词创建另一个搜索应用。如需实现此目标,请使用 _onlyApplicableForAttachedSearchApplications=true 将特定数据源中的每个同义词编入索引。此设置可限制同义词,使其仅适用于包含特定数据源的搜索应用。

例如,将以下代码行添加到上一个代码示例中,可确保编入索引的同义词特定于数据源:

structuredData.put("_onlyApplicableForAttachedSearchApplications", true);