2017-10-12 110 views
0

目前我正在創建一個應用程序,我需要調用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輸出中的下一個文檔?

回答

1

它看起來像你使用不同的HTTP動詞,這就是爲什麼你得到不同的鏈接。第一個鏈接用於GET請求,而第二個鏈接用於POST請求。

建議不要依賴@odata.nextLink來實現分頁。該機制的存在是爲了保護您的服務免受昂貴的請求,而不是尋呼的一般機制。搜索服務本身決定何時返回此鏈接,並且該行爲可能會發生變化,因此您不應該依賴它。

相反,您應該通過$ top和$ skip請求所需的頁面。例如,如果您想在每頁100個文檔的頁面上顯示結果,則您的第一個請求將具有$ top = 100 & $ skip = 0,您的第二個請求將具有$ top = 100 & $ skip = 100,有$ top = 100 & $ skip = 200,依此類推。您可以繼續像這樣獲取,直到沒有更多結果。

+0

謝謝。獲得總數後,實現JAVA邏輯將很容易。目前我需要檢索2123個文檔(結果中的JSON對象)。運行搜索查詢後,有什麼方法可以讓我們獲得結果的總數? –

+0

使用$ count = true。所有Search API參數均在此處進行了說明:https://docs.microsoft.com/rest/api/searchservice/search-documents#request –