Spring Boot整合分布式搜索引擎ElasticSearch 实现相关操作

一、ElasticSearch 介绍

Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。它能很方便的使大量数据具有搜索、分析和探索的能力。充分利用Elasticsearch的水平伸缩性,能使数据在生产环境变得更有价值。Elasticsearch 的实现原理主要分为以下几个步骤,首先用户将数据提交到Elasticsearch 数据库中,再通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据,当用户搜索数据时候,再根据权重将结果排名,打分,再将返回结果呈现给用户。
ElasticSearch 官网地址:www.elastic.co/cn/

二、环境准备

在开始开发之前,我们需要准备一些环境配置:

jdk 1.8 或其他更高版本
开发工具 IDEA
管理依赖 Maven
ElasticSearch环境,此处使用docker搭建,ElasticSearch 版本为7.17.7
Spring Boot 2.X

三、创建Spring Boot项目导入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

四、创建高级客户端浏览器访问127.0.0.1:9200


@SpringBootApplication
@MapperScan("com.zh.mapper")
public class esApplication {


    public static void main(String[] args) {
        SpringApplication.run(esApplication.class,args);
    }

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        return new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://127.0.0.1:9200")
        ));
    }
}

五、使用ES进行增删改查


@SpringBootTest
public class HotelTest {
    @Autowired
    private RestHighLevelClient client;

    //    创建索引库
    @Test
    void createIndex() throws IOException {
        // 创建索引库请求对象
        CreateIndexRequest request = new CreateIndexRequest("item");
        //准备参数
        request.source(STRING, XContentType.JSON);
        // 执行请求
        client.indices().create(request, RequestOptions.DEFAULT);
    }

    //判断索引库是否存在
    @Test
    void exists() throws IOException {
        // 创建一个GetIndexRequest对象,索引名为dongke
        GetIndexRequest request = new GetIndexRequest("dongke");
        // 判断索引是否存在
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        // 输出索引是否存在
        System.out.println(exists ? "存在" : "不存在");
    }

    //删除索引库
    @Test
    void deletess() throws IOException {
        // 创建一个删除索引的请求
        DeleteIndexRequest request = new DeleteIndexRequest("hotel");
        // 删除索引
        client.indices().delete(request, RequestOptions.DEFAULT);
    }



    @BeforeEach
    void indexs() {
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://127.0.0.1:9200")
        ));
    }

    @AfterEach
    void after() throws IOException {
        this.client.close();
    }


}

六、总结

依赖版本必须与你当前所用的版本保持一致,否则连接失败。
如果添加时不指定文档ID,他就会随机生成一个ID,ID唯一。
创建文档时若该ID已存在,发送创建文档请求后会更新文档中的数据。
更新文档时需要将实体对象中的属性全部指定值,不然会被设置为空,如果只设置了一个字段,那么只有该字段会被修改成功,其他会被修改为null。
hasFailures()方法是返回是否失败,即它的值为false时说明上传成功
elasticsearch很消耗内存,极力推荐使用docker部署运行

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值