目前我正在創建一個應用程序,我需要調用Azure搜索的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上搜索運行我的搜索查詢,我得到鏈接,就像一個結果:"@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服務運行相同的查詢時,我得到下一個文檔的鏈接:
"@odata.nextLink": "https://serviceName.search.windows.net/indexes('global-index')/docs/search.post.search?api-version=2016-09-01"
通過第二個鏈接,我無法獲得下一個文檔。爲什麼JSON對象中的鏈接與實際需要的鏈接不同?
另一件事是, 當我把「熱門」的條件,通過我的代碼,結果有去無回我聯繫明年document.What可能是什麼原因呢?
如何獲取正確的鏈接以查看JSON輸出中的下一個文檔?
謝謝。獲得總數後,實現JAVA邏輯將很容易。目前我需要檢索2123個文檔(結果中的JSON對象)。運行搜索查詢後,有什麼方法可以讓我們獲得結果的總數? –
使用$ count = true。所有Search API參數均在此處進行了說明:https://docs.microsoft.com/rest/api/searchservice/search-documents#request –