1. 設定

設定

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

検索 API は、実行するすべての検索に既定の検索構成を適用します。たとえば、読み込むプロパティに対して有意な既定や、既定のフィルターを設定できます。

フィルターは、構成 (設定を使用) またはクエリ コントリビューターのフレームワークを使用したコードによって定義できます。

設定を使用した既定の検索構成

ここで取り上げる既定の検索構成は、M の設定から取得したもので、「SearchDefaults」と呼ばれます。このセクションでは、この設定を詳しく見ていきます。 設定の名前は可変です。 パラメーター(「name」)として検索関連の API リソースに渡されます。 UI コンポーネントが REST 呼び出しを実行すると、設定内の設定値が検索され、検索コントローラーに渡されます。

Sitecore Content Hub の優れた例となるのが、SearchConfiguration.json です。 設定のカテゴリは「PortalConfiguration」です。

検索設定のスキーマには、次のプロパティがあります。

  • Name: 設定の名前
  • Take: 既定で返される結果の数。 この値は、検索ページにページングがない場合にのみ使用されます。
  • Filters: 各クエリに追加するフィルター。 フィルターは AND で組み合わせます (「フィルター」セクションを参照)。
  • Facets: ファセットとして使用される定義。 ファセットと結果エンティティのリンク方法は、スキーマによって決定します (タクソノミー リレーションの使用など) (「ファセット」セクションを参照)。
  • Followups: フォローアップ ファセットの設定。 ファセット フィルターが特定の定義に適用される場合、検索クエリでは他のファセットも追加されます。
  • PropertiesToLoad: 結果に含まれるプロパティ。
  • relationsToLoad: 結果に含まれるリレーション。
  • renditionsToLoad: レンディションを持つエンティティの場合は、結果に読み込むレンディション名。
  • ResultTypes: 結果として返されるエンティティの型。 検索クエリには、これらの型のエンティティのみが返されるようにフィルターが追加されます。
  • SuperFacets: 設定で特定の定義がスーパーファセットとしてフラグ付けされている場合、検索の応答には、選択したスーパーファセットに関する追加情報が含まれます。
  • Sorting: 既定の並べ替え順序

{
    "name": "SearchConfiguration",
    "take": 10,
    "filters": [
        {
            "name": "relations.FinalLifeCycleStatusToAsset.parents",
            "operator": "Equals",
            "valueExpression": "String(\"StatusValue\") == \"Approved\""
        }
    ],
    "facets": [
        {
            "name": "M.Campaign"
        },        
        {
            "name": "M.AssetOriginator",
            "limit": 3,
            "sorting": "Alphabetical",
            "direction": "Ascending"
        }
    ],
    "followups": [        
        {
            "name": "M.AssetType",
            "valueExpression": "string(\"ClassificationName\") == \"Video\"",
            "followupName": "Ms.BitRate"
        }
    ],
    "superfacets": [
        {
            "definition": "M.Brand"
        }
    ],
    "propertiesToLoad": [
        "FileName",
        "Renditions"
    ],
    "resultTypes": [
        "M.Asset"
    ],
    "sorting": [
        {
            "property": "ModifiedOn",
            "direction": "Descending"
        }
    ]
}

ファセット

ファセット定義の最もシンプルな例は、定義名を使用する方法です。ファセットは Sitecore Content Hub のスキーマに完全に依存する点に注意してください (例: IsTaxonomyItemDefinition としてマークされている定義)。

次の 2 種類のファセットがサポートされています。

  • 通常のファセット (リレーション)
  • フィールド ファセット (プロパティ)

通常のファセット

{
    "name": "M.Campaign"
}

フィールド ファセット

{
    "name": "FileExtension",
    "type": 1
}

各ファセットは、オプションの limit パラメーターを使用することで、返されるファセット値の数を制限できます。 返されるファセット値の数は、既定で 10 です。 limit パラメーターを 0 に設定すると、すべてのファセット値が返されます。

オプションの sorting および direction パラメーターを使用すると、現在のファセットの並べ替えオプションを設定できます。

ファセットの並べ替えに使用できるオプションは次のとおりです。

  • Count (既定)
  • Alphabetical (アルファベット順)
  • フィールド

Alphabetical (アルファベット順) の並べ替えが有意に機能するのは、すべてのファセット値が返される場合のみである点に注意してください。

Json の構成設定を使用した既定のフィルター

フィルターは、構成 (この設定を使用) またはクエリ コントリビューターのフレームワークを使用したコードによって定義できます。

