ES学习笔记--4

集成SpringBoot

找官方文档:https://www.elastic.co/guide/index.html

选择ElasticSearch Client,推荐使用JAVA REST Client的high level版本

1、找到了原生的依赖

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

来自 https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-maven.html

2、找对象

Initialization  to  bolder. elient a  so close with it it  will in  to This  In of 'nis Hign Leæl tN

3、分析这个类中的方法

配置基本的项目

注意问题:一定要保证 我们的导入的依赖和我们的es 版本一致

image-20210917150715653

源码中提供对象!

虽然这里导入3个类,静态内部类,核心类就一个!
/*
 * Copyright 2012-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure.elasticsearch.rest;

import java.time.Duration;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Elasticsearch rest client infrastructure configurations.
 *
 * @author Brian Clozel
 * @author Stephane Nicoll
 */
class RestClientConfigurations {

   @Configuration(proxyBeanMethods = false)
   static class RestClientBuilderConfiguration {

      @Bean
      @ConditionalOnMissingBean
      RestClientBuilder elasticsearchRestClientBuilder(RestClientProperties properties,
            ObjectProvider<RestClientBuilderCustomizer> builderCustomizers) {
         HttpHost[] hosts = properties.getUris().stream().map(HttpHost::create).toArray(HttpHost[]::new);
         RestClientBuilder builder = RestClient.builder(hosts);
         PropertyMapper map = PropertyMapper.get();
         map.from(properties::getUsername).whenHasText().to((username) -> {
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            Credentials credentials = new UsernamePasswordCredentials(properties.getUsername(),
                  properties.getPassword());
            credentialsProvider.setCredentials(AuthScope.ANY, credentials);
            builder.setHttpClientConfigCallback(
                  (httpClientBuilder) -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
         });
         builder.setRequestConfigCallback((requestConfigBuilder) -> {
            map.from(properties::getConnectionTimeout).whenNonNull().asInt(Duration::toMillis)
                  .to(requestConfigBuilder::setConnectTimeout);
            map.from(properties::getReadTimeout).whenNonNull().asInt(Duration::toMillis)
                  .to(requestConfigBuilder::setSocketTimeout);
            return requestConfigBuilder;
         });
         builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
         return builder;
      }

   }

   @Configuration(proxyBeanMethods = false)
   @ConditionalOnClass(RestHighLevelClient.class)
   static class RestHighLevelClientConfiguration {

      @Bean
      @ConditionalOnMissingBean
      RestHighLevelClient elasticsearchRestHighLevelClient(RestClientBuilder restClientBuilder) {
         return new RestHighLevelClient(restClientBuilder);
      }

      @Bean
      @ConditionalOnMissingBean
      RestClient elasticsearchRestClient(RestClientBuilder builder,
            ObjectProvider<RestHighLevelClient> restHighLevelClient) {
         RestHighLevelClient client = restHighLevelClient.getIfUnique();
         if (client != null) {
            return client.getLowLevelClient();
         }
         return builder.build();
      }

   }

   @Configuration(proxyBeanMethods = false)
   static class RestClientFallbackConfiguration {

      @Bean
      @ConditionalOnMissingBean
      RestClient elasticsearchRestClient(RestClientBuilder builder) {
         return builder.build();
      }

   }

}

4、具体的api测试

索引api:

  1. 创建索引
  2. 判断索引是否存在
  3. 删除索引

image-20210917151024129

文档api:

  1. 创建文档
  2. CRUD文档
package com.supjh;

import com.alibaba.fastjson.JSON;
import com.supjh.pojo.User;
import com.supjh.utils.ESConst;
import org.apache.lucene.util.QueryBuilder;
import org.assertj.core.data.Index;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import javax.naming.directory.SearchResult;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

/**
 * @Author: supjh22
 * @Date: 16/9/2021 下午 8:22
 * @Description: es7.14.x 高级客户端测试 API
*/
@SpringBootTest
class SupjhEsApiApplicationTests {

    //按照类型匹配
    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    // 测试索引的创建 Request
    @Test
    void testCreateIndex() throws IOException {
        // 1、创建索引请求
        CreateIndexRequest request = new CreateIndexRequest("supjh_index");
        // 2、客户端执行请求 IndicesClient,请求后获得相应
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);

        System.out.println(response);
    }

