SpringBoot整合ElasticSearch及基本操作

ElasticSearch简介

我们的应用经常需要添加检索功能,开源的 ElasticSearch 是目前全文搜索引擎的首选。他可以快速的存储、搜索和分析海量数据。Spring Boot通过整合Spring Data ElasticSearch为我们提供了非常便捷的检索功能支持。
Elasticsearch是一个分布式搜索服务,提供Restful API,底层基于Lucene,采用多shard(分片)的方式保证数据安全,并且提供自动resharding的功能,github等大型的站点也是采用了ElasticSearch作为其搜索服务。

核心概念

以 员工文档 的形式存储为例:一个文档代表一个员工数据。存储数据到 ElasticSearch 的行为叫做 索引 ,但在索引一个文档之前,需要确定将文档存储在哪里。一个 ElasticSearch 集群可以 包含多个 索引 ,相应的每个索引可以包含多个 类型 。 这些不同的类型存储着多个 文档 ,每个文档又有 多个 属性 。
类似关系:
索引-数据库
类型-表
文档-表中的记录
属性-列
Cluster和Node,集群与节点。由于ElasticSearch可以存放并检索PB级别的数据,一台服务器是存放不了这么多数据的,而且从ElasticSearch的高可用以及容灾性来考虑,必定是用多个服务器协调存储数据的。节点就是具备ElasticSearch环境并存放有数据的单个服务器。集群就是所有可用的节点组成的网状图。
注意:ElasticSearch不推荐使用类型
在这里插入图片描述

ElasticSearch安装

在linux服务器上使用docker命令下载ElasticSearch,前提是已经安装过docker,安装docker可以参考docker下载安装教程

docker pull elasticsearch

下载完成以后如下可以看到elasticsearch版本号以及镜像id等信息
在这里插入图片描述第一次启动运行以下命令

docker run -d -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -p 9200:9200 -p 9300:9300 --name myelasticsearch ffa00077159c

以后启动运行以下

docker start myelasticsearch

ElasticSearch基本操作

基本操作包括增删改查具体参照elasticsearch基本操作

SpringBoot整合ElasticSearch

1.新建项目选择web模块和ElasticSearch模块
在这里插入图片描述
2.在application.properties配置文章中必须要指定elasticsearch集群以及当前存活的任意一个节点的9300端口。

spring.elasticsearch.rest.uris=ip:9300

3.使用ElasticsearchRestTemplate对数据进行增删改查操作


import org.elasticsearch.index.reindex.UpdateByQueryRequestBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.*;
import top.zbawq.testel.bean.Student;

import javax.swing.text.Document;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@SpringBootTest
class TestelApplicationTests {
    @Autowired
    ElasticsearchRestTemplate elasticsearchTemplate;

    // 添加数据
    @Test
    void contextLoads() {
        // IndexQuery query, IndexCoordinates index
        // 对索引名为teststudent的索引添加数据,如果不存在索引名为teststudent的索引则创建索引之后执行插入,如果存在直接插入
        Student student = new Student(1, "张三", 18);
        IndexQuery indexQuery = new IndexQueryBuilder()
                .withId("1")
                .withObject(student).build();
        IndexCoordinates indexCoordinates = IndexCoordinates.of("testemp");
        elasticsearchTemplate.index(indexQuery, indexCoordinates);
    }

    // 删除索引中的数据
    @Test
    void test01() {
        IndexCoordinates indexCoordinates = IndexCoordinates.of("testemp");
        elasticsearchTemplate.delete("1", indexCoordinates);
    }

    @Test
    void test02() {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        NativeSearchQuery query = new NativeSearchQueryBuilder().withIds(strings).build();
        IndexCoordinates indexCoordinates = IndexCoordinates.of("testemp");
        elasticsearchTemplate.delete(query, Student.class, indexCoordinates);
    }

    // 修改数据  更新涉及到版本控制以便维护数据一致性,其实分为两个操作:get和reindex,大致步骤是:首先取到相应的document,然后执行更新script,最后返回执行的结果。至于具体的多版本控制机制将在第6部分解释。
    /**
    @Test
    void test03() {
        //UpdateQuery query, IndexCoordinates index
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("name","王五");
        String script="ctx._source.name='王五'";
        UpdateQuery updateQuery = UpdateQuery.builder("1").withParams(map).withScript(script).build();
        IndexCoordinates indexCoordinates=IndexCoordinates.of("testemp");
        elasticsearchTemplate.update(updateQuery,indexCoordinates);
    }*/
    // 查数据
    @Test
    void test04() {
        //Query query, Class<T> clazz, IndexCoordinates index
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        NativeSearchQuery query = new NativeSearchQueryBuilder().withIds(strings).build();
        IndexCoordinates indexCoordinates = IndexCoordinates.of("testemp");
        List<Student> ts = elasticsearchTemplate.multiGet(query, Student.class, indexCoordinates);
        for (Student student:ts){
            System.out.println(student);
        }
    }

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是SpringBoot整合Elasticsearch的步骤: 1. 添加Elasticsearch的依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> ``` 2. 配置Elasticsearch连接 在application.yml或application.properties中添加以下配置: ``` spring: data: elasticsearch: cluster-name: elasticsearch # Elasticsearch集群名称 cluster-nodes: 127.0.0.1:9300 # Elasticsearch连接地址 ``` 3. 创建Elasticsearch实体类 创建一个Java类,使用@Document注解标记为Elasticsearch文档类型,并使用其他注解指定属性的映射关系,如下所示: ``` @Document(indexName = "my_index", type = "my_type") public class MyDocument { @Id private String id; private String title; private String content; // ... 省略getter和setter方法 } ``` 4. 创建Elasticsearch操作接口 创建一个接口,继承ElasticsearchRepository接口,并指定泛型为步骤3中创建的实体类,如下所示: ``` public interface MyDocumentRepository extends ElasticsearchRepository<MyDocument, String> { } ``` 5. 使用Elasticsearch操作数据 在需要使用Elasticsearch的地方注入MyDocumentRepository,即可使用其提供的方法进行数据的CRUD操作,如下所示: ``` @Autowired private MyDocumentRepository repository; public void save(MyDocument document) { repository.save(document); } public MyDocument findById(String id) { return repository.findById(id).orElse(null); } public void deleteById(String id) { repository.deleteById(id); } ``` 以上就是SpringBoot整合Elasticsearch的基本步骤,希望对你有帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值