使用 Microsoft Graph 搜索 API 传递多个搜索请求

Microsoft 搜索 API 允许在单个请求正文中包含多个搜索请求。 此功能目前可用于 OneDrive 和 SharePoint,以及 Microsoft Graph 连接器 和答案资源类型(包括书签、首字母缩写词和 Q&A)引入和索引的外部内容。

支持的实体类型

支持在请求正文中包含多个搜索请求的实体类型包括:sitedrivedriveItemlistItemexternalItembookmarkacronymqna 同一请求块中的实体类型应遵循 交错搜索 主题中定义的受支持的实体类型关系和组合。

示例

示例 1:使用 SharePoint 文件类型和书签/首字母缩略词组合搜索

请求

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

{
  "requests": [
    {
      "entityTypes": [
        "bookmark", "acronym"
      ],
      "query": {
        "queryString": "POC"
      },
      "from": 0,
      "size": 25
    },
    {
      "entityTypes": [
        "listItem",
        "site"
      ],
      "query": {
        "queryString": "POC"
      },
      "from": 0,
      "size": 25
    }
  ]
}

响应

下面显示了使用 SharePoint 文件类型和书签/首字母缩略词组合进行搜索的响应。

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

{
  "@odata.context": "https://graph.microsoft.com/beta/$metadata#search",
  "value": [
    {
      "searchTerms": [
        "POC"
      ],
      "hitsContainers": [
        {
          "total": 1,
          "moreResultsAvailable": false,
          "hits": [
            {
              "hitId": "adce5789-c324-485a-a8bf-66bb809527ff",
              "rank": 1,
              "summary": "",
              "resource": {
                "@odata.type": "#microsoft.graph.search.acronym",
                "id": "adce5789-c324-485a-a8bf-66bb809527ff",
                "displayName": "POC",
                "description": "Acronym in Spanish",
                "webUrl": "",
                "standsFor": "prueba de concepto"
              }
            },
            {
              "hitId": "1c0599db-2e89-4327-827a-3935c999f6cc",
              "rank": 2,
              "summary": "",
              "resource": {
                "@odata.type": "#microsoft.graph.search.bookmark",
                "id": "1c0599db-2e89-4327-827a-3935c999f6cc",
                "displayName": "POC",
                "description": "A proof of concept (POC) is an exercise in which work is focused on determining whether an idea can be turned into a reality. ",
                "webUrl": "https://en.wikipedia.org/wiki/POC"
              }
            }
          ]
        }
      ]
    },
    {
      "searchTerms": [
        "POC"
      ],
      "hitsContainers": [
        {
          "total": 2,
          "moreResultsAvailable": false,
          "hits": [
            {
              "hitId": "adce5789-c324-485a-a8bf-66bb809527ff",
              "rank": 3,
              "summary": "Test listItem 1",
              "resource": {
                "@odata.type": "#microsoft.graph.listItem",
                "createdDateTime": "2019-10-07T10:00:08Z",
                "lastModifiedDateTime": "2019-10-07T10:00:11Z",
                "title": "Here is a summary of your messages from last week -   New Feature: Live captions in English-US a POC"
              }
            },
            {
              "hitId": "microsoft.sharepoint.com,9fb3f597-167e-4c3d-b5e6-1ddc18d22d48,c53cd46e-9033-4b42-af94-0ad76ab75fd0",
              "rank": 4,
              "summary": "Test site",
              "resource": {
                "@odata.type": "#microsoft.graph.site",
                "createdDateTime": "2019-10-07T10:00:08Z",
                "lastModifiedDateTime": "2019-10-07T10:00:11Z",
                "title": "Test site summary POC"
              }
            }
          ]
        }
      ]
    }
  ]
}

错误的请求示例

以下示例显示了导致错误请求错误响应的请求。 有关搜索请求的限制的详细信息,请参阅 [已知限制 (#known 限制) 。

示例 1:请求正文中的单独请求块中具有相同实体类型的搜索

请求

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

{
  "requests": [
    {
      "entityTypes": [
        "bookmark"
      ],
      "query": {
        "queryString": "POC"
      },
      "from": 0,
      "size": 25
    },
    {
      "entityTypes": [
        "bookmark",
      ],
      "query": {
        "queryString": "POC"
      },
      "from": 0,
      "size": 25
    }
  ]
}

响应

下面是错误请求响应的示例。

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

{
    "error": {
        "code": "BadRequest",
        "message": "SearchRequest Invalid (Entity types must not be duplicates in multiple entity requests)",
        "target": "",
        "details": [
            {
                "code": "Microsoft.SubstrateSearch.Api.ErrorReporting.ResourceBasedExceptions.BadRequestException",
                "message": "Entity types must not be duplicates in multiple entity requests",
                "target": "",
                "httpCode": 400
            }
        ],
        "httpCode": 400
    }
}

示例 2:使用请求正文中单独请求块中的文件实体类型搜索

请求

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

{
  "requests": [
    {
      "entityTypes": [
        "site"
      ],
      "query": {
        "queryString": "POC"
      },
      "from": 0,
      "size": 25
    },
    {
      "entityTypes": [
        "drive",
      ],
      "query": {
        "queryString": "POC"
      },
      "from": 0,
      "size": 25
    }
  ]
}

响应

下面是错误请求响应的示例。

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

{
    "error": {
        "code": "BadRequest",
        "message": "SearchRequest Invalid (Entity types must not be duplicates in multiple entity requests)",
        "target": "",
        "details": [
            {
                "code": "Microsoft.SubstrateSearch.Api.ErrorReporting.ResourceBasedExceptions.BadRequestException",
                "message": "File entity types can only appear in one entity request",
                "target": "",
                "httpCode": 400
            }
        ],
        "httpCode": 400
    }
}

已知限制

  • 请求正文中不同 searchRequest 实例中用于 fromsize 属性的值应相同。 在不同 searchRequest 实例的不同 searchQuery 实例中用于 queryString 属性的值应相同。
  • 不允许在请求正文中跨不同搜索请求重复实体类型。 例如,请求正文中不能有 书签searchRequest 和另一个 书签searchRequest
  • (站点驱动器driveItemlistlistItem) 的文件实体类型只能出现在请求正文的同一搜索请求中。 例如,不能在一个 searchRequest 中包含网站实体,在请求正文中包含另一个 searchRequest 中的驱动器实体。
  • 拼写检查器功能只能使用一次,它应包含在请求正文的第一个 searchRequest 中。

后续步骤