热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringBoot 整合 Elasticsearch 实现海量级数据搜索功能【java面试】

这篇文章主要介绍了SpringBoot 整合 Elasticsearch 实现海量级数据搜索,本文主要围绕 SpringBoot 整合 ElasticSearch 接受数据的插入和

SpringBoot 整合 Elasticsearch 实现海量级数据搜索功能

今天给大家讲讲 SpringBoot 框架 整合 Elasticsearch 实现海量级数据搜索。

一、简介

在上篇ElasticSearch 文章中,我们详细的介绍了 ElasticSearch 的各种 api 使用。

实际的项目开发过程中,我们通常基于某些主流框架平台进行技术开发,比如 SpringBoot,今天我们就以 SpringBoot 整合 ElasticSearch 为例,给大家详细的介绍 ElasticSearch 的使用!

SpringBoot 连接 ElasticSearch,主流的方式有以下四种方式

  • 方式一:通过 Elastic Transport Client 客户端连接 es 服务器,底层基于 TCP 协议通过 transport 模块和远程 ES 服务端通信,不过,从 V7.0 开始官方不建议使用,V8.0开始正式移除。

  • 方式二:通过 Elastic Java Low Level Rest Client 客户端连接 es 服务器,底层基于 HTTP 协议通过 restful API 来和远程 ES 服务端通信,只提供了最简单最基本的 API,类似于上篇文章中给大家介绍的 API 操作逻辑

  • Elastic Java High Level Rest Client
    Elastic Java Low Level Rest Client
    Elastic Transport Client
  • 方式四:通过 JestClient 客户端连接 es 服务器,这是开源社区基于 HTTP 协议开发的一款 es 客户端,官方宣称接口及代码设计比 ES 官方提供的 Rest 客户端更简洁、更合理,更好用,具有一定的 ES 服务端版本兼容性,但是更新速度不是很快,目前 ES 版本已经出到 V7.9,但是 JestClient 只支持 V1.0~V6.X 版  本的 ES。

还有一个需要大家注意的地方,那就是版本号的兼容!

在开发过程中,大家尤其需要关注一下客户端和服务端的版本号,要尽可能保持一致,比如服务端 es 的版本号是 6.8.2 ,那么连接 es 的客户端版本号,最好也是 6.8.2 ,即使因项目的原因不能保持一致,客户端的版本号必须在 6.0.0 ~6.8.2 ,不要超过服务器的版本号,这样客户端才能保持正常工作,否则会出现很多意想不到的问题,假如客户端是 7.0.4 的版本号,此时的程序会各种报错,甚至没办法用!

为什么要这样做呢?主要原因就是 es 的服务端,高版本不兼容低版本;es6 和 es7 的某些 API 请求参数结构有着很大的区别,所以客户端和服务端版本号尽量保持一致。

废话也不多说了,直接上代码!

二、代码实践

本文采用的 SpringBoot 版本号是 2.1.0.RELEASE ,服务端 es 的版本号是 6.8.2 ,客户端采用的是官方推荐的 Elastic Java High Level Rest Client 版本号是 6.4.2 ,方便与 SpringBoot 的版本兼容。

2.1、导入依赖


    org.elasticsearch
    elasticsearch
    6.4.2


    org.elasticsearch.client
    elasticsearch-rest-client
    6.4.2


    org.elasticsearch.client
    elasticsearch-rest-high-level-client
    6.4.2

2.2、配置环境变量

在 application.properties 全局配置文件中,配置 elasticsearch 自定义环境变量

elasticsearch.scheme=http
elasticsearch.address=127.0.0.1:9200
elasticsearch.userName=
elasticsearch.userPwd=
elasticsearch.socketTimeout=5000
elasticsearch.cOnnectTimeout=5000
elasticsearch.cOnnectionRequestTimeout=5000

2.3、创建 elasticsearch 的 config 类

