---
title: "検索結果ウィジェットでfacetsをカスタマイズ"
slug: "customize-facets-in-a-search-results-widget"
abstract: "JS SDKを使って検索結果ウィジェットの動作をオーバーライドし、facetsの初期値を設定する方法Sitecore Search。"
---
# 検索結果ウィジェットでfacetsをカスタマイズ

ファセットコンテンツがある場合、facetsに初期値を追加したり、検索でデフォルトの動作を上書きしたりする必要があるかもしれません。

例えば、以前設定artifact\_typeファセットをファセットIDごとに一致させる場合、ファセットの扱い方を上書きし、文字列を提供して初期値として設定する必要があります。

> !注ファセット検索は、ファセットのSearchResultsStoreSelectedFacetとSearchResultsFacetConfigItemが一致した場合に成功裏に実行されます。

以下の手順は、facetsのカスタマイズ時によく使われる一般的なユースケースに適用できます。これらは互いに独立しており、一度に一つずつ使用できます。

## facetsの扱い方を上書きする

検索でfacetsを使う場合、デフォルトでマッチはfacetIdに基づいています。この手順は、デフォルトの動作を上書きしたいときに使うことができます。

facetsの扱い方を上書きするには:

-   クエリフックuseSearchResults 、configオブジェクトで、次のコードブロックで示すようにfacetsをSearchResultsStoreSelectedFacetオブジェクトとして追加します。
    
    useSearchResults<EntityModel, InitialState>({
     state: {
     sortType: defaultSortType,
     page: defaultPage,
     itemsPerPage: defaultItemsPerPage,
     keyphrase: defaultKeyphrase,
     // Lists the facets and they behave and match with the CEC.
     // All facets default to valueId or match by comparing valueIds.
     // Selected facet value types in the inital state must match this model.
     config: {
     facets: {
     word\_count: { type: 'range' },
     museum: { type: 'valueId' },
     artifact\_type: { type: 'text' }
     },
     },
     })
    
    > !注ファセット検索は、Sitecore Searchユーザーインターフェースの **ファセット** セクションで設定された属性のみを実行します。
    

## facetsの初期数値を追加します

facetsの初期値を追加するには:

-   useSearchResultsクエリフック、stateオブジェクトのselectedFacetsに、次のコードブロックで示すように望むfacetsを追加します。
    
    useSearchResults<EntityModel, InitialState>({
     state: {
     sortType: defaultSortType,
     page: defaultPage,
     itemsPerPage: defaultItemsPerPage,
     keyphrase: defaultKeyphrase,
     // Use selected facets property to set initial values for facets
     // Unless specified otherwise in the config object, a facet matches by facetId.
     // In case of conflict, the config or default behavior is applied.
     selectedFacets: 
     {
     facetId: 'museum',
     facetValueId: 'facetid\_eyJ0eXBlIjoiZXEiLCJuYZXIgV29uZGVyIExhbmUifQ==',
     },
     {
     facetId: 'word\_count', // => should be handled as a range
     min: 200,
     max: 800,
     },
     ,
     }
     })
    
    > !注ファセット検索は、Sitecore Searchインターフェースの **ファセット** セクションで設定された属性のみを実行します。