Tous les produits
Search
Centre de documentation

OpenSearch:Push documents by using the PHP SDK

Dernière mise à jour :Aug 10, 2026

Utilisez le SDK PHP d'OpenSearch pour ajouter, mettre à jour et supprimer des documents dans votre application.

Configurer les variables d'environnement

Configurez les variables d'environnement ALIBABA_CLOUD_ACCESS_KEY_ID et ALIBABA_CLOUD_ACCESS_KEY_SECRET.

Important
  • La paire AccessKey d'un compte Alibaba Cloud permet d'accéder à toutes les opérations d'API. Nous vous recommandons d'utiliser un utilisateur Resource Access Management (RAM) pour appeler les opérations d'API ou effectuer des tâches de maintenance courantes. Pour savoir comment utiliser un utilisateur RAM, consultez la rubrique Créer un utilisateur RAM.

  • Pour savoir comment créer une paire AccessKey, consultez la rubrique Créer une paire AccessKey.

  • Si vous utilisez la paire AccessKey d'un utilisateur RAM, assurez-vous que les autorisations requises sont accordées au rôle AliyunServiceRoleForOpenSearch via votre compte Alibaba Cloud. Pour plus d'informations, consultez les rubriques AliyunServiceRoleForOpenSearch et Règles d'autorisation d'accès.

  • Nous vous déconseillons d'inclure votre paire AccessKey dans des supports facilement accessibles à des tiers, tels que le code du projet. Dans le cas contraire, vos identifiants AccessKey pourraient être divulgués, ce qui compromettrait la sécurité des ressources de votre compte.

  • Linux et macOS

    Exécutez les commandes suivantes. Remplacez <access_key_id> et <access_key_secret> par l'ID de clé d'accès (AccessKey ID) et la clé d'accès secrète (AccessKey Secret) de l'utilisateur RAM que vous utilisez.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> 
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Windows

    1. Créez un fichier de variables d'environnement, ajoutez-y les variables d'environnement ALIBABA_CLOUD_ACCESS_KEY_ID et ALIBABA_CLOUD_ACCESS_KEY_SECRET, puis définissez ces variables avec votre ID de clé d'accès (AccessKey ID) et votre clé d'accès secrète (AccessKey Secret).

    2. Redémarrez Windows pour que la paire AccessKey prenne effet.

Créer un fichier de configuration

Ce fichier spécifie la paire AccessKey, l'endpoint API, le nom de l'application, le modèle de suggestion déroulante ainsi que les options utilisées pour envoyer et interroger des documents.

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

Envoyer des documents

Ajouter des documents

L'exemple suivant ajoute 10 documents à une application OpenSearch.

<?php
// Import the header file.
require_once("Config.inc.php");
use OpenSearch\Client\DocumentClient;
// Specify the table to which you want to push documents.
$tableName = 'The table to which you want to push documents';
// Create a DocumentClient object.
$documentClient = new DocumentClient($client);
// Add the documents to be pushed to an array.
$docsToUpload = array();
for ($i = 0; $i < 10; $i++){
    $item = array();
    $item['cmd'] = 'ADD';
    $item["fields"] = array(
        "id" => $i + 1,
        "name" => "Search".$i
        );
    $docsToUpload[] = $item;
}
// Encode the documents to be pushed into a JSON string.
$json = json_encode($docsToUpload);
// Call the push method to push the documents.
$ret = $documentClient->push($json, $appName, $tableName);

Mettre à jour des documents

<?php
// Import the header file.
require_once("Config.inc.php");
use OpenSearch\Client\DocumentClient;
// Specify the table to which you want to push documents.
$tableName = 'The table to which you want to push documents';
// Create a DocumentClient object.
$documentClient = new DocumentClient($client);
// Add the documents to be pushed to an array.
$docsToUpload = array();
$item = array();
// Only OpenSearch applications of the advanced edition support the UPDATE operation.
$item['cmd'] = 'UPDATE';
$item["fields"] = array(
    "id" => 1,
    "name" => "OpenSearch"
);
$docsToUpload[] = $item;
// Encode the documents to be pushed into a JSON string.
$json = json_encode($docsToUpload);
// Call the push method to push the documents.
$ret = $documentClient->push($json, $appName, $tableName);

Supprimer des documents

<?php
// Import the header file.
require_once("Config.inc.php");
use OpenSearch\Client\DocumentClient;
// Specify the table to which you want to push documents.
$tableName = 'The table to which you want to push documents';
// Create a DocumentClient object.
$documentClient = new DocumentClient($client);
// Add the documents to be pushed to an array.
$docsToUpload = array();
$item = array();
$item['cmd'] = 'DELETE';
$item["fields"] = array(
    "id" => 1, // Enter the primary key IDs of the documents to be deleted.
);
$docsToUpload[] = $item;
// Encode the documents to be pushed into a JSON string.
$json = json_encode($docsToUpload);
// Call the push method to push the documents.
$ret = $documentClient->push($json, $appName, $tableName);