All Products
Search
Document Center

Demo code for pushing data and implementing the search feature

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

Upload documents

In the following code, 10 documents are uploaded to an OpenSearch application.

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

// Specify the table to which data is to be uploaded.
$tableName = 'The table to which data is to be uploaded';
// Create a DocumentClient object.
$documentClient = new DocumentClient($client);
// Add the documents to be uploaded to an array.
$docs_to_upload = array();
for ($i = 0; $i < 10; $i++){
    $item = array();
    $item['cmd'] = 'ADD';
    $item["fields"] = array(
        "id" => $i + 1,
        "name" => "Search for kfc".$i
        );
    $docs_to_upload[] = $item;
}
// Encode the documents.
$json = json_encode($docs_to_upload);
// Call the push method to upload the documents.
$ret = $documentClient->push($json, $appName, $tableName);

// Hibernate the thread for 10 seconds. If you push documents to the primary table of an advanced application, 90% of the documents can be synchronized in 10 seconds and 99% of the documents are available within 10 minutes. However, this is not applicable to document push to secondary tables. 
sleep(10);

// Create a SearchClient object.
$searchClient = new SearchClient($client);
// Create a SearchParamsBuilder object.
$params = new SearchParamsBuilder();
// Set the start parameter in the config clause.
$params->setStart(0);
// Set the hits parameter in the config clause.
$params->setHits(20);
// Specify the application for which you want to implement the search feature.
$params->setAppName('The application name');
// Specify a query keyword.
$params->setQuery("name:'Search'");
// Specify the data format of return results. In this example, the data format is set to FULLJSON.
$params->setFormat("fulljson");
// Specify a field based on which documents are to be sorted.
$params->addSort('RANK', SearchParamsBuilder::SORT_DECREASE);
// Run the query and return the results.
$ret = $searchClient->execute($params->build());
// Decode the results that are in the JSON format.
print_r(json_decode($ret->result,true));
// Display debugging information.
echo $ret->traceInfo->tracer;