設定の「filters」セクションでは、AND フィルターのみが許可されます。 フィルターで使用される各値の式は、単一の ID に解決される必要があります。

フィルター式を解決できない場合は、例外がスローされます。

例:

{
    "name": "relations.FinalLifeCycleStatusToAsset.parents",
    "operator": "Equals",
    "valueExpression": "String(\"StatusValue\") == \"Approved\""
}

上記の例では、LifeCycleStatus が Approved の ID に対してフィルターが適用されます (単一 ID)。

値の式が使用されていない場合、名前は定義名であると想定され、定義フィルターが適用されます。

{
    "name": "M.Asset"
}

上記の例では、definition_name == "M.Asset" に対してフィルターが適用されます。

フィルターのフィルターを作成することもできます。 この場合、内部フィルターに適用する演算子を選択することもできます。

次に例を示します。

{
    "operator": "OR",
    "filters": [
        {
            "name": "relations.FinalLifeCycleStatusToAsset.parents",
            "operator": "Equals",
            "valueExpression": "String(\"StatusValue\") == \"Created\""
        },
        {
            "name": "relations.FinalLifeCycleStatusToAsset.parents",
            "operator": "Equals",
            "valueExpression": "String(\"StatusValue\") == \"Rejected\""
        }
    ]
}

Default Filters via Code

Another option to append filters is via code.

The example below applies an OR filter on multiple definition names and a specific one for M.Asset (lifecycle status approved).

The filter looks like this:

(definition = "M.Sku" OR definition = "M.Product" OR definition = "M.Campaign" OR definition = "M.Recipe" OR (definition = M.Asset AND LifeCycleStatus = Approved))

    public class SearchConfigurationQueryContributor : IQueryContributor
    {
        private readonly IQuerying _querying;

        public SearchConfigurationQueryContributor(IQuerying querying)
        {
            this._querying = querying;
        }

        public Base.Search.Filters.FilterBase AddQuery(QueryContext context)
        {
            CompositeFilter filter = new CompositeFilter(FilterCombineMethod.Or);

            var qb = new QueryBuilder();

            //First step is to resolve final lifecycle status = Approved. (we are only gonna show approved stuff in the search.)
            long? approvedId = this._querying.Basic.Entities.SingleId(qb.Definition.Name == Stylelabs.M.Content.Constants.FinalLifeCycleStatusDefinition.DefinitionName &&
                qb.String(Stylelabs.M.Content.Constants.FinalLifeCycleStatusDefinition.StatusValue) == Stylelabs.M.Content.Constants.FinalLifeCycleStatusDefinition.ApprovedStatus);

            if (!approvedId.HasValue)
                throw new InvalidOperationException("Could not resolve approved status.");

            //These are the definitions we will apply a simple filter for (OR between definitions)
            string[] definitionsToDisplay = new string[] { "M.Sku", "M.Product", "M.Campaign", "M.Recipe" };
            foreach(string definition in definitionsToDisplay)
            {
                filter.Add(new FieldFilter<string>(Stylelabs.M.Base.ElasticSearch.Constants.PropertyNames.DefinitionName) { Value = definition });
            }

            //For asset we will only display the approved ones
            var assetFilter = new CompositeFilter(FilterCombineMethod.And);
            assetFilter.Add(new FieldFilter<string>(Stylelabs.M.Base.ElasticSearch.Constants.PropertyNames.DefinitionName) { Value = "M.Asset" });
            assetFilter.Add(new FieldFilter<long>("relations.FinalLifeCycleStatusToAsset.parents") { Value = approvedId.Value });
            filter.Add(assetFilter);

            return filter;
        }
    }

この QueryContributor を使用することで、クエリに適用されているフィルターに直接影響を与えることができます。 これらのフィルターはクライアント側からは認識できません (querystring、querybuilder など)。

QueryContributor クラスの準備ができたら、次のステップはそれをプロジェクトの ApplicationModule に登録することです。

        public override void OnConfiguration(IContainer container)
        {
            base.OnConfiguration(container);

            IQueryConfiguration queryConfiguration = container.Resolve<IQueryConfiguration>();
            queryConfiguration.QueryContributorRegistry.Add<SearchConfigurationQueryContributor>("SearchConfiguration");
        }

構成によるフィルターには、複数のクエリ コントリビューターを組み合わせることができます。 クエリ コントリビューターは、フィルターを追加する SearchConfiguration の名前に常に関連付けられています。

この記事を改善するための提案がある場合は、 お知らせください!