このドキュメントでは、Elasticsearch Java API の使用方法について説明します。 Java High Level REST Client 6.3.x を例として使用します。

前提条件

  • JDK がインストールされていること。 JDK バージョンは 1.8 以降である必要があります。
  • Alibaba Cloud Elasticsearch インスタンスが作成されていること。 インスタンスのバージョンは、 High Level Client のバージョン以上である必要があります。 このドキュメントでは、バージョン 6.3.2 のインスタンスが作成されます。
    重要 High Level Client は上位互換性があります。 たとえば、バージョン 6.3.2 の High Level Client は、バージョン 6.3.2 以降の Elasticsearch クラスターと通信できます。 最新のクライアントの機能を使用できるようにするには、クラスターバージョンと同じ高レベルのクライアントバージョンを使用することを推奨します。
  • Java Maven プロジェクトを作成し、 Pom 依存関係 を Java プロジェクトの pom.xml ファイルに追加していること。pom 依存関係の追加
    このドキュメントでは、Elasticsearch 6.3.2 を例として使用しています。

Pom 依存関係

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>6.3.2</version>
</dependency>

次のサンプルコードは、 index API を使用してドキュメントのインデックスを作成し、 delete API を使用してドキュメントを削除します。

// The file path is src/main/java/RestClientTest.java.
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;

import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class RestClientTest {
    public static void main(String[] args) {

        // The Alibaba Cloud Elasticsearch cluster requires basic authentication.
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
       // Use the username and password that you have specified when creating the Alibaba Cloud Elasticsearch instance. The username and password are also used to log on to the Kibana console.
        credentialsProvider.setCredentials(AuthScope.ANY、new UsernamePasswordCredentials( "{username}"、 "{password}"));

       // Create a REST client by using the builder and configure HttpClientConfigCallback for the HTTP client.
       // Click the ID of the Elasticsearch instance. On the Basic Information page, obtain the public IP address of the Elasticsearch cluster.
        RestClientBuilder builder = RestClient.builder(new HttpHost("{Elasticsearch cluster address}", 9200))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                });

        // Create a RestHighLevelClient instance by using the REST low-level client builder.
        RestHighLevelClient highClient = new RestHighLevelClient(builder);

        try {
            // Create a request.
            Map<String, Object> jsonMap = new HashMap<>();
           // field_01 and field_02 are the field names, and value_01 and value_02 are the corresponding values.
            jsonMap.put("{field_01}", "{value_01}");
            jsonMap.put("{field_02}", "{value_02}");
            // index_name is the index name, type_name is the type name, and doc_id is the document ID.       
            IndexRequest indexRequest = new IndexRequest("{index_name}", "{type_name}", "{doc_id}").source(jsonMap);

            // Run the following command in parallel.
            IndexResponse indexResponse = highClient.index(indexRequest);

            long version = indexResponse.getVersion();

            System.out.println("Index document successfully! " + version);
            // index_name is the index name, type_name is the type name, and doc_id is the document ID. The index name, type name, and document ID are the same as that you have specified when creating the index.
            DeleteRequest request = new DeleteRequest("{index_name}", "{type_name}", "{doc_id}");
            DeleteResponse deleteResponse = highClient.delete(request);

            System.out.println("Delete document successfully!") ;

            highClient.close();

        } catch (IOException ioException) {
            // Handle exceptions.
        }
    }
}

中かっこ {} 内に含まれる前述のサンプルコードのパラメーターを、サービス固有のパラメーターに置き換えることができます。 詳細については、コードのコメントをご参照ください。