Microsoft Graph API 中的错误请求 "ErrorExecuteSearchStaleData" - 搜索邮件

Bad Request "ErrorExecuteSearchStaleData" in Microsoft Graph API - Search for Mails

我们正在使用 Microsoft Graph 搜索 API 来搜索我们的 O365 电子邮件。由于搜索每个邮件请求只允许 25 个结果。 (参见 https://docs.microsoft.com/en-us/graph/api/resources/search-api-overview?view=graph-rest-beta#page-search-results

我们想通过像这样批量处理我们的搜索请求来解决这个问题:

POST https://graph.microsoft.com/beta/$batch
{
    "requests": [
        {
            "url": "/search/query",
            "method": "POST",
            "id": "MyMails-0",
            "body":
            {
                "requests": [
                    {
                        "entityTypes": ["message"],
                        "query":
                        {
                            "query_string":
                            {
                                "query": "some search text"
                            }
                        },
                        "from": 0,
                        "size": 25
                    }
                ]
            },
            "headers":
            {
                "Content-Type": "application/json"
            }
        },
        {
            "url": "/search/query",
            "method": "POST",
            "id": "MyMails-1",
            "body":
            {
                "requests": [
                    {
                        "entityTypes": ["message"],
                        "query":
                        {
                            "query_string":
                            {
                                "query": "some search text"
                            }
                        },
                        "from": 25,
                        "size": 25
                    }
                ]
            },
            "headers":
            {
                "Content-Type": "application/json"
            }
        },
        {
            "url": "/search/query",
            "method": "POST",
            "id": "MyMails-2",
            "body":
            {
                "requests": [
                    {
                        "entityTypes": ["message"],
                        "query":
                        {
                            "query_string":
                            {
                                "query": "some search text"
                            }
                        },
                        "from": 50,
                        "size": 25
                    }
                ]
            },
            "headers":
            {
                "Content-Type": "application/json"
            }
        },
        {
            "url": "/search/query",
            "method": "POST",
            "id": "MyMails-3",
            "body":
            {
                "requests": [
                    {
                        "entityTypes": ["message"],
                        "query":
                        {
                            "query_string":
                            {
                                "query": "some search text"
                            }
                        },
                        "from": 75,
                        "size": 25
                    }
                ]
            },
            "headers":
            {
                "Content-Type": "application/json"
            }
        }
    ]
}

这通常很好用,但在某些情况下 API returns 像这样:

{
    "id": "MyMails-2",
    "status": 400,
    "headers":
    {
        "Link": "<https://developer.microsoft-tst.com/en-us/graph/changes?$filterby=beta,RemoveDeprecatedUnderscoreProperty&from=2021-12-01&to=2022-01-01>;rel=\"deprecation\";type=\"text/html\",<https://developer.microsoft-tst.com/en-us/graph/changes?$filterby=beta,RemoveDeprecatedUnderscoreProperty&from=2021-12-01&to=2022-01-01>;rel=\"deprecation\";type=\"text/html\",<https://developer.microsoft-tst.com/en-us/graph/changes?$filterby=beta,RemoveDeprecatedUnderscoreProperty&from=2021-12-01&to=2022-01-01>;rel=\"deprecation\";type=\"text/html\"",
        "Deprecation": "Tue, 14 Dec 2021 23:59:59 GMT",
        "Sunset": "Sat, 31 Dec 2022 23:59:59 GMT",
        "Cache-Control": "no-cache",
        "Content-Type": "application/json"
    },
    "body":
    {
        "error":
        {
            "code": "BadRequest",
            "message": "\r\n An exception occurred\r\n Lss call failed with status code 400. \"Exchange service returned error ErrorExecuteSearchStaleData: Please reissue the query with rowOffset = 0. The specified rowoffset is '50', but the results are stale.\".",
            "innerError":
            {
                "date": "2022-01-25T09:58:53",
                "request-id": "75def95f-a857-427d-a8b4-ee2792329e87",
                "client-request-id": "75def95f-a857-427d-a8b4-ee2792329e87"
            }
        }
    }
}

我注意到 header 中的弃用说明,但是我找不到任何相关信息。与实际异常无关。

我错过了什么?

[解决方法]

感谢@user2250152 的提示。 这通过将请求分成两个请求大大减少了问题,而第二个请求一次可以请求超过 25 封邮件:

POST https://graph.microsoft.com/beta/$batch
{
    "requests": [
        {
            "url": "/search/query",
            "method": "POST",
            "id": "MyMails-0",
            "body": {
                "requests": [
                    {
                        "entityTypes": [
                            "message"
                        ],
                        "query": {
                            "query_string": {
                                "query": "some search text"
                            }
                        },
                        "from": 0,
                        "size": 25
                    }
                ]
            },
            "headers": {
                "Content-Type": "application/json"
            }
        },
        {
            "url": "/search/query",
            "method": "POST",
            "id": "MyMails-1",
            "body": {
                "requests": [
                    {
                        "entityTypes": [
                            "message"
                        ],
                        "query": {
                            "query_string": {
                                "query": "some search text"
                            }
                        },
                        "from": 25,
                        "size": 200
                    }
                ]
            },
            "headers": {
                "Content-Type": "application/json"
            }
        }
    ]
}

过时的结果错误有时会发生。

您可以减少批处理请求的数量,以减少出现陈旧结果错误的可能性。

对于第一页 "from": 0,最大 size 是 25。但是对于下一页 "from": 25,您可以将页数 size 增加到 200。

我用 "from": 25"size": 200 测试了搜索查询,它有 returns 200 个结果。

资源:

Page search results