侧边栏壁纸
博主头像
Devlive 开源社区博主等级

行动起来,活在当下

  • 累计撰写 122 篇文章
  • 累计创建 32 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Spring DataJPA ElasticSearch教程(基础版)

我是管理员哦
2024-01-23 / 0 评论 / 0 点赞 / 4 阅读 / 14907 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2024-01-23,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

本教程主要详细讲解Spring Data ElasticSearch,它向ElasticSearch提供Spring Data平台的抽象.

ElasticSearch是基于文档的存储,以持久保存数据,并可用作数据库,缓存,消息代理等.

基础环境


技术版本
Java1.8+
SpringBoot2.x.x
DataJPA2.x.x
ElasticSearch5.x.x

创建项目


  • 初始化项目
mvn archetype:generate -DgroupId=com.edurt.sli.slide -DartifactId=spring-learn-integration-datajpa-elasticsearch -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0 -DinteractiveMode=false
  • 修改pom.xml增加elasticsearch的支持
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <artifactId>spring-learn-integration-datajpa</artifactId>
        <groupId>com.edurt.sli</groupId>
        <version>1.0.0</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-learn-integration-datajpa-elasticsearch</artifactId>

    <name>Spring DataJPA ElasticSearch教程(基础版)</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${dependency.springboot2.common.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${dependency.lombok.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            <version>${dependency.springboot2.common.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${dependency.springboot2.common.version}</version>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${plugin.maven.compiler.version}</version>
                <configuration>
                    <source>${system.java.version}</source>
                    <target>${system.java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

spring-boot-starter-data-elasticsearch整合ElasticSearch需要的依赖包

  • 一个简单的应用类
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 com.edurt.sli.slide;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * <p> SpringBootDataJPAElasticSearchIntegration </p>
 * <p> Description : SpringBootDataJPAElasticSearchIntegration </p>
 * <p> Author : qianmoQ </p>
 * <p> Version : 1.0 </p>
 * <p> Create Time : 2019-07-25 10:24 </p>
 * <p> Author Email: <a href="mailTo:shichengoooo@163.com">qianmoQ</a> </p>
 */
@SpringBootApplication
public class SpringBootDataJPAElasticSearchIntegration {

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

}

配置支持ElasticSearch


  • 在resources资源目录下创建一个application.properties的配置文件,内容如下
spring.data.elasticsearch.cluster-name=es
spring.data.elasticsearch.cluster-nodes=10.10.0.17:9300

操作ElasticSearch数据


  • /src/main/java/com/edurt/sli/slide目录下创建model目录,并在该目录下新建ElasticSearchModel文件
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 com.edurt.sli.slide.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.nio.file.attribute.FileTime;

/**
 * <p> ElasticSearchModel </p>
 * <p> Description : ElasticSearchModel </p>
 * <p> Author : qianmoQ </p>
 * <p> Version : 1.0 </p>
 * <p> Create Time : 2019-07-25 10:27 </p>
 * <p> Author Email: <a href="mailTo:shichengoooo@163.com">qianmoQ</a> </p>
 */
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "test", type = "elasticsearch", refreshInterval = "1s")
public class ElasticSearchModel {

    @Id
    private Long id;

    private String title;

    @Field(type = FieldType.Auto, index = true, store = true)
    private String context;

}

@Document相当于Hibernate实体的@Entity/@Table(必写)

类型属性名默认值描述
StringindexName索引库的名称,建议以项目的名称命名
Stringtype""类型,建议以实体的名称命名
shortshards5默认分区数
shortreplica1每个分区默认的备份数
StringrefreshInterval1s刷新间隔
StringindexStoreTypefs索引文件存储类型

@Id相当于Hibernate实体的主键@Id注解(必写)

@Field(相当于Hibernate实体的@Column注解),@Field默认是可以不加的,默认所有属性都会添加到ES中

类型属性名默认值说明
FileTypetypeFieldType.Auto自动检测属性的类型
FileTypeindexFieldIndex.analyzed默认情况下分词
booleanstorefalse默认情况下不存储原文
StringsearchAnalyzer""指定字段搜索时使用的分词器
StringindexAnalyzer""指定字段建立索引时指定的分词器
String[]ignoreFields{}如果某个字段需要被忽略
  • /src/main/java/com/edurt/sli/slide目录下创建repository目录,并在该目录下新建ElasticSearchSupport文件
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 com.edurt.sli.slide.repository;

import com.edurt.sli.slide.model.ElasticSearchModel;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

/**
 * <p> ElasticSearchSupport </p>
 * <p> Description : ElasticSearchSupport </p>
 * <p> Author : qianmoQ </p>
 * <p> Version : 1.0 </p>
 * <p> Create Time : 2019-07-25 10:36 </p>
 * <p> Author Email: <a href="mailTo:shichengoooo@163.com">qianmoQ</a> </p>
 */
@Repository
public interface ElasticSearchSupport extends ElasticsearchRepository<ElasticSearchModel, Long> {
}

ElasticsearchRepository中提供了一些基础的增删改查以及分页的功能.

  • 测试增删改查的功能

/src/main/java/com/edurt/sli/slide目录下创建controller目录,并在该目录下新建ElasticSearchController文件

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 com.edurt.sli.slide.controller;

import com.edurt.sli.slide.model.ElasticSearchModel;
import com.edurt.sli.slide.repository.ElasticSearchSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p> ElasticSearchController </p>
 * <p> Description : ElasticSearchController </p>
 * <p> Author : qianmoQ </p>
 * <p> Version : 1.0 </p>
 * <p> Create Time : 2019-07-25 10:39 </p>
 * <p> Author Email: <a href="mailTo:shichengoooo@163.com">qianmoQ</a> </p>
 */
@RestController
@RequestMapping(value = "elasticsearch")
public class ElasticSearchController {

    @Autowired
    private ElasticSearchSupport support;

    @GetMapping
    public Object get() {
        return this.support.findAll();
    }

    @PostMapping
    public Object post(@RequestBody ElasticSearchModel mode) {
        return this.support.save(mode);
    }

    @PutMapping
    public Object put(@RequestBody ElasticSearchModel mode) {
        return this.support.save(mode);
    }

    @DeleteMapping
    public Object delete(@RequestParam String id) {
        this.support.deleteById(Long.valueOf(id));
        return "SUCCESS";
    }

}

添加数据

shicheng@shichengdeMacBook-Pro ~> curl -X POST http://localhost:8080/elasticsearch -H 'Content-Type:application/json' -d '{"title": "Hello ElasticSearch", "context": "我是SpringBoot整合ElasticSearch示例"}'
{"id":null,"title":"Hello ElasticSearch","context":"我是SpringBoot整合ElasticSearch示例"}⏎

修改数据

shicheng@shichengdeMacBook-Pro ~> curl -X PUT http://localhost:8080/elasticsearch -H 'Content-Type:application/json' -d '{"id": 1,"title": "Hello ElasticSearch", "context": "我是SpringBoot整合ElasticSearch示例,Modfiy"}'
{"id":1,"title":"Hello ElasticSearch","context":"我是SpringBoot整合ElasticSearch示例,Modfiy"}⏎

获取数据

shicheng@shichengdeMacBook-Pro ~> curl -X GET http://localhost:8080/elasticsearch
{"content":[{"id":null,"title":"Hello ElasticSearch","context":"我是SpringBoot整合ElasticSearch示例,Modfiy"},{"id":1,"title":"Hello ElasticSearch","context":"我是SpringBoot整合ElasticSearch示例,Modfiy"}],"pageable":{"sort":{"sorted":false,"unsorted":true},"offset":0,"pageSize":2,"pageNumber":0,"paged":true,"unpaged":false},"facets":[],"aggregations":null,"scrollId":null,"totalElements":2,"totalPages":1,"size":2,"number":0,"numberOfElements":2,"first":true,"sort":{"sorted":false,"unsorted":true},"last":true}⏎

删除数据

shicheng@shichengdeMacBook-Pro ~> curl -X DELETE 'http://localhost:8080/elasticsearch?id=1'
SUCCESS⏎

打包文件部署


  • 打包数据
mvn clean package -Dmaven.test.skip=true -X

运行打包后的文件即可

java -jar target/spring-learn-integration-datajpa-elasticsearch-1.0.0.jar

源码地址


0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区