使用 Microsoft 搜索 API 请求拼写更正
可以使用 Microsoft Graph 中的 Microsoft 搜索 API 请求拼写更正,以处理用户查询中的拼写错误与匹配内容中字词之间的不匹配。 若要请求拼写更正,请在 searchRequest 的 queryAlterationOptions 属性中指定以下属性:
enableSuggestion 用于为用户查询启用/禁用拼写建议。 可以传递
true
以获取用户查询中拼写错误的建议拼写更正信息。enableModification 用于为用户查询启用/禁用拼写修改。 如果原始查询没有拼写错误的结果,可以传递
true
以获取已更正查询的搜索结果,并获取相应的更正信息。
如果同时启用拼写修改,则拼写修改的优先级高于拼写建议。
如果在启用拼写检查器修改时禁用拼写检查器建议,则结果仍可以包含原始查询拼写错误的拼写检查器建议。 这是设计使然,因为如果启用了拼写修改,该功能默认会提供拼写建议。
所有用户查询字符串都应相同,以便对多个实体的搜索启用拼写更正。
以下示例查询包含字符串accountt
的 listItem 资源,并请求查询的拼写建议。
响应包含拼写建议的 alterationResponse 对象。
POST https://graph.microsoft.com/beta/search/query
Content-Type: application/json
{
"requests": [
{
"entityTypes": [
"listItem"
],
"query": {
"queryString": "accountt"
},
"from": 0,
"size": 25,
"queryAlterationOptions": {
"enableSuggestion": true,
"enableModification": false
}
}
]
}
HTTP/1.1 200 OK
Content-type: application/json
{
"@odata.context": "https://graph.microsoft.com/beta/$metadata#Collection(microsoft.graph.searchResponse)",
"value": [
{
"searchTerms": [
"accountt"
],
"hitsContainers": [
{
"total": 0,
"moreResultsAvailable": false
}
],
"queryAlterationResponse": {
"@odata.type": "#microsoft.substrateSearch.alterationResponse",
"originalQueryString": "accountt",
"queryAlteration": {
"@odata.type": "#microsoft.substrateSearch.searchAlteration",
"alteredQueryString": "account",
"alteredHighlightedQueryString": "account",
"alteredQueryTokens": [
{
"offset": 0,
"length": 8,
"suggestion": "account"
}
]
},
"queryAlterationType": "Suggestion"
}
}
]
}
以下示例查询包含字符串accountt
的 listItem 资源,并请求对查询进行拼写修改。
在此示例中,原始查询没有拼写错误 accountt
的结果。 响应包含与更正字符串 account
和拼写修改的 更改Response 对象相关的结果。
POST https://graph.microsoft.com/beta/search/query
Content-Type: application/json
{
"requests": [
{
"entityTypes": [
"listItem"
],
"query": {
"queryString": "accountt"
},
"from": 0,
"size": 25,
"queryAlterationOptions": {
"enableSuggestion": true,
"enableModification": true
}
}
]
}
HTTP/1.1 200 OK
Content-type: application/json
{
"@odata.context": "https://graph.microsoft.com/beta/$metadata#Collection(microsoft.graph.searchResponse)",
"value": [
{
"searchTerms": [
"account"
],
"hitsContainers": [
{
"total": 1,
"moreResultsAvailable": false,
"hits": [
{
"hitId": "FlULeN/ui/1GjLx1rUfio5UAAEl",
"rank": 1,
"summary": "",
"resource": {
"@odata.type": "#microsoft.graph.listItem",
"createdDateTime": "2019-06-10T06:37:43Z",
"lastModifiedDateTime": "2019-06-10T06:37:43Z",
"name": "web_part_test_long Notebook",
"webUrl": "https://contoso.sharepoint.com/sites/contoso-team/Lists/Issue tracker list/DispForm.aspx?ID=1",
"sharepointIds": {
"listId": "33498de0-d695-4d23-ac26-e1bf95a3206e",
"listItemId": "13"
},
"createdBy": {
"user": {
"displayName": "System Account"
}
},
"lastModifiedBy": {
"user": {
"displayName": "System Account"
}
},
"parentReference": {
"sharepointIds": {
"listId": "da61a2b0-4120-4a3f-812b-0fc0d79bf16b"
},
"siteId": "m365x231305.sharepoint.com,5724d91f-650c-4810-83cc-61a8818917d6,c3ba25dc-2c9f-48cb-83be-74cdf68ea5a0"
}
}
}
]
}
],
"queryAlterationResponse": {
"@odata.type": "#microsoft.substrateSearch.alterationResponse",
"originalQueryString": "accountt",
"queryAlteration": {
"@odata.type": "#microsoft.substrateSearch.searchAlteration",
"alteredQueryString": "account",
"alteredHighlightedQueryString": "account",
"alteredQueryTokens": [
{
"offset": 0,
"length": 8,
"suggestion": "account"
}
]
},
"queryAlterationType": "Modification"
}
}
]
}
仅以下资源支持拼写更正:消息、事件、站点、驱动器、drive、driveItem、listItem、listItem 和 externalItem。