Tous les produits
Search
Centre de documentation

OpenSearch:Démonstration de push

Dernière mise à jour :Aug 10, 2026

Poussez des documents vers une application OpenSearch High-Performance Search Edition à l'aide du SDK OpenSearch pour Java V3.1. Cet exemple ajoute, met à jour et supprime des documents en un seul lot, puis interroge les résultats pour vérifier le push.

Prérequis

Avant de commencer, assurez-vous de disposer des éléments suivants :

Configuration des identifiants

Stockez votre AccessKey ID et votre AccessKey secret sous forme de variables d'environnement. Ne codez jamais les identifiants en dur dans les fichiers source.

Important

La paire de clés AccessKey d'un compte Alibaba Cloud permet d'accéder à toutes les opérations API. Utilisez plutôt la paire de clés AccessKey d'un utilisateur RAM et n'accordez que les permissions nécessaires à l'application. Pour créer une paire de clés AccessKey, consultez Créer une paire de clés AccessKey. Pour créer un utilisateur RAM, consultez Créer un utilisateur RAM.

Linux et macOS

Remplacez <access_key_id> et <access_key_secret> par les identifiants de l'utilisateur RAM :

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 et ajoutez ALIBABA_CLOUD_ACCESS_KEY_ID et ALIBABA_CLOUD_ACCESS_KEY_SECRET en définissant respectivement votre AccessKey ID et votre AccessKey secret comme valeurs.

  2. Redémarrez Windows pour que les variables d'environnement prennent effet.

Fonctionnement

Le push de données avec le SDK Java s'effectue en trois étapes :

  1. Construisez chaque document sous la forme d'un objet Map et attribuez-lui une commande (ADD ou DELETE).

  2. Sérialisez tous les documents dans une chaîne JSONArray et chargez-la dans le tampon du client.

  3. Appelez documentClient.push() pour soumettre l'ensemble du lot à l'application en une seule fois.

Chaque lot ne peut contenir que des champs provenant d'une seule table. Les pushes inter-tables ne sont pas pris en charge.

Commandes prises en charge

Commande Comportement
ADD Ajoute un nouveau document. Si un document avec la même clé primaire existe déjà, effectue une mise à jour complète de ce document.
DELETE Supprime le document avec la clé primaire spécifiée. Seul le champ de clé primaire est requis.
UPDATE Effectue une mise à jour partielle. Pris en charge uniquement par les applications avancées. Les applications standard ne prennent en charge que ADD.

Format des données

Les jeux de données doivent respecter le format valide. Pour télécharger un fichier exemple servant de modèle :

  1. Connectez-vous à la console OpenSearch.

  2. Dans la colonne Actions de votre application, choisissez More > Upload File.

  3. Téléchargez le fichier exemple.

Si le nombre de documents dans un seul push dépasse la limite, le push échoue avec une erreur.

Push et interrogation des documents

L'exemple suivant pousse deux documents (doc1 et doc2), met à jour doc2 en utilisant doc3, supprime doc1, puis interroge les résultats pour vérification. Toutes les opérations utilisent documentClient.push() pour soumettre le lot.

package com.aliyun.opensearch;

import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Lists;
import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Maps;
import com.aliyun.opensearch.sdk.dependencies.org.json.JSONArray;
import com.aliyun.opensearch.sdk.dependencies.org.json.JSONObject;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchResult;
import com.aliyun.opensearch.sdk.generated.document.Command;
import com.aliyun.opensearch.sdk.generated.document.DocumentConstants;
import com.aliyun.opensearch.sdk.generated.search.*;
import com.aliyun.opensearch.sdk.generated.search.general.SearchResult;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Random;

public class testPushSearch2 {

    private String appName = "The name of the OpenSearch application to which you want to push data";
    private String tableName = "The name of the table to which you want to push data";
    private String host = "The endpoint of the OpenSearch API in your region";

