Servigistics InService 自訂 > 搜尋自訂 > 搜尋自訂 > 搜尋條件組態 > SearchCriteriaFormatter
  
SearchCriteriaFormatter
可以為每個搜尋查詢參數 (SearchQueryParams) 配置 SearchCriteriaFormatter 委派,以便在將條件傳送至 E3C 之前設定輸入格式。如果沒有配置格式器,將會使用預設的 formatter(com.ptc.sc.services.plugins.DefaultKeyWordFormatter)
<Service context="default"
name="com.ptc.sc.services.plugins.SearchInputFormatter">
<Option serviceClass=
"com.ptc.sc.services.plugins.DefaultKeyWordFormatter"
requestor="null" selector= "DEFAULT"/>
<Option serviceClass=
"com.ptc.sc.services.plugins.DefaultKeyWordFormatter"
requestor="null" selector= "keyword"/>
<Option serviceClass=
"com.ptc.sc.services.plugins.MultiValueSearchInputFormatter"
requestor="null" selector= "infotype"/>
<Option serviceClass=
"com.ptc.sc.services.plugins.MultiValueSearchInputFormatter"
requestor="null" selector= "publicationtype"/>
<Option serviceClass=
"com.ptc.sc.services.plugins.SearchTypeSearchInputFormatter"
requestor="null" selector= "searchtype"/>
</Service>
public interface SearchInputFormatter {
/**
* Method to parse and format search input
* <BR><BR><B>Supported API: </B>true
* <BR><BR><B>Extendable: </B>true
*
* @param keyword - String
* @throws Exception
*/
public String formatInput (String keyword) throws Exception;
}
預設的 formatter(com.ptc.sc.services.plugins.DefaultKeyWordFormatter) 不會執行任何格式設定,而是會保留自 UI 接收的輸入。
多值 formatter(com.ptc.sc.services.plugins.MultiValueSearchInputFormatter) 會將以 value1,value2,value3 格式接收的輸入設定為 value1|value2|value3 格式。
搜尋類型 formatter(com.ptc.sc.services.plugins.SearchTypeSearchInputFormatter) 會省略從 UI 接收的 "any" 值,並且不會傳送 "searchType" 至 E3C。接收的其他任何值都將自動傳送。
自訂
可以將新格式器新增與配置為輸入參數,例如在傳送至 E3C 之前設定日期值格式,或是設定從用戶端接收的關鍵字的格式等。
視需要實行新的自訂格式器,以及實行設定輸入值格式所需的邏輯 (如下所示)。下面的格式器可根據一般運算式剖析輸入,並且設定關鍵字輸入的格式。

import wt.util.WTProperties;
public class KeywordInputFormatter implements SearchInputFormatter {
@Override
public String formatInput(String keyword) throws Exception {
// custom parser that uses regular expressions to expand
// search terms (preserve original but include additional terms)
WTProperties wtProperties = WTProperties.getServerProperties();
String searchTerm =
wtProperties.getProperty("com.ptc.sc.search.searchTerm");
String searchTerms<] = searchTerm.split(" ");
// if the keyword has double quotes , don't format the input
if (!keyword.contains("\"") &&
keyword.matches(searchTerms<0])) {
keyword = keyword + " OR " +
keyword.replaceAll(searchTerms<1], searchTerms<2]);
}
return keyword;
}
}