All Products
Search
Document Center

Demo code for implementing scroll queries

Last Updated: Sep 09, 2021

Header file for completing basic configurations

The configurations that you specify in this header file will be used to implement the document push and document query features. The configurations include the AccessKey information, API endpoint, application name, drop-down suggestion name, and options.

<?php
// Import the header file.
require_once("../OpenSearch/Autoloader/Autoloader.php");
use OpenSearch\Client\OpenSearchClient;

// Specify the AccessKey ID of your Alibaba Cloud account.
$accessKeyId = '<Your accessKeyId>';
// Specify the AccessKey secret of your Alibaba Cloud account.
$secret = '<Your secret>';
// Specify the endpoint of the OpenSearch API in your region. You can obtain the endpoint on the details page of the application in the OpenSearch console.
$endPoint = '<region endPoint>';
// Specify the application name.
$appName = '<app name>';
// Specify the drop-down suggestion name.
$suggestName = '<suggest name>';
// Enable the debugging mode.
$options = array('debug' => true);
// Create an OpenSearchClient object.
$client = new OpenSearchClient($accessKeyId, $secret, $endPoint, $options);

Implement scroll queries

The following demo code shows how to implement scroll queries.

<?php
header("Content-Type:text/html;charset=utf-8");
// Import the header file.
require_once("Config.inc.php");
use OpenSearch\Client\SearchClient;
use OpenSearch\Util\SearchParamsBuilder;

$searchClient = new SearchClient($client);
// Create a parameter object and specify the parameters.
$params = new SearchParamsBuilder();
// Specify the number of documents to be returned for each scroll query. You do not need to specify an offset for return results.
$params->setHits(1);
// Specify the application name.
$params->setAppName('The application name');
// Specify the query clause.
$params->setQuery("name:'Search'");
// Specify the data format of return results. Supported formats are JSON and FULLJSON. In this example, the data format is set to FULLJSON.
$params->setFormat("fulljson");

// In OpenSearch SDK for PHP V3, scroll queries support sorting only based on a single field. The field must be of the int type. We recommend that you configure sorting based on the primary key. This prevents repeated document retrieval that is caused by data updates during queries.
$params->addSort('id', SearchParamsBuilder::SORT_INCREASE);

// Specify a filter condition.
$params->setFilter('id>0');

// Specify the fields to be returned in search results.
$params->setFetchFields(array('id','name','phone','int_arr','literal_arr','float_arr','cate_id'));

// Specify a validity period for the scroll ID to be used by the next scroll query, in minutes. In this example, the value is set to 3m. You do not need to specify a scroll ID for the first scroll query. The first scroll query returns a scroll ID that can be used by the next scroll query.
 $params->setScrollExpire('3m');
// Run the query and return the results.
$ret = $searchClient->execute($params->build())->result;

for($i=0;$i<json_decode($ret)->result->viewtotal;$i++){
    // Use the scroll ID of the first scroll query as a query parameter.
    $params->setScrollId(json_decode($ret)->result->scroll_id);
    // Run the query again and return the results.
    $ret = $searchClient->execute($params->build())->result;
    // Display the return results.
    print_r($ret.'<br/><br/>');
}