    public static void main(String[] args) {
        // Read credentials from environment variables.
        // Set up the environment variables before running this code.
        // See the "Set up credentials" section for instructions.
        String accesskey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String secret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");

        // Print the default encoding format.
        System.out.println(String.format("file.encoding: %s", System.getProperty("file.encoding")));
        System.out.println(String.format("defaultCharset: %s", Charset.defaultCharset().name()));

        // Generate random values to use as primary keys.
        Random rand = new Random();
        int value1 = rand.nextInt(Integer.MAX_VALUE);
        int value2 = rand.nextInt(Integer.MAX_VALUE);

        // Build doc1.
        Map<String, Object> doc1 = Maps.newLinkedHashMap();
        doc1.put("id", value1);

        String title_string = "Add doc1 in push mode"; // UTF-8 encoded
        byte[] bytes;
        try {
            bytes = title_string.getBytes("utf-8");
            String utf8_string = new String(bytes, "utf-8");
            doc1.put("name", utf8_string);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        doc1.put("phone", "137****1111");

        int[] int_arr = {11, 11};
        doc1.put("int_arr", int_arr);

        String[] literal_arr1 = {"Add doc1 in push mode", "Test adding doc1 in push mode"};
        doc1.put("literal_arr", literal_arr1);

        float[] float_arr = {(float) 1.1, (float) 1.1};
        doc1.put("float_arr", float_arr);

        doc1.put("cate_id", 1);

        // Wrap doc1 in a JSONObject with the ADD command.
        // Standard applications do not support UPDATE or partial updates.
        // Use ADD to add a new document or fully replace an existing one.
        JSONObject json1 = new JSONObject();
        json1.put(DocumentConstants.DOC_KEY_CMD, Command.ADD.toString());
        json1.put(DocumentConstants.DOC_KEY_FIELDS, doc1);

        // Build doc2.
        Map<String, Object> doc2 = Maps.newLinkedHashMap();
        doc2.put("id", value2);

        String title_string2 = "Add doc2 in push mode"; // UTF-8 encoded
        byte[] bytes2;
        try {
            bytes2 = title_string2.getBytes("utf-8");
            String utf8_string2 = new String(bytes2, "utf-8");
            doc2.put("name", utf8_string2);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        doc2.put("phone", "137****2222");

        int[] int_arr2 = {22, 22};
        doc2.put("int_arr", int_arr2);

        String[] literal_arr2 = {"Add doc2 in push mode", "Test adding doc2 in push mode"};
        doc2.put("literal_arr", literal_arr2);

        float[] float_arr2 = {(float) 2.2, (float) 2.2};
        doc2.put("float_arr", float_arr2);

        doc2.put("cate_id", 1);

        // ADD adds doc2. If a document with value2 as the primary key already exists, it is fully replaced.
        JSONObject json2 = new JSONObject();
        json2.put(DocumentConstants.DOC_KEY_CMD, Command.ADD.toString());
        json2.put(DocumentConstants.DOC_KEY_FIELDS, doc2);

        // Build doc3, which updates doc2 (same primary key: value2).
        Map<String, Object> doc3 = Maps.newLinkedHashMap();
        doc3.put("id", value2);

        String title_string3 = "Update doc2 to doc3 in push mode"; // UTF-8 encoded
        byte[] bytes3;
        try {
            bytes3 = title_string3.getBytes("utf-8");
            String utf8_string3 = new String(bytes3, "utf-8");
            doc3.put("name", utf8_string3);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        doc3.put("phone", "137****3333");

        int[] int_arr3 = {33, 33};
        doc3.put("int_arr", int_arr3);

        String[] literal_arr3 = {"Update doc2 to doc3 in push mode", "Update doc2 to doc3 in push mode"};
        doc3.put("literal_arr", literal_arr3);

        float[] float_arr3 = {(float) 3.3, (float) 3.3};
        doc3.put("float_arr", float_arr3);

        doc3.put("cate_id", 1);

        // ADD with value2 as the primary key replaces doc2 with doc3.
        JSONObject json3 = new JSONObject();
        json3.put(DocumentConstants.DOC_KEY_CMD, Command.ADD.toString());
        json3.put(DocumentConstants.DOC_KEY_FIELDS, doc3);

        // Build a DELETE command for doc1. Only the primary key is required.
        Map<String, Object> doc4 = Maps.newLinkedHashMap();
        doc4.put("id", value1);

        JSONObject json4 = new JSONObject();
        json4.put(DocumentConstants.DOC_KEY_CMD, Command.DELETE.toString());
        json4.put(DocumentConstants.DOC_KEY_FIELDS, doc4);

        // Assemble the batch: add doc1, add doc2, update doc2 to doc3, delete doc1.
        JSONArray docsJsonArr = new JSONArray();
        docsJsonArr.put(json1); // Add doc1
        docsJsonArr.put(json2); // Add doc2
        docsJsonArr.put(json3); // Update doc2 to doc3
        docsJsonArr.put(json4); // Delete doc1
        String docsJson = docsJsonArr.toString();

        // Initialize the client.
        OpenSearch openSearch = new OpenSearch(accesskey, secret, host);
        OpenSearchClient serviceClient = new OpenSearchClient(openSearch);
        DocumentClient documentClient = new DocumentClient(serviceClient);

        try {
            // Push the batch.
            OpenSearchResult osr = documentClient.push(docsJson, appName, tableName);

            // A successful response means the push request was accepted without API-level errors.
            // The application may still encounter indexing errors after the push — for example,
            // if a field value cannot be converted to the expected type. Check the error logs
            // in the OpenSearch console to confirm that all documents were indexed successfully.
            if (osr.getResult().equalsIgnoreCase("true")) {
                System.out.println("Push accepted. Request ID: " + osr.getTraceInfo().getRequestId());
            } else {
                System.out.println("Push failed: " + osr.getTraceInfo());
            }

        } catch (OpenSearchException e) {
            e.printStackTrace();
        } catch (OpenSearchClientException e) {
            e.printStackTrace();
        }

        // Hibernate the thread for 1 second.
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Query the pushed documents.
        SearcherClient searcherClient = new SearcherClient(serviceClient);

        // Configure paging and the response format.
        Config config = new Config(Lists.newArrayList(appName));
        config.setStart(0);
        config.setHits(30);
        // Supported formats: XML and JSON. FULLJSON is not supported.
        config.setSearchFormat(SearchFormat.JSON);

        // Specify the fields to return.
        config.setFetchFields(Lists.newArrayList("id", "name", "phone", "int_arr", "literal_arr", "float_arr", "cate_id"));

        SearchParams searchParams = new SearchParams(config);

        // Query by primary key. Specify multiple values in a single setQuery call —
        // multiple setQuery calls overwrite each other.
        searchParams.setQuery("id:'" + value1 + "'|'" + value2 + "'");

        // Apply a filter condition.
        searchParams.setFilter("cate_id<=3");

        // Sort by id (descending), then by RANK (ascending) for documents with the same id.
        Sort sorter = new Sort();
        sorter.addToSortFields(new SortField("id", Order.DECREASE));
        sorter.addToSortFields(new SortField("RANK", Order.INCREASE));
        searchParams.setSort(sorter);

        SearchResult searchResult;
        try {
            searchResult = searcherClient.execute(searchParams);
            String result = searchResult.getResult();
            JSONObject obj = new JSONObject(result);
            System.out.println(obj.toString());

        } catch (OpenSearchException e) {
            e.printStackTrace();
        } catch (OpenSearchClientException e) {
            e.printStackTrace();
        }
    }
}

Compréhension du résultat du push

Un push réussi signifie que la requête a été acceptée sans erreur au niveau de l'API. Après l'acceptation, l'application peut toujours rencontrer des erreurs d'indexation — par exemple, si une valeur de champ ne peut pas être convertie dans le type attendu. Consultez les journaux d'erreurs dans la console OpenSearch pour confirmer que tous les documents ont été indexés avec succès.

Étapes suivantes

Après avoir poussé les données, configurez les paramètres de pertinence et de classement de votre application pour améliorer la qualité des résultats de recherche.