定义同义词

组织通常会采用独特的术语或多种引用方式来指代同一概念。定义同义词可以建立术语等效性,帮助用户在搜索时找到相关项。

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

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

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

当用户在查询中包含 _term 值时,实际查询会变为 "term OR synonyms."例如,如果您将 "scifi" 定义为同义词 "science fiction",则对 "scifi" 的查询会匹配包含这两个术语之一的项。

默认情况下,同义词不是双向的。除非您还将"science fiction"定义为术语,并将"scifi"定义为同义词,否则对"science fiction"的查询只会匹配该确切短语。如需使术语可互换,请分别定义每个术语:

术语 同义词
scifi science fiction
science fiction scifi

在应用同义词之前,查询处理会移除连字符和标点符号。 对"sci-fi"的查询会匹配术语"sci fi"。如需支持带连字符的术语,请将 _term 标准化,使用空格代替连字符。

可互换的示例:

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

默认情况下,同义词适用于整个网域和所有搜索应用。如需限制同义词,请参阅 定义特定于数据源的同义词

使用 SDK 定义全局同义词

您可以使用 Content Connector SDK 定义术语和同义词。如需了解详情,请参阅 创建内容连接器

以下代码段会根据 RepositoryDoc 根据 CSV 记录构建:

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=FALLBACKdefaultAcl.public=true

定义特定于搜索应用的同义词

如需提供特定于团队的同义词(例如,针对工程团队与销售团队),请使用 _onlyApplicableForAttachedSearchApplications=true 为每个同义词编制索引。这样会将同义词限制为包含特定数据源的搜索应用。

示例:

structuredData.put("_onlyApplicableForAttachedSearchApplications", true);