ElasticSearch 8 学习笔记总结

Posted IT_Holmes

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ElasticSearch 8 学习笔记总结相关的知识,希望对你有一定的参考价值。

文章目录

一. ES8 的Java API 环境准备

ES8 废除了Type的概念。为了适应这种数据结构的改变,ES官方从1.7版本开始建议使用新的Elasticsearch Java Client。


搭建maven环境:

<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <elastic.version>8.6.2</elastic.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.elasticsearch.plugin</groupId>
        <artifactId>x-pack-sql-jdbc</artifactId>
        <version>8.6.2</version>
    </dependency>
    <dependency>
        <groupId>co.elastic.clients</groupId>
        <artifactId>elasticsearch-java</artifactId>
        <version>$elastic.version</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.12.3</version>
    </dependency>
    <dependency>
        <groupId>jakarta.json</groupId>
        <artifactId>jakarta.json-api</artifactId>
        <version>2.0.1</version>
    </dependency>
</dependencies>

获取客户端对象:

因为,服务注册的是基于https的安全elasticsearch服务认证,所以,将之前的证书进行一个转换:

# 生成es-api-ca.crt证书,之前没有密码设置。
openssl pkcs12 -in elastic-stack-ca.p12 -clcerts -nokeys -out es-api-ca.crt

将生成的证书放到项目里面。

创建连接对象:

package com.itholmes.elasticsearch.api;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;

import javax.net.ssl.SSLContext;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;

public class ESClient 

    public static void main(String[] args) throws Exception
        // 初始化ES服务器的连接
        initESConnection();
    

    public static void initESConnection() throws Exception
        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        // 声明ES当前登录的账号密码
        credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("elastic","3j=JYpywv=jMtQB+XIXS"));
        // 证书的路径
        Path caCertificatePath = Paths.get("E:\\\\itholmes\\\\demo\\\\itholmes-es8\\\\certs\\\\es-api-ca.crt");
        // x.509 , pkcs12都是 证书的算法
        CertificateFactory factory = CertificateFactory.getInstance("x.509");
        Certificate trustedCa;
        try (InputStream is = Files.newInputStream(caCertificatePath))
            trustedCa = factory.generateCertificate(is);
        
        KeyStore trustStore = KeyStore.getInstance("pkcs12");
        trustStore.load(null,null);
        trustStore.setCertificateEntry("ca",trustedCa);
        SSLContextBuilder sslContextBuilder = SSLContexts.custom()
                .loadTrustMaterial(trustStore,null);
        SSLContext sslContext = sslContextBuilder.build();
        RestClientBuilder builder = RestClient.builder(new HttpHost("192.168.43.133", 9200, "https"))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() 
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) 
                        return httpClientBuilder.setSSLContext(sslContext).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                                .setDefaultCredentialsProvider(credentialsProvider);
                    
                );

        // 创建客户端
        RestClient restClient = builder.build();
        // 通过客户端,创建传输对象
        RestClientTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

        // 同步客户端对象
        ElasticsearchClient client = new ElasticsearchClient(transport);
        // 异步客户端对象
        ElasticsearchAsyncClient asyncClient = new ElasticsearchAsyncClient(transport);

        // fixme 同步是需要关闭的,而异步是不需要关闭的。
        transport.close();
    


二. ES8 的Java API 索引操作

ES Java的API相关操作:

  • 采用构造器形式来创建所需要的对象。
  • 通过lambda来创建。

对象形式操作:

package com.itholmes.elasticsearch.api;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.indices.*;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import javax.net.ssl.SSLContext;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;

public class ESClient 

    private static ElasticsearchClient client;

    private static ElasticsearchAsyncClient asyncClient;

    private static ElasticsearchTransport transport;

    public static final String INDEX_HOLMES = "itholmes";

    public static void main(String[] args) throws Exception
        // 初始化ES服务器的连接
        initESConnection();
        // 操作索引
        operationIndex();
    
    // 操作索引
    private static void operationIndex() throws Exception
        // 获取索引客户端对象
        ElasticsearchIndicesClient indices = client.indices();
        // 判断索引是否存在
        ExistsRequest existsRequest = new ExistsRequest.Builder().index(INDEX_HOLMES).build();
        final boolean flg = indices.exists(existsRequest).value();
        if (flg)
            System.out.println("索引" + INDEX_HOLMES + "已经存在!");
         else 
            // 创建索引
            // CreateIndexRequest构造方法已经私有化了,所以需要采用构建器方式来构建对象。ES的API对象基本上都采用构建器的方式创建对象。
            CreateIndexRequest request = new CreateIndexRequest.Builder()
                    .index(INDEX_HOLMES)
                    .build();
            final CreateIndexResponse createIndexResponse = indices.create(request);
            System.out.println("创建索引的响应对象" + createIndexResponse);
        
        // 查询索引
        GetIndexRequest getIndexRequest = new GetIndexRequest.Builder().index(INDEX_HOLMES).build();
        final GetIndexResponse getIndexResponse = indices.get(getIndexRequest);
        // IndexState itholmes = getIndexResponse.get("itholmes");
        System.out.println("查询的响应结果:" + getIndexResponse);
        // 删除索引
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest.Builder().index(INDEX_HOLMES).build();
        DeleteIndexResponse delete = indices.delete(deleteIndexRequest);
        System.out.println("索引删除成功:" + delete);
        // fixme 同步是需要关闭的,而异步是不需要关闭的。
        transport.close();
    

    public static void initESConnection() throws Exception
        final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        // 声明ES当前登录的账号密码
        credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("elastic","3j=JYpywv=jMtQB+XIXS"));
        // 证书的路径
        Path caCertificatePath = Paths.get("E:\\\\itholmes\\\\demo\\\\itholmes-es8\\\\certs\\\\es-api-ca.crt");
        // x.509 , pkcs12都是 证书的算法
        CertificateFactory factory = CertificateFactory.getInstance("x.509");
        Certificate trustedCa;
        try (InputStream is = Files.newInputStream(caCertificatePath))
            trustedCa = factory.generateCertificate(is);
        
        KeyStore trustStore = KeyStore.getInstance("pkcs12");
        trustStore.load(null,null);
        trustStore.setCertificateEntry("ca",trustedCa);
        SSLContextBuilder sslContextBuilder = SSLContexts.custom()
                .loadTrustMaterial(trustStore,null);
        SSLContext sslContext = sslContextBuilder.build();
        RestClientBuilder builder = RestClient.builder(new HttpHost("192.168.43.133", 9200, "https"))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() 
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) 
                        return httpClientBuilder.setSSLContext(sslContext).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                                .setDefaultCredentialsProvider(credentialsProvider);
                    
                );

        // 创建客户端
        RestClient restClient = builder.build();
        // 通过客户端,创建传输对象
        transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

        // 同步客户端对象
        client = new ElasticsearchClient(transport);
        // 异步客户端对象
        asyncClient = new ElasticsearchAsyncClient(transport);
    


lambda方式创建索引:(推荐使用,代码简洁)

package com.itholmes.elasticsearch.api;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.indices.*;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import co.elastic.clients.util.ObjectBuilder;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import javax.net.ssl.SSLContext;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.ElasticSearch相关概念总结

Elasticsearch总结笔记

ElasticSearch NEST笔记

ElasticSearch NEST笔记

〈二〉ElasticSearch的认识:索引类型文档

ElasticSearch 学习笔记总结