    // 测试获取索引,判断其是否存在
    @Test
    void testExistIndex() throws IOException{
        GetIndexRequest request = new GetIndexRequest("supjh_index");
        boolean exists = client.indices().exists(request,RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    // 测试删除索引
    @Test
    void testDeleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("supjh_index");
        // 删除
        AcknowledgedResponse delete = client.indices().delete(request,RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());
    }

    // 测试添加文档
    @Test
    void testAddDocument() throws IOException {
        // 创建对象
        User user = new User("SuperJH",22);
        System.out.println(user);
        // 创建请求
        IndexRequest request = new IndexRequest("supjh_index");

        // 规则 put /supjh_index/_doc/1
        request.id("1");
        // 超时格式二选一
        request.timeout(TimeValue.timeValueSeconds(1));
        request.timeout("1s");

        // 将我们的数据放入请求 json
        request.source(JSON.toJSONString(user), XContentType.JSON);

        // 客户端发送请求,获取响应的结果
        IndexResponse indexResponse = client.index(request,RequestOptions.DEFAULT);

        System.out.println(indexResponse.toString());// 返回具体索引信息
        System.out.println(indexResponse.status());// 对应我们命令返回的状态

    }
    // 获取文档,判断是否存在
    @Test
    void testIsExists() throws IOException {
        GetRequest getRequest = new GetRequest("supjh_index", "1");
        // 不获取返回的_source的上下文了,增加性能
        getRequest.fetchSourceContext(new FetchSourceContext(false));
        getRequest.storedFields("_none_");

        boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    // 获取文档的信息
    @Test
    void testGetDocument() throws IOException {
        GetRequest getRequest = new GetRequest("supjh_index", "1");
        GetResponse getResponse = client.get(getRequest,RequestOptions.DEFAULT);
        System.out.println(getResponse.getSourceAsString());// 打印文档的内容
        System.out.println(getResponse);// 返回的全部内容和命令是一样的
    }

    // 更新文档的信息
    @Test
    void testUpdateRequest() throws IOException {
        UpdateRequest updateRequest = new UpdateRequest("supjh_index","1");
        updateRequest.timeout("1s");

        User user = new User("SuperJH",2);
        updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);

        UpdateResponse updateResponse = client.update(updateRequest,RequestOptions.DEFAULT);
        System.out.println(updateResponse.status());
    }

    // 删除文档
    @Test
    void testDeleteRequest() throws IOException {
        DeleteRequest request = new DeleteRequest("supjh_index","1");
        request.timeout("1s");

        DeleteResponse deleteResponse = client.delete(request,RequestOptions.DEFAULT);
        System.out.println(deleteResponse.status());
    }

    // 特殊的,批量插入数据!
    @Test
    void testBulkRequest() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("10s");

        ArrayList<User> userList = new ArrayList<>();
        userList.add(new User("SuperJH",22));
        userList.add(new User("Kiko",2));
        userList.add(new User("Riven",5));
        userList.add(new User("zjh",21));
        userList.add(new User("FF",14));
        userList.add(new User("llw",1));

        // 批处理请求
        for (int i = 0; i < userList.size(); i++) {
            // 批量更新和批量删除,就在这里修改对应的请求既可以了
            bulkRequest.add(
                    new IndexRequest("supjh_index")
                    .id(""+(i+1)).//可以自己设置id,也可以不设置id随机生成,大数据情况下都是随机生成,性能更高
                    source(JSON.toJSONString(userList.get(i)),XContentType.JSON));
        }

        BulkResponse bulkIResponse = client.bulk(bulkRequest,RequestOptions.DEFAULT);
        System.out.println(bulkIResponse.hasFailures());// 是否失败,返回false表示成功
    }

    // 查询
    // SearchRequest搜索请求
    // SearchSourceBuilder条件构造
    // HighlightBuilder 构造高亮
    // MatchAllQueryBuilder
    // xxx QueryBuilder 对应所有的命令
    @Test
    void testSearch() throws IOException {
        SearchRequest searchRequest = new SearchRequest(ESConst.ES_INDEX);
        // 构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        // 查询条件,我们可以使用QueryBuilders 工具实现
        // 精确
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "Kiko");
        // 匹配所有
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();

        // 分页    sourceBuilder.from(); sourceBuilder.size();  高亮 highlight
        sourceBuilder.query(matchAllQueryBuilder);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        searchRequest.source(sourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(searchResponse.getHits()));
        System.out.println("=========================================");
        for (SearchHit documentFields : searchResponse.getHits().getHits()) {
            System.out.println(documentFields.getSourceAsMap());
        }
    }
}

剩下就是实战了!
完结撒花~

posted @ 2021-09-17 15:12  SuperJH  阅读(70)  评论(0)    收藏  举报