定義同義詞

機構通常有專屬的術語,或以多種方式指稱同一概念。定義同義字可建立字詞等價關係,協助使用者在搜尋時找到項目。

如要定義同義詞,請使用 _dictionaryEntry 知名結構定義為項目建立索引。

_dictionaryEntry 類型的項目可具有下列屬性:

屬性 類型 說明 是否必要?
_term string 要定義的字詞。建議使用不含連字號的字詞或句子,且不含標點符號。 必填
_synonym string (repeated) 查詢中要納入的替代字詞,必須與 _term 中定義的字串相符。 必填
_onlyApplicableForAttachedSearchApplications boolean 可依資料來源和搜尋應用程式將同義字分組。詳情請參閱「定義資料來源專屬的同義字」。 選用

如果使用者在查詢中加入 _term 值,有效查詢就會變成「term OR synonyms」(字詞或同義字)。舉例來說,如果使用同義字 "science fiction" 定義 "scifi",則對 "scifi" 的查詢會比對包含任一字詞的項目。

同義詞預設為單向。查詢 "science fiction" 時,系統只會比對完全相符的詞組,除非您也將其定義為同義字詞 "scifi"。如要讓字詞可互換,請分別定義每個字詞:

字詞 同義詞
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);