Refinar los resultados de búsqueda mediante agregaciones (versión preliminar)Refine search results using aggregations (preview)

Refinar los resultados de la búsqueda y mostrar su distribución en el índice.Refine search results and show their distribution in the index.

Ejemplo 1: Solicitar agregaciones por campos de cadenaExample 1: Request aggregations by string fields

En el siguiente ejemplo se buscan recursos listItem y se agregan resultados por tipo de archivo y clase de contenido, ambos valores de cadena.The following example searches listItem resources and aggregates results by their file type and content class, both of which are string values.

La respuesta incluye dos objetos searchBucket para las dos agregaciones:The response includes two searchBucket objects for the two aggregations:

  • La propiedad key especifica el valor real (por o ) de los objetos FileType contentclass listItem que coincidan y que ese valor agrega en el mismo cubo.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.
  • La propiedad count especifica el número de objetos agregados en el mismo depósito.The count property specifies the number of such objects aggregated in the same bucket. Ten en cuenta que este número es una aproximación del número de coincidencias y no proporcionará un número exacto de coincidencias.Note that this number is an approximation of the number of matches and will not provide an exact number of matches.
  • Los cubos de resultados agregados por tipo de archivo se ordenan por recuento en orden descendente.Buckets of results aggregated by file type are sorted by count in descending order. En este ejemplo, hay 3 cubos para 3 tipos de archivo: docx xlsx , y pptx .In this example, there are 3 buckets for 3 file types: docx, xlsx, and pptx.
  • Los cubos de resultados agregados por clase de contenido se ordenan por el valor de cadena de la clase de contenido en orden descendente.Buckets of results aggregated by content class are sorted by the string value of the content class in descending order. En este ejemplo, solo hay un depósito con todos los objetos que coinciden compartiendo la misma clase de STS_ListItem_DocumentLibrary contenido.In this example, there is only one bucket with all the matching objects sharing the same content class, STS_ListItem_DocumentLibrary.

SolicitudRequest

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
              }
          }
      ]
    }
  ]
}

RespuestaResponse

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\""
                        }
                    ]
                }
            ]
        }
    ]
}

Ejemplo 2: Aplicar un filtro de agregación basado en una solicitud anteriorExample 2: Apply an aggregation filter based on a previous request

En este ejemplo, se aplica un filtro de agregación basado en el aggregationFilterToken devuelto como campo docx en el ejemplo FileType 1.In this example, we apply an aggregation filter that is based on the aggregationFilterToken returned for docx as the FileType field in example 1.

El valor de cadena asignado a la propiedad aggregationFilters sigue el formato "{field}: \ "{aggregationFilterToken} \ "".The string value assigned to the aggregationFilters property follows the format "{field}:\"{aggregationFilterToken}\"". Si se requieren varios valores para el mismo filtro, el valor de cadena asignado a la propiedad aggregationFilters debe seguir este formato: "{field}:or( \ "{aggregationFilterToken1} \ ", \ "{aggregationFilterToken2} \ ")".If multiple values for the same filter are required, the string value assigned to the aggregationFilters property should follow this format : "{field}:or(\"{aggregationFilterToken1}\",\"{aggregationFilterToken2}\")".

SolicitudRequest

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\""
      ]
    }
  ]
}

RespuestaResponse

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\""
                    }
                ]
            }
        ]
        }
    ]
}

Ejemplo 3: Solicitar agregación mediante un campo numéricoExample 3: Request aggregation by a numeric field

En el siguiente ejemplo se buscan recursos driveItem y se agregan resultados por su tamaño, que es un valor numérico.The following example searches driveItem resources and aggregates results by their size which is a numeric value. La solicitud especifica la agregación en 3 intervalos de tamaño:The request specifies aggregation by 3 size ranges:

  • Tamaño inferior a 100Size less than 100
  • Tamaño entre 100 y 1000Size between 100 and 1000
  • Tamaño 1000 y superiorSize 1000 and higher

La respuesta incluye 3 objetos searchBucket, uno para cada agregación de intervalo de tamaño:The response includes 3 searchBucket objects, one for each size range aggregation:

  • Los 2 cubos de los intervalos de tamaño inferior no incluyen ninguna coincidencia de búsqueda.The 2 buckets of the lower size ranges don't include any search matches.
  • Las 9 coincidencias de búsqueda tienen tamaños 1000 o superiores.All 9 search matches have sizes 1000 or higher.

SolicitudRequest

POST https://graph.microsoft.com/beta/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"
                            }
                        ]
                    }
                }
            ]
        }
    ]
}

RespuestaResponse

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\")"
                        }
                    ]
                }
            ]
        }
    ]
}

Limitaciones conocidasKnown limitations

Las agregaciones solo se admiten para elementos de SharePoint o OneDrive.Aggregations are supported only for SharePoint or OneDrive items. No se admiten para mensajes o eventos.They are not supported for message or event.

Pasos siguientesNext steps