集計を使用して検索結果を絞り込むRefine search results using aggregations

検索結果を絞り込んで、インデックスにその配布を表示します。Refine search results and show their distribution in the index.

例 1: 文字列フィールドによる集計を要求するExample 1: Request aggregations by string fields

次の例では、 listItem リソースを検索し、それらのファイルの種類と content クラス (両方とも文字列値) によって結果を集約します。The following example searches listItem resources and aggregates results by their file type and content class, both of which are string values.

応答には、2つの集計に対して2つの Searchbucket オブジェクトが含まれています。The response includes two searchBucket objects for the two aggregations:

  • キープロパティは、 FileType contentclass 同じバケット内で集計されたlistItemオブジェクトと一致する、その値によって実際の値 (または) を指定します。The key property specifies the actual value (by FileType or contentclass) for those matching listItem objects that are aggregated in the same bucket by that value.
  • Countプロパティは、同じバケット内で集約されたそのようなオブジェクトの数を指定します。The count property specifies the number of such objects aggregated in the same bucket. この数値は一致数の近似値で、正確に一致する数は提供されないことに注意してください。Note that this number is an approximation of the number of matches and will not provide an exact number of matches.
  • ファイルの種類によって集計された結果のバケットは、count で降順に並べ替えられます。Buckets of results aggregated by file type are sorted by count in descending order. この例では、3つのファイルの種類 (、、、) に3つのバケットがあります。 docx xlsx pptxIn this example, there are 3 buckets for 3 file types: docx, xlsx, and pptx.
  • Content クラスで集計された結果のバケットは、コンテンツクラスの文字列値で降順に並べ替えられます。Buckets of results aggregated by content class are sorted by the string value of the content class in descending order. この例では、一致するすべてのオブジェクトが同じコンテンツクラスを共有するバケットが1つだけ存在 STS_ListItem_DocumentLibrary します。In this example, there is only one bucket with all the matching objects sharing the same content class, STS_ListItem_DocumentLibrary.

要求Request

POST https://graph.microsoft.com/beta/search/query
Content-Type: application/json

{
  "requests": [
    {
      "entityTypes": [
          "listItem"
      ],
      "query": {
          "queryString": "test"
      },
      "from": 0,
      "size": 25,
      "aggregations": [
          {
              "field": "FileType",
              "size": 20,
              "bucketDefinition": {
                  "sortBy": "count",
                  "isDescending": "true",
                  "minimumCount": 0
              }
          },
          {
              "field": "contentclass",
              "size": 15,
              "bucketDefinition": {
                  "sortBy": "keyAsString",
                  "isDescending": "true",
                  "minimumCount": 0
              }
          }
      ]
    }
  ]
}

応答Response

HTTP/1.1 200 OK
Content-type: application/json

{
    "@odata.type": "#microsoft.graph.searchResponse",
    "hitsContainers": [
        {
            "@odata.type": "#microsoft.graph.searchHitsContainer",
            "hits": [
                "..."
            ],
            "total": 9,
            "moreResultsAvailable": false,
            "aggregations": [
                {
                    "@odata.type": "#microsoft.substrateSearch.searchAggregation",
                    "field": "FileType",
                    "buckets": [
                        {
                            "@odata.type": "#microsoft.substrateSearch.searchBucket",
                            "key": "docx",
                            "count": 5,
                            "aggregationFilterToken": "\"ǂǂ646f6378\""
                        },
                        {
                            "@odata.type": "#microsoft.substrateSearch.searchBucket",
                            "key": "xlsx",
                            "count": 3,
                            "aggregationFilterToken": "\"ǂǂ786c7378\""
                        },
                        {
                            "@odata.type": "#microsoft.substrateSearch.searchBucket",
                            "key": "pptx",
                            "count": 1,
                            "aggregationFilterToken": "\"ǂǂ70707478\""
                        }
                    ]
                },
                {
                    "@odata.type": "#microsoft.substrateSearch.searchAggregation",
                    "field": "contentclass",
                    "buckets": [
                        {
                            "@odata.type": "#microsoft.substrateSearch.searchBucket",
                            "key": "STS_ListItem_DocumentLibrary",
                            "count": 9,
                            "aggregationFilterToken": "\"ǂǂ5354535f4c6973744974656d5f446f63756d656e744c696272617279\""
                        }
                    ]
                }
            ]
        }
    ]
}

例 2: 以前の要求に基づいて集計フィルターを適用するExample 2: Apply an aggregation filter based on a previous request

この例では、例1のフィールドとして返された 集積 Ationfiltertoken に基づいて集計フィルターを適用し docx FileType ます。In this example, we apply an aggregation filter that is based on the aggregationFilterToken returned for docx as the FileType field in example 1.

