searchEntity: query
本文内容
命名空间:microsoft.graph
重要
Microsoft Graph /beta
版本下的 API 可能会发生更改。 不支持在生产应用程序中使用这些 API。 若要确定 API 是否在 v1.0 中可用,请使用 版本 选择器。
运行指定的搜索查询。 响应中提供了搜索结果。
警告
Microsoft Search API 请求和响应中使用的资源已重命名或删除了属性或者已被弃用。 查找有关过时的更多详细信息 。 相应更新任何早期应用中的搜索 API 查询。
此 API 可用于以下国家级云部署 。
全局服务
美国政府 L4
美国政府 L5 (DOD)
由世纪互联运营的中国
✅
✅
✅
✅
权限
为此 API 选择标记为最低特权的权限。 只有在应用需要它时 ,才使用更高的特权权限。 有关委派权限和应用程序权限的详细信息,请参阅权限类型 。 要了解有关这些权限的详细信息,请参阅 权限参考 。
权限类型
最低特权权限
更高特权权限
委派(工作或学校帐户)
Mail.Read
Acronym.Read.All、Bookmark.Read.All、Calendars.Read、Chat.Read、ExternalItem.Read.All、Files.Read.All、QnA.Read.All、Sites.Read.All
委派(个人 Microsoft 帐户)
不支持。
不支持。
应用程序
Files.Read.All
Sites.Read.All
HTTP 请求
POST /search/query
名称
说明
Authorization
持有者 {token}。 必填。 详细了解 身份验证和授权 。
Content-type
application/json. 必需。
请求正文
在请求正文中,提供具有以下参数的 JSON 对象。
参数
类型
说明
请求
searchRequest 集合
一个或多个搜索请求的集合,每个搜索请求在 JSON Blob 中设置格式。 每个 JSON Blob 都包含响应中预期的资源类型、基础源、分页参数、请求的字段和实际搜索查询。 请注意在搜索特定实体类型组合以及对搜索结果进行排序或聚合方面的 已知限制 。
响应
如果成功,此方法在响应正文中返回响应 HTTP 200 OK
代码和 searchResponse 对象的集合。
示例
以下示例演示如何执行搜索以获取预期的连接器项。
请求
POST https://graph.microsoft.com/beta/search/query
Content-type: application/json
{
"requests": [
{
"entityTypes": [
"externalItem"
],
"contentSources": [
"/external/connections/connectionfriendlyname"
],
"region": "US",
"query": {
"queryString": "contoso product"
},
"from": 0,
"size": 25,
"fields": [
"title",
"description"
]
}
]
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Beta.Search.Query;
using Microsoft.Graph.Beta.Models;
var requestBody = new QueryPostRequestBody
{
Requests = new List<SearchRequest>
{
new SearchRequest
{
EntityTypes = new List<EntityType?>
{
EntityType.ExternalItem,
},
ContentSources = new List<string>
{
"/external/connections/connectionfriendlyname",
},
Region = "US",
Query = new SearchQuery
{
QueryString = "contoso product",
},
From = 0,
Size = 25,
Fields = new List<string>
{
"title",
"description",
},
},
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Search.Query.PostAsQueryPostResponseAsync(requestBody);
// THE CLI IS IN PREVIEW. NON-PRODUCTION USE ONLY
mgc-beta search query post --body '{\
"requests": [\
{\
"entityTypes": [\
"externalItem"\
],\
"contentSources": [\
"/external/connections/connectionfriendlyname"\
],\
"region": "US",\
"query": {\
"queryString": "contoso product"\
},\
"from": 0,\
"size": 25,\
"fields": [\
"title",\
"description"\
]\
}\
]\
}\
'
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
graphsearch "github.com/microsoftgraph/msgraph-beta-sdk-go/search"
graphmodels "github.com/microsoftgraph/msgraph-beta-sdk-go/models"
//other-imports
)
graphClient := msgraphsdk.NewGraphServiceClientWithCredentials(cred, scopes)
requestBody := graphsearch.NewQueryPostRequestBody()
searchRequest := graphmodels.NewSearchRequest()
entityTypes := []graphmodels.EntityTypeable {
entityType := graphmodels.EXTERNALITEM_ENTITYTYPE
searchRequest.SetEntityType(&entityType)
}
searchRequest.SetEntityTypes(entityTypes)
contentSources := []string {
"/external/connections/connectionfriendlyname",
}
searchRequest.SetContentSources(contentSources)
region := "US"
searchRequest.SetRegion(®ion)
query := graphmodels.NewSearchQuery()
queryString := "contoso product"
query.SetQueryString(&queryString)
searchRequest.SetQuery(query)
from := int32(0)
searchRequest.SetFrom(&from)
size := int32(25)
searchRequest.SetSize(&size)
fields := []string {
"title",
"description",
}
searchRequest.SetFields(fields)
requests := []graphmodels.SearchRequestable {
searchRequest,
}
requestBody.SetRequests(requests)
query, err := graphClient.Search().Query().PostAsQueryPostResponse(context.Background(), requestBody, nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.beta.search.query.QueryPostRequestBody queryPostRequestBody = new com.microsoft.graph.beta.search.query.QueryPostRequestBody();
LinkedList<SearchRequest> requests = new LinkedList<SearchRequest>();
SearchRequest searchRequest = new SearchRequest();
LinkedList<EntityType> entityTypes = new LinkedList<EntityType>();
entityTypes.add(EntityType.ExternalItem);
searchRequest.setEntityTypes(entityTypes);
LinkedList<String> contentSources = new LinkedList<String>();
contentSources.add("/external/connections/connectionfriendlyname");
searchRequest.setContentSources(contentSources);
searchRequest.setRegion("US");
SearchQuery query = new SearchQuery();
query.setQueryString("contoso product");
searchRequest.setQuery(query);
searchRequest.setFrom(0);
searchRequest.setSize(25);
LinkedList<String> fields = new LinkedList<String>();
fields.add("title");
fields.add("description");
searchRequest.setFields(fields);
requests.add(searchRequest);
queryPostRequestBody.setRequests(requests);
var result = graphClient.search().query().post(queryPostRequestBody);
const options = {
authProvider,
};
const client = Client.init(options);
const searchResponse = {
requests: [
{
entityTypes: [
'externalItem'
],
contentSources: [
'/external/connections/connectionfriendlyname'
],
region: 'US',
query: {
queryString: 'contoso product'
},
from: 0,
size: 25,
fields: [
'title',
'description'
]
}
]
};
await client.api('/search/query')
.version('beta')
.post(searchResponse);
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Models\QueryPostRequestBody;
use Microsoft\Graph\Generated\Models\SearchRequest;
use Microsoft\Graph\Generated\Models\EntityType;
use Microsoft\Graph\Generated\Models\SearchQuery;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new QueryPostRequestBody();
$requestsSearchRequest1 = new SearchRequest();
$requestsSearchRequest1->setEntityTypes([new EntityType('externalItem'), ]);
$requestsSearchRequest1->setContentSources(['/external/connections/connectionfriendlyname', ]);
$requestsSearchRequest1->setRegion('US');
$requestsSearchRequest1Query = new SearchQuery();
$requestsSearchRequest1Query->setQueryString('contoso product');
$requestsSearchRequest1->setQuery($requestsSearchRequest1Query);
$requestsSearchRequest1->setFrom(0);
$requestsSearchRequest1->setSize(25);
$requestsSearchRequest1->setFields(['title', 'description', ]);
$requestsArray []= $requestsSearchRequest1;
$requestBody->setRequests($requestsArray);
$result = $graphServiceClient->search()->query()->post($requestBody)->wait();
Import-Module Microsoft.Graph.Beta.Search
$params = @{
requests = @(
@{
entityTypes = @(
"externalItem"
)
contentSources = @(
"/external/connections/connectionfriendlyname"
)
region = "US"
query = @{
queryString = "contoso product"
}
from = 0
size = 25
fields = @(
"title"
"description"
)
}
)
}
Invoke-MgBetaQuerySearch -BodyParameter $params
from msgraph import GraphServiceClient
from msgraph.generated.search.query.query_post_request_body import QueryPostRequestBody
from msgraph.generated.models.search_request import SearchRequest
from msgraph.generated.models.entity_type import EntityType
from msgraph.generated.models.search_query import SearchQuery
graph_client = GraphServiceClient(credentials, scopes)
request_body = QueryPostRequestBody(
requests = [
SearchRequest(
entity_types = [
EntityType.ExternalItem,
],
content_sources = [
"/external/connections/connectionfriendlyname",
],
region = "US",
query = SearchQuery(
query_string = "contoso product",
),
from = 0,
size = 25,
fields = [
"title",
"description",
],
),
],
)
result = await graph_client.search.query.post(request_body)
响应
以下示例显示了相应的响应。
注意: 为了提高可读性,可能缩短了此处显示的响应对象。
HTTP/1.1 200 OK
Content-type: application/json
{
"value": [
{
"searchTerms": [
"searchTerms-value"
],
"hitsContainers": [
{
"hits": [
{
"hitId": "1",
"rank": 1,
"summary": "_summary-value",
"resource": "The source field will contain the underlying graph entity part of the response"
}
],
"total": 47,
"moreResultsAvailable": true
}
]
}
]
}
示例 2:使用 queryTemplate 的基本调用
以下示例演示如何使用可查询属性 createdBy 检索用户创建的所有文件。
请求
POST https://graph.microsoft.com/beta/search/query
Content-type: application/json
{
"requests": [
{
"entityTypes": [
"listItem"
],
"region": "US",
"query": {
"queryString": "contoso",
"queryTemplate":"{searchTerms} CreatedBy:Bob"
},
"from": 0,
"size": 25
}
]
}
响应
以下示例显示了相应的响应。
HTTP/1.1 200 OK
Content-type: application/json
{
"value": [
{
"searchTerms": [
"contoso"
],
"hitsContainers": [
{
"hits": [
{
"hitId": "1",
"rank": 1,
"summary": "_summary-value",
"resource": {
"@odata.type": "#microsoft.graph.listItem",
"id": "c23c7035-73d6-4bad-8901-9e2930d4be8e",
"createdBy": {
"user": {
"displayName": "Bob",
"email": "Bob@contoso.com"
}
},
"createdDateTime": "2021-11-19T17:04:18Z",
"lastModifiedDateTime": "2023-03-09T18:52:26Z"
}
}
],
"total": 1,
"moreResultsAvailable": false
}
]
}
]
}
相关内容