[elastic 8.x]java客户端连接elasticsearch与操作索引与文档

文章详细介绍了如何使用ElasticsearchJava客户端进行初始化、连接安全验证的HTTPS集群以及处理索引、文档操作(如创建、删除、查询、更新和批量导入),同时展示了两种不同的认证方式:基本凭证和APIKey。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

初始化客户端

引入相关依赖

<dependency>
    <groupId>co.elastic.clients</groupId>
    <artifactId>elasticsearch-java</artifactId>
    <version>8.10.2</version>
</dependency>

初始化客户端

为了方便演示,我关闭了elasticsearch的安全验证,带安全验证的初始化方式将在最后专门介绍

String serverUrl="http://127.0.0.1:9200";
RestClient restClient=RestClient
                .builder(HttpHost
                .create(serverUrl))
                .build();
ElasticsearchTransport transport=new RestClientTransport(restClient,new JacksonJsonpMapper());
ElasticsearchClient esClient=new ElasticsearchClient(transport);

索引

创建索引

    void createIndex(){

        String mappings = "{\n" +
                "  \"properties\" : {\n" +
                "    \"id\" : {\n" +
                "      \"type\" : \"keyword\" \n" +
                "    },\n"+
                "    \"name\" : {\n" +
                "      \"type\" : \"text\",\n" +
                "      \"fields\" : {\n" +
                "        \"keyword\" : {\n" +
                "          \"type\" : \"keyword\",\n" +
                "          \"ignore_above\" : 256 \n" +
                "        }\n" +
                "      } \n" +
                "    }, \n" +
                "    \"price\" : { \n" +
                "      \"type\" : \"long\" \n" +
                "     } \n" +
                "  }\n" +
                "}\n";
        JsonpMapper mapper=esClient._transport().jsonpMapper();
        JsonParser parser= Json.createParser(new StringReader(mappings));
        CreateIndexRequest createIndexRequest=new CreateIndexRequest.Builder().index("test")
                .mappings(TypeMapping._DESERIALIZER.deserialize(parser,mapper)).build();
        try {
            esClient.indices().create(createIndexRequest);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

删除索引

    void deleteMapping(){
        try {
            DeleteIndexResponse response;
            response= esClient.indices().delete(deleteIndexRequest->deleteIndexRequest.index("test"));
            System.out.println(response.toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

判断索引是否存在

    
    void existsIndex(){
        try {
            BooleanResponse hotel = esClient.indices().exists(existsIndexRequest -> existsIndexRequest.index("hotel"));
            System.out.println(hotel.value());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

文档

新增文档

    void insertDoc(){
        Hotel hotel=hotelService.getById(61083L);
        HotelDoc hotelDoc=new HotelDoc(hotel);
        IndexRequest<HotelDoc> request=new IndexRequest.Builder<HotelDoc>()
                .id("11")
                .index("hotel")
                .document(hotelDoc)
                .build();
        try {
            IndexResponse index = esClient.index(request);
            System.out.println(index.id());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

其中,HotelDoc是一个实体类

删除文档

    void deleteDoc(){
        try {
            esClient.delete(deleteRequest->deleteRequest.index("hotel").id("11"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

查询文档

    void searchDoc(){
        TermQuery termQuery= QueryBuilders.term()
                .field("_id")
                .value("11")
                .build();
        SearchRequest request=new SearchRequest.Builder()
                        .index("hotel")
                        .query(termQuery._toQuery()).build();
        try {
            SearchResponse<HotelDoc> response=esClient.search(request,HotelDoc.class);
            
            //输出结果
            for(Hit<HotelDoc> hit:response.hits().hits()){
                System.out.println(hit.source());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

更新文档

    void updateDoc(){
        HotelDoc hotelDoc=new HotelDoc();
        //需要更新哪个字段就赋值哪个字段
        hotelDoc.setCity("xx");
        try {
            esClient.update(updateRequest->updateRequest
                            .index("hotel")
                            .id("11")
                            .doc(hotelDoc)
                    ,HotelDoc.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

批量导入文档

    void insertMany(){
        List<Hotel> hotels=hotelService.list();
        List<HotelDoc> hotelDocs=hotels.stream().map(HotelDoc::new).collect(Collectors.toList());
        BulkRequest.Builder bl=new BulkRequest.Builder();
        for(HotelDoc hotelDoc:hotelDocs){
            bl.operations(op->op.index(
                    idx->idx.index("hotel")
                            .id(hotelDoc.getId().toString())
                            .document(hotelDoc)
            ));
        }
        try {
            esClient.bulk(bl.refresh(Refresh.WaitFor).build());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

连接Https集群

带安全验证的连接有点复杂,将下列代码中CA证书的位置改为实际所在的位置就行了。

通过用户名密码连接

password为elastic的密码,可以在我的另一篇文章中查看密码的重置方式
Docker安装部署[8.x]版本Elasticsearch+Kibana+IK分词器

    void makeConnection_https() throws CertificateException, IOException,
            NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        // 创建凭据提供器
        final CredentialsProvider credentialsProvider =
                new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("elastic", password));

        // 设置CA证书路径
        Path caCertificatePath = Paths.get("E:\\tools\\elasticsearch-8.10.2\\config\\certs\\http_ca.crt");
        // 创建证书工厂
        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);
        // 创建SSL上下文构建器
        SSLContextBuilder sslContextBuilder = SSLContexts.custom()
                .loadTrustMaterial(trustStore, null);
        final SSLContext sslContext = sslContextBuilder.build();

        // 构建Rest客户端构建器
        RestClientBuilder builder = RestClient.builder(
                        new HttpHost("localhost", 9200, "https"))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(
                            HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setSSLContext(sslContext)
                                .setDefaultCredentialsProvider(credentialsProvider);
                    }
                });

        // 构建Rest客户端
        RestClient restClient = builder.build();

        // 创建Rest客户端传输
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());
        esClient = new ElasticsearchClient(transport);
//        asyncClient = new ElasticsearchAsyncClient(transport);
    }

通过ApiKey连接

ApiKey在Kibana的Security下生成

     void makeConnection_token() throws CertificateException, IOException,
            NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        // 定义CA证书路径
        Path caCertificatePath = Paths.get("E:\\tools\\elasticsearch-8.10.2\\config\\certs\\http_ca.crt");
        // 创建X.509证书工厂
        CertificateFactory factory =
                CertificateFactory.getInstance("X.509");
        Certificate trustedCa;
        try (InputStream is = Files.newInputStream(caCertificatePath)) {
            // 从输入流中生成X.509证书
            trustedCa = factory.generateCertificate(is);
        }
        // 创建PKCS12密钥库
        KeyStore trustStore = KeyStore.getInstance("pkcs12");
        trustStore.load(null, null);
        // 将CA证书添加到密钥库
        trustStore.setCertificateEntry("ca", trustedCa);
        // 创建SSL上下文构建器,并设置信任材料
        SSLContextBuilder sslContextBuilder = SSLContexts.custom()
                .loadTrustMaterial(trustStore, null);
        final SSLContext sslContext = sslContextBuilder.build();

        // 创建Rest客户端构建器
        RestClientBuilder builder = RestClient.builder(
                        new HttpHost("localhost", 9200, "https"))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(
                            HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setSSLContext(sslContext);
                    }
                });

        // 设置默认请求头
        Header[] defaultHeaders =
                new Header[]{new BasicHeader("Authorization",
                        "ApiKey yourApiKey")};
        builder.setDefaultHeaders(defaultHeaders);

        // 构建Rest客户端
        RestClient restClient = builder.build();

        // 创建基于RestClient的传输方式
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        // 创建Elasticsearch客户端
        esClient = new ElasticsearchClient(transport);
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辰宝IWZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值