@Configuration
public class ElasticsearchConfiguration {
    private static final Logger log = LoggerFactory.getLogger(ElasticsearchConfiguration.class);
    private static final int ADDRESS_LENGTH = 2;
    @Value("${elasticsearch.scheme:http}")
    private String scheme;
    @Value("${elasticsearch.address}")
    private String address;
    @Value("${elasticsearch.userName}")
    private String userName;
    @Value("${elasticsearch.userPwd}")
    private String userPwd;
    @Value("${elasticsearch.socketTimeout:5000}")
    private Integer socketTimeout;
    @Value("${elasticsearch.connectTimeout:5000}")
    private Integer connectTimeout;
    @Value("${elasticsearch.connectionRequestTimeout:5000}")
    private Integer connectionRequestTimeout;
    /**
     * 初始化客户端
     * @return
     */
    @Bean(name = "restHighLevelClient")
    public RestHighLevelClient restClientBuilder() {
        HttpHost[] hosts = Arrays.stream(address.split(","))
                .map(this::buildHttpHost)
                .filter(Objects::nonNull)
                .toArray(HttpHost[]::new);
        RestClientBuilder restClientBuilder = RestClient.builder(hosts);
        // 异步参数配置
        restClientBuilder.setHttpClientConfigCallback(httpClientBuilder -> {
            httpClientBuilder.setDefaultCredentialsProvider(buildCredentialsProvider());
            return httpClientBuilder;
        });
        // 异步连接延时配置
        restClientBuilder.setRequestConfigCallback(requestConfigBuilder -> {
            requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeout);
            requestConfigBuilder.setSocketTimeout(socketTimeout);
            requestConfigBuilder.setConnectTimeout(connectTimeout);
            return requestConfigBuilder;
        });
        return new RestHighLevelClient(restClientBuilder);
    }
    /**
     * 根据配置创建HttpHost
     * @param s
     * @return
     */
    private HttpHost buildHttpHost(String s) {
        String[] address = s.split(":");
        if (address.length == ADDRESS_LENGTH) {
            String ip = address[0];
            int port = Integer.parseInt(address[1]);
            return new HttpHost(ip, port, scheme);
        } else {
            return null;
        }
    }
    /**
     * 构建认证服务
     * @return
     */
    private CredentialsProvider buildCredentialsProvider(){
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName,
                userPwd));
        return credentialsProvider;
    }
}

至此,客户端配置完毕,项目启动的时候,会自动注入到 Spring 的 ioc 容器里面。

2.4、索引管理

