Configure environment variables
Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. For information about how to use a RAM user, see Create a RAM user.
For information about how to create an AccessKey pair, see Create an AccessKey pair.
If you use the AccessKey pair of a RAM user, make sure that the required permissions are granted to the AliyunServiceRoleForOpenSearch role by using your Alibaba Cloud account. For more information, see AliyunServiceRoleForOpenSearch and Access authorization rules.
We recommend that you do not include your AccessKey pair in materials that are easily accessible to others, such as the project code. Otherwise, your AccessKey pair may be leaked and resources in your account become insecure.
Linux and macOS
Run the following commands. Replace
<access_key_id>
and<access_key_secret>
with the AccessKey ID and AccessKey secret of the RAM user that you use.export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
Windows
Create an environment variable file, add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to the file, and then set the environment variables to your AccessKey ID and AccessKey secret.
Restart Windows for the AccessKey pair to take effect.
Create a header file that contains configurations
The configurations that you specify in this header file are used to push and query documents. The configurations include the AccessKey pair, API endpoint, application name, name of the drop-down suggestion model, and options.
<?php
// Import the header file.
require_once("../OpenSearch/Autoloader/Autoloader.php");
use OpenSearch\Client\OpenSearchClient;
// Specify your AccessKey pair.
// Obtain the AccessKey ID and AccessKey secret from environment variables.
// You must configure environment variables before you run this code. For more information, see the "Configure environment variables" section of this topic.
// Specify the AccessKey ID.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
// Specify the AccessKey secret.
$secret = getenv('ALIBABA_CLOUD_ACCESS_KEY_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 name of the drop-down suggestion model.
$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 sample 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 SearchParamsBuilder 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 returned results.
$params->setHits(1);
// Specify the application name.
$params->setAppName('The application name');
// Specify a search query.
$params->setQuery("name: 'Search'");
// Specify the data format of returned 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.
$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 returned results.
print_r($ret.'<br/><br/>');
}