dtrphb5597 2018-10-12 08:03
浏览 54

如何:使用PHP在Azure搜索中进行分页搜索

I have this sample PHP code:

 public function getListFromAzure($searchParam, $listCategory, $request){
    $aListingManager = $this->get('recruitday.model_manager.job_listing');
    $url = $jobListingManager->getAzureSearchParam($request, 'azure_search_idx');
    $apiKey = $jobListingManager->getAzureSearchParam($request, 'azure_key');


    $searchParam = preg_replace('/\s+/', '+', $searchParam);
    $postdata = json_encode(
        array(
            'search' => $searchParam,
            'filter' => $listCategory,
            'orderby'=> 'publishedDate desc',
            'facets' => array('locationName','empType', 'workSchedule','listFunction','positionLevel','industry'),
            'top' => 15,
            )
    );    



    $opts = array(
      'http'=>array(
        'method'=>"POST",
        'header'=>"Content-type: application/json
" .
                  "api-key: ". $apiKey . "
" .
                  "Accept: application/json",
        'content'=>$postdata
      )
    );

    $context = stream_context_create($opts);

    // Open the file using the HTTP headers set above
    $file = file_get_contents($url, false, $context);
    $file = json_decode($file,true);
    return $file;

}

This works fine on a single query/page. Assuming I have 10,000 records to pull, and on a single query, azure search has limit of 1000 records. This should come up with azure search parameters $top- where it specifies how many items to return in a batch, and $skip specifies how many items to skip. inserted in this part of code:

$postdata = json_encode(
        array(
            'search' => $searchParam,
            'filter' => $jobCategory,
            'orderby'=> 'publishedDate desc',
            'facets' => array('locationName','employmentType', 'workSchedule','jobFunction','positionLevel','industry'),
            'top' => 15,
            'skip' => 0,
            'count' => true
            )
    );

assuming, this query will be for the 1st batch/page as top =15 records to be shown. for the next batch/page skip would iterate for example as 'skip' => 15.

The problem is I don't how to iterate this parameter. or should I iterate this? or there's another way?. azure search parameters reference: https://docs.microsoft.com/en-us/azure/search/search-pagination-page-layout

I'm thinking of appending json file. relevant search: Append data to a .JSON file with PHP

previously, I am having 1000 records shown. but I need to adjust since my records has more than 1000 records already. on the front end side -- I am calling this through ajax. then formed into html. then there I adjust the pagination through jquery/javascript turning into chunks of 20 records for (example) per page.

hope I don't confuse anyone. thank you in advance! cheers!

btw: I am using PHP, Symfony 2.

  • 写回答

1条回答 默认 最新

  • dpict99695329 2018-10-13 00:01
    关注

    Ideally your front end shouldn't need to handle 10000 records at a time, because it's hard for a user to manage a list that large and can be very slow to retrieve that many records. If possible you should make the front end directly paginated as well, in which case "top" and "skip" will be passed in from your front end and the PHP code makes 1 POST request with those parameters.

    But if that's not possible, you can use the search API without "skip" and "top" for the first request, and then follow the chain of nextPageParameters. Then append the value from every page into 1 array and return it to the front end.

    // $file = file_get_contents($url, false, $context); // without "top" and "skip"
    // $file = json_decode($file,true);
    
    $values = array();
    while (True)
    {
      // get values from current page
      $values = array_merge($array, $file["value"]);
    
      // stop if there are no more pages
      if (!array_key_exists("@search.nextPageParameters", $file))
      {
        break;
      }
    
      // get next page 
      $postdata = $file["@search.nextPageParameters"];
      $opts = array(
        'http'=>array(
          'method'=>"POST",
          'header'=>"Content-type: application/json
    " .
                    "api-key: ". $apiKey . "
    " .
                    "Accept: application/json",
          'content'=>$postdata
        )
      );
      $context = stream_context_create($opts);
      $file = file_get_contents($url, false, $context);
      $file = json_decode($file, true);
    }
    return $values;
    
    评论

报告相同问题?