如何获取 link 以从 Azure 搜索中获取下一条记录?

How to get a link to fetch next records from Azure Search?

目前我正在创建一个应用程序,我需要在其中调用 API 的 azure 搜索。

这是 API : https://serviceName.search.windows.net/indexes/global-index/docs/search?api-version=2016-09-01

这是我的 Java 代码:

@Override
public JSONObject global(SearchParametersDto searchInTableDto) {
    JSONObject jsonOutput = new JSONObject();
    ArrayList encodedURLList = new ArrayList < > ();
    try {
        String url = "https://" + searchInTableDto.getServiceName().toString() + ".search.windows.net/indexes/" +
            searchInTableDto.getIndexName().toString() + "/docs/search?api-version=2016-09-01";

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        post.setHeader("Content-Type", "application/json");
        post.setHeader("api-key", searchInTableDto.getApiKey().toString());
        String clientId = searchInTableDto.getClientId().toString();
        String updatedClientId = " \"+" + clientId + "\" ";
        JSONObject jsonInput = new JSONObject();


        jsonInput.put("search", "(test||test||test||test||test||test||test)+ Contacts+Campaigns+Companies+Targets+Complanits+Claims+Activities+Opportunities+Completed Activities");
        //jsonInput.put("top", 1000);

        System.out.println(jsonInput);
        post.setEntity(new StringEntity(jsonInput.toString(), ContentType.create("application/json")));
        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer resultOutput = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            resultOutput.append(line);
        }
        JSONParser parser = new JSONParser();
        jsonOutput = (JSONObject) parser.parse(resultOutput.toString());
        org.json.simple.JSONArray valuesArray = (org.json.simple.JSONArray) jsonOutput.get("value");
        //jsonOutput.put("searchResult", valuesArray);
        System.out.println(valuesArray.size());
    } catch (Exception e) {
        e.printStackTrace();
        String errorMessageAndClassWithMethod = getErrorContainingClassAndMethod();
        throw new SpringAppRuntimeException(errorMessageAndClassWithMethod + e.toString());
    }
    return jsonOutput;
}

当我在 Azure 搜索中 运行 我的搜索查询时,我得到 link 下一个结果,例如:"@odata.nextLink": "https://serviceName.search.windows.net/indexes('global-index')/docs?api-version=2015-02-28-Preview&search=%28test%7C%7Ctest%7C%7Ctest%7C%7Ctest%7C%7Ctest%7C%7Ctest%7C%7Ctest%29%2B%20Contacts%2BCampaigns%2BCompanies%2BTargets%2BComplanits%2BClaims%2BActivities%2BOpportunities%2BCompleted%20Activities&$skip=50"

但是,当我 运行 通过我的 Java 服务进行相同的查询时,我得到以下文档的关注 link :

"@odata.nextLink": "https://serviceName.search.windows.net/indexes('global-index')/docs/search.post.search?api-version=2016-09-01"

到这秒link,我无法获取下一个文档。为什么 JSON 对象中的 link 与实际需要的 link 不同?

另一件事是, 当我通过我的代码为 "top" 设置条件时,结果永远不会 return me link for next document.What 可能是这个原因?

如何获得正确的 link 以查看 JSON 输出中的下一个文档?

您似乎使用了不同的 HTTP 动词,这就是您获得不同 link 的原因。第一个 link 用于 GET 请求,而第二个 link 用于 POST 请求。

不建议依赖@odata.nextLink实现分页。该机制的存在是为了保护您的服务免受昂贵请求的影响,而不是一种通用的寻呼机制。搜索服务本身决定何时 return 此 link 并且该行为可能会发生变化,因此您不应依赖它。

相反,您应该通过 $top 和 $skip 请求您想要的页面。例如,如果你想在每页 100 个文档中显示结果,你的第一个请求将有 $top=100&$skip=0,你的第二个请求将有 $top=100&$skip=100,你的第三个请求将有 $ top=100&$skip=200,依此类推。您可以继续这样获取,直到没有更多结果为止。