【问题标题】:How to: Paginated Search in Azure Search using PHP如何:使用 PHP 在 Azure 搜索中进行分页搜索
【发布时间】:2018-10-13 08:01:37
【问题描述】:

我有这个示例 PHP 代码:

 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\r\n" .
                  "api-key: ". $apiKey . "\r\n" .
                  "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;

}

这在单个查询/页面上运行良好。 假设我有 10,000 条记录要提取,并且在单个查询中,Azure 搜索的记录限制为 1000 条。 这应该提供 azure 搜索参数 $top- 它指定要在批处理中返回多少项目,而 $skip 指定要跳过多少项目。 插入这部分代码:

$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
            )
    );

假设,此查询将针对第一批/页面作为 top = 15 条记录显示。 对于下一批/页面跳过将迭代例如 'skip' => 15.

问题是我不知道如何迭代这个参数。还是我应该迭代这个?还是有其他方法? 天蓝色搜索参数参考:https://docs.microsoft.com/en-us/azure/search/search-pagination-page-layout

我正在考虑附加 json 文件。相关搜索:Append data to a .JSON file with PHP

以前,我显示了 1000 条记录。但我需要调整,因为我的记录已经有 1000 多条记录。 在前端——我通过 ajax 调用它。然后形成html。 然后我通过 jquery/javascript 将分页调整为每页(例如)20 条记录的块。

希望我不会混淆任何人。先感谢您!干杯!

顺便说一句:我正在使用 PHP,Symfony 2。

【问题讨论】:

    标签: php ajax azure azure-cognitive-search


    【解决方案1】:

    理想情况下,您的前端不需要一次处理 10000 条记录,因为用户很难管理这么大的列表,而且检索这么多记录可能会非常缓慢。如果可能的话,你也应该让前端直接分页,在这种情况下,“top”和“skip”将从你的前端传入,PHP 代码使用这些参数发出 1 个 POST 请求。

    但如果这不可行,您可以使用search API 不带“skip”和“top”的第一个请求,然后遵循 nextPageParameters 链。然后将每一页的值追加到1个数组中,返回给前端。

    // $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\r\n" .
                    "api-key: ". $apiKey . "\r\n" .
                    "Accept: application/json",
          'content'=>$postdata
        )
      );
      $context = stream_context_create($opts);
      $file = file_get_contents($url, false, $context);
      $file = json_decode($file, true);
    }
    return $values;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-04
      • 2021-05-12
      • 2014-11-29
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      相关资源
      最近更新 更多