集積 Ationfiltersプロパティに割り当てられた文字列値は、 "{field}: \ " {集積 ationfiltertoken} \ "" という形式に従います。The string value assigned to the aggregationFilters property follows the format "{field}:\"{aggregationFilterToken}\"".

要求Request

POST https://graph.microsoft.com/beta/search/query
Content-Type: application/json

{
  "requests": [
    {
      "entityTypes": [
          "driveItem"
      ],
      "query": {
          "queryString": "test"
      },
      "from": 0,
      "size": 20,
      "aggregations": [
          {
              "field": "FileType",
              "size": 10,
              "bucketDefinition": {
                  "sortBy": "count",
                  "isDescending": "true",
                  "minimumCount": 0
              }
          }
      ],
      "aggregationFilters": [
        "FileType:\"ǂǂ68746d6c\""
      ]
    }
  ]
}

応答Response

HTTP/1.1 200 OK
Content-type: application/json

{
    "@odata.type": "#microsoft.graph.searchResponse",
    "hitsContainers": [
        {
            "@odata.type": "#microsoft.graph.searchHitsContainer",
            "hits": [
                "..."
            ],
            "total": 69960,
            "moreResultsAvailable": true,
            "aggregations": [
            {
                "@odata.type": "#microsoft.substrateSearch.searchAggregation",
                "field": "FileType",
                "buckets": [
                    {
                        "@odata.type": "#microsoft.substrateSearch.searchBucket",
                        "key": "html",
                        "count": 69960,
                        "aggregationFilterToken": "\"ǂǂ68746d6c\""
                    }
                ]
            }
        ]
        }
    ]
}

例 3: 数値フィールドによって集計を要求するExample 3: Request aggregation by a numeric field

次の例では、 ドライブ項目 のリソースを検索し、結果を数値として集計したサイズで集約します。The following example searches driveItem resources and aggregates results by their size which is a numeric value. 要求は、3つのサイズ範囲で集約された値を指定します。The request specifies aggregation by 3 size ranges:

  • 100未満のサイズSize less than 100
  • 100 ~ 1000 のサイズSize between 100 and 1000
  • サイズ1000以上Size 1000 and higher

応答には、3つの Searchbucket オブジェクトが含まれています。The response includes 3 searchBucket objects, one for each size range aggregation:

  • 小さいサイズの範囲の2つのバケットには、検索一致は含まれません。The 2 buckets of the lower size ranges don't include any search matches.
  • すべての9つの検索結果のサイズは1000以上です。All 9 search matches have sizes 1000 or higher.

要求Request

POST /search/query
Content-Type: application/json

{
    "requests": [
        {
            "entityTypes": [
                "driveItem"
            ],
            "query": {
                "queryString": "test"
            },
            "from": 0,
            "size": 10,
            "aggregations": [
                {
                    "field": "Size",
                    "size": 5,
                    "bucketDefinition": {
                        "sortBy": "keyAsNumber",
                        "isDescending": "true",
                        "minimumCount": 0,
                        "ranges": [
                            {
                                "to": "100"
                            },
                            {
                                "from": "100",
                                "to": "1000"
                            },
                            {
                                "from": "1000"
                            }
                        ]
                    }
                }
            ]
        }
    ]
}

応答Response

HTTP/1.1 200 OK
Content-type: application/json

{
    "@odata.type": "#microsoft.graph.searchResponse",
    "hitsContainers": [
        {
            "@odata.type": "#microsoft.graph.searchHitsContainer",
            "hits": [
                "..."
                    ],
            "total": 9,
            "moreResultsAvailable": false,
            "aggregations": [
                {
                    "@odata.type": "#microsoft.substrateSearch.searchAggregation",
                    "field": "Size",
                    "buckets": [
                        {
                            "@odata.type": "#microsoft.substrateSearch.searchBucket",
                            "key": "Less than 100",
                            "count": 0,
                            "aggregationFilterToken": "range(min, 100)"
                        },
                        {
                            "@odata.type": "#microsoft.substrateSearch.searchBucket",
                            "key": "100 up to 1000",
                            "count": 0,
                            "aggregationFilterToken": "range(100, 1000)"
                        },
                        {
                            "@odata.type": "#microsoft.substrateSearch.searchBucket",
                            "key": "1000 and up",
                            "count": 9,
                            "aggregationFilterToken": "range(1000, max, to=\"le\")"
                        }
                    ]
                }
            ]
        }
    ]
}

既知の制限Known limitations

集計は、SharePoint または OneDrive のアイテムに対してのみサポートされています。Aggregations are supported only for SharePoint or OneDrive items. メッセージイベント、およびexternalitemではサポートされていません。They are not supported for message, event, and externalItem.

次の手順Next steps