es 中最重要的就是索引库,客户端如何创建呢?请看下文!

  • 创建索引

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class IndexJunit {
    @Autowired
    private RestHighLevelClient client;
    /**
     * 创建索引(简单模式)
     * @throws IOException
     */
    @Test
    public void createIndex() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("cs_index");
        CreateIndexResponse respOnse= client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(response.isAcknowledged());
    }
    /**
     * 创建索引(复杂模式)
     * 可以直接把对应的文档结构也一并初始化
     * @throws IOException
     */
    @Test
    public void createIndexComplete() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest();
        //索引名称
        request.index("cs_index");
        //索引配置
        Settings settings = Settings.builder()
                .put("index.number_of_shards", 3)
                .put("index.number_of_replicas", 1)
                .build();
        request.settings(settings);
        //映射结构字段
        Map properties = new HashMap();
        properties.put("id", ImmutableBiMap.of("type", "text"));
        properties.put("name", ImmutableBiMap.of("type", "text"));
        properties.put("sex", ImmutableBiMap.of("type", "text"));
        properties.put("age", ImmutableBiMap.of("type", "long"));
        properties.put("city", ImmutableBiMap.of("type", "text"));
        properties.put("createTime", ImmutableBiMap.of("type", "long"));
        Map mapping = new HashMap<>();
        mapping.put("properties", properties);
        //添加一个默认类型
        System.out.println(JSON.toJSONString(request));
        request.mapping("_doc",mapping);
        CreateIndexResponse respOnse= client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(response.isAcknowledged());
    }
}
  • 删除索引
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class IndexJunit {
    @Autowired
    private RestHighLevelClient client;
    /**
     * 删除索引
     * @throws IOException
     */
    @Test
    public void deleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("cs_index1");
        AcknowledgedResponse respOnse= client.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println(response.isAcknowledged());
    }
}
  • 查询索引

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class IndexJunit {
    @Autowired
    private RestHighLevelClient client;
    /**
     * 查询索引
     * @throws IOException
     */
    @Test
    public void getIndex() throws IOException {
        // 创建请求
        GetIndexRequest request = new GetIndexRequest();
        request.indices("cs_index");
        // 执行请求,获取响应
        GetIndexResponse respOnse= client.indices().get(request, RequestOptions.DEFAULT);
        System.out.println(response.toString());
    }
}
  • 查询索引是否存在

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class IndexJunit {
    @Autowired
    private RestHighLevelClient client;
    /**
     * 检查索引是否存在
     * @throws IOException
     */
    @Test
    public void exists() throws IOException {
        // 创建请求
        GetIndexRequest request = new GetIndexRequest();
        request.indices("cs_index");
        // 执行请求,获取响应
        boolean respOnse= client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(response);
    }
}
  • 查询所有的索引名称

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class IndexJunit {
    @Autowired
    private RestHighLevelClient client;
    /**
     * 查询所有的索引名称
     * @throws IOException
     */
    @Test
    public void getAllIndices() throws IOException {
        GetAliasesRequest request = new GetAliasesRequest();
        GetAliasesResponse respOnse=  client.indices().getAlias(request,RequestOptions.DEFAULT);
        Map> map = response.getAliases();
        Set indices = map.keySet();
        for (String key : indices) {
            System.out.println(key);
        }
    }
}
  • 查询索引映射字段

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class IndexJunit {
    @Autowired
    private RestHighLevelClient client;
    /**
     * 查询索引映射字段
     * @throws IOException
     */
    @Test
    public void getMapping() throws IOException {
        GetMappingsRequest request = new GetMappingsRequest();
        request.indices("cs_index");
        request.types("_doc");
        GetMappingsResponse respOnse= client.indices().getMapping(request, RequestOptions.DEFAULT);
        System.out.println(response.toString());
    }
}
  • 添加索引映射字段

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class IndexJunit {
    @Autowired
    private RestHighLevelClient client;
    /**
     * 添加索引映射字段
     * @throws IOException
     */
    @Test
    public void addMapping() throws IOException {
        PutMappingRequest request = new PutMappingRequest();
        request.indices("cs_index");
        request.type("_doc");
        //添加字段
        Map properties = new HashMap();
        properties.put("accountName", ImmutableBiMap.of("type", "keyword"));
        Map mapping = new HashMap<>();
        mapping.put("properties", properties);
        request.source(mapping);
        PutMappingResponse respOnse= client.indices().putMapping(request, RequestOptions.DEFAULT);
        System.out.println(response.isAcknowledged());
    }
}

2.5、文档管理

所谓文档,就是向索引里面添加数据,方便进行数据查询,详细操作内容,请看下文!

  • 添加文档

public class UserDocument {
    private String id;
    private String name;
    private String sex;
    private Integer age;
    private String city;
    private Date createTime;

    //省略get、set...
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class DocJunit {
    @Autowired
    private RestHighLevelClient client;
    /**
     * 添加文档
     * @throws IOException
     */
    @Test
    public void addDocument() throws IOException {
        // 创建对象
        UserDocument user = new UserDocument();
        user.setId("1");
        user.setName("里斯");
        user.setCity("武汉");
        user.setSex("男");
        user.setAge(20);
        user.setCreateTime(new Date());
        // 创建索引,即获取索引
        IndexRequest request = new IndexRequest();
        // 外层参数
        request.id("1");
        request.index("cs_index");
        request.type("_doc");
        request.timeout(TimeValue.timeValueSeconds(1));
        // 存入对象
        request.source(JSON.toJSONString(user), XContentType.JSON);
        // 发送请求
        System.out.println(request.toString());
        IndexResponse respOnse= client.index(request, RequestOptions.DEFAULT);
        System.out.println(response.toString());
    }
}
  • 更新文档

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class DocJunit {
    @Autowired
    private RestHighLevelClient client;
    /**
     * 更新文档(按需修改)
     * @throws IOException
     */
    @Test
    public void updateDocument() throws IOException {
        // 创建对象
        UserDocument user = new UserDocument();
        user.setId("2");
        user.setName("程咬金");
        user.setCreateTime(new Date());
        // 创建索引,即获取索引
        UpdateRequest request = new UpdateRequest();
        // 外层参数
        request.id("2");
        request.index("cs_index");
        request.type("_doc");
        request.timeout(TimeValue.timeValueSeconds(1));
        // 存入对象
        request.doc(JSON.toJSONString(user), XContentType.JSON);
        // 发送请求
        System.out.println(request.toString());
        UpdateResponse respOnse= client.update(request, RequestOptions.DEFAULT);
        System.out.println(response.toString());
    }
}
  • 删除文档

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class DocJunit {
    @Autowired
    private RestHighLevelClient client;
    /**
     * 删除文档
     * @throws IOException
     */
    @Test
    public void deleteDocument() throws IOException {
        // 创建索引,即获取索引
        DeleteRequest request = new DeleteRequest();
        // 外层参数
        request.id("1");
        request.index("cs_index");
        request.type("_doc");
        request.timeout(TimeValue.timeValueSeconds(1));
        // 发送请求
        System.out.println(request.toString());
        DeleteResponse respOnse= client.delete(request, RequestOptions.DEFAULT);
        System.out.println(response.toString());
    }
}
  • 查询文档是不是存在

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class DocJunit {
    @Autowired
    private RestHighLevelClient client;
    /**
     * 查询文档是不是存在
     * @throws IOException
     */
    @Test
    public void exists() throws IOException {
        // 创建索引,即获取索引
        GetRequest request = new GetRequest();
        // 外层参数
        request.id("3");
        request.index("cs_index");
        request.type("_doc");
        // 发送请求
        System.out.println(request.toString());
        boolean respOnse= client.exists(request, RequestOptions.DEFAULT);
        System.out.println(response);
    }
}
  • 通过 ID 查询指定文档

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class DocJunit {
    @Autowired
    private RestHighLevelClient client;
    /**
     * 通过ID,查询指定文档
     * @throws IOException
     */
    @Test
    public void getById() throws IOException {
        // 创建索引,即获取索引
        GetRequest request = new GetRequest();
        // 外层参数
        request.id("1");
        request.index("cs_index");
        request.type("_doc");
        // 发送请求
        System.out.println(request.toString());
        GetResponse respOnse= client.get(request, RequestOptions.DEFAULT);
        System.out.println(response.toString());
    }
}
  • 批量添加文档

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class DocJunit {
    @Autowired
    private RestHighLevelClient client;
    /**
     * 批量添加文档
     * @throws IOException
     */
    @Test
    public void batchAddDocument() throws IOException {
        // 批量请求
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout(TimeValue.timeValueSeconds(10));
        // 创建对象
        List userArrayList = new ArrayList<>();
        userArrayList.add(new UserDocument("张三", "男", 30, "武汉"));
        userArrayList.add(new UserDocument("里斯", "女", 31, "北京"));
        userArrayList.add(new UserDocument("王五", "男", 32, "武汉"));
        userArrayList.add(new UserDocument("赵六", "女", 33, "长沙"));
        userArrayList.add(new UserDocument("七七", "男", 34, "武汉"));
        // 添加请求
        for (int i = 0; i 

三、小结

本文主要围绕 SpringBoot 整合 ElasticSearch 接受数据的插入和搜索使用技巧,在实际的使用过程中,版本号尤其的重要,不同版本的 es,对应的 api 是不一样的。

到此这篇关于SpringBoot 整合 Elasticsearch 实现海量级数据搜索的文章就介绍到这了,更多相关SpringBoot 整合 Elasticsearch 数据搜索内容请搜索编程笔记以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程笔记!


推荐阅读
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • 本文介绍了在使用Python中的aiohttp模块模拟服务器时出现的连接失败问题,并提供了相应的解决方法。文章中详细说明了出错的代码以及相关的软件版本和环境信息,同时也提到了相关的警告信息和函数的替代方案。通过阅读本文,读者可以了解到如何解决Python连接服务器失败的问题,并对aiohttp模块有更深入的了解。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • t-io 2.0.0发布-法网天眼第一版的回顾和更新说明
    本文回顾了t-io 1.x版本的工程结构和性能数据,并介绍了t-io在码云上的成绩和用户反馈。同时,还提到了@openSeLi同学发布的t-io 30W长连接并发压力测试报告。最后,详细介绍了t-io 2.0.0版本的更新内容,包括更简洁的使用方式和内置的httpsession功能。 ... [详细]
author-avatar
mobiledu2502922957
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有