2017-10-12 2 views
0

현재 하늘 검색 API를 호출해야하는 애플리케이션을 만들고 있습니다. "@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"Azure Search에서 다음 레코드를 가져 오는 링크를 얻는 방법은 무엇입니까?

: 나는 푸른 검색에 내 검색 쿼리를 실행할 때

@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; 
} 

, 나는 같은 다음의 결과에 대한 링크를 얻을 : 여기 https://serviceName.search.windows.net/indexes/global-index/docs/search?api-version=2016-09-01

내 자바 코드 : 여기

는 API입니다

하지만 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 –

관련 문제