定义同义词

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

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

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

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

如果用户在查询中添加了 _term 值,则有效查询会变为“字词 OR 同义词”。例如,如果您将 "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 定义术语和同义词。如需了解详情,请参阅创建内容连接器

以下代码段展示了如何根据 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=FALLBACKdefaultAcl.public=true

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

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

示例:

structuredData.put("_onlyApplicableForAttachedSearchApplications", true);