【问题标题】:How to get a link to fetch next records from Azure Search?如何获取链接以从 Azure 搜索中获取下一条记录?
【发布时间】:2017-10-13 01:58:13
【问题描述】:

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

这里是 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 搜索中运行搜索查询时,我会获得下一个结果的链接,例如:"@odata.nextLink": "https://serviceName.search.windows.net/indexes('global-index')/docs?api-version=2015-02-28-Preview&amp;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&amp;$skip=50"

但是,当我通过我的 Java 服务运行相同的查询时,我会得到下一个文档的以下链接:

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

通过第二个链接,我无法获取下一个文档。为什么 JSON 对象中的链接与实际需要的链接不同?

还有一点, 当我通过我的代码设置“top”条件时,结果永远不会返回我下一个文档的链接。这可能是什么原因?

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

【问题讨论】:

    标签: azure azure-cognitive-search


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢。得到总数后,很容易实现 JAVA 逻辑。目前我需要检索 2123 个文档(结果中的 JSON 对象)。有什么方法可以让我们在运行搜索查询后获得结果总数?
    • 使用 $count=true。所有搜索 API 参数都记录在此处:docs.microsoft.com/rest/api/searchservice/…
    猜你喜欢
    • 2021-09-17
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多