当前位置:   article > 正文

springboot整理elasticsearch+增删改查_springboot elasticsearch 增删改查

springboot elasticsearch 增删改查
使用idea构建springboot项目

设置web配置
在这里插入图片描述
选择NoSQL elasticsearch数据库+驱动
在这里插入图片描述

pom.xml

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!--json直接转对象,springboot可以自动选择版本-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
application.yml

指定cluster-name 具体文件在es安装的conf目录下的.yml结尾文件
设置端口为9300

spring:
    data:
        elasticsearch:
          cluster-name: my-es
          cluster-nodes: 192.168.56.100:9300
          repositories:
            enabled: true

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

分层开发包结构

实体类

实体类最重要的是如果使用构造器,必须在实体类中写好一个无参构造,否则会有错误,
实体类必须实现序列化接口

//这里指定自生成或插入的index名,以及type名
@Data
@Document(indexName = "titleindex",type = "_doc")
public class TitleDocument implements Serializable {
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitlename() {
        return titlename;
    }

    public void setTitlename(String titlename) {
        this.titlename = titlename;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    /**
     *
     */
    @Id
    private Long id;

    /**
     *  选中ik 分词器
     */
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String titlename;

    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String description;

    public TitleDocument() {
    }

    public TitleDocument(Long id, String titlename, String description) {
        this.id = id;
        this.titlename = titlename;
        this.description = description;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
接口

继承elasticsearch包的强大接口,实现CRUD功能

@Component
public interface TitleRepository  extends ElasticsearchRepository<TitleDocument,Long> {
    
}
  • 1
  • 2
  • 3
  • 4
服务类
@Service
public class TitleDocumentService {
    @Autowired
    private TitleRepository titleRepository;
    private TitleDocument document;
    /**
     * 创建索引
     * @param titlename
     * @param description
     */
    public void save(Long id,String titlename,String description) throws NoSuchAlgorithmException {
        TitleDocument titleDocument = new TitleDocument(id, titlename+id, description+id);
        titleRepository.save(titleDocument);
}


    /**
     * 删除索引数据
     * @param id
     * @return
     */
    public boolean delete(Long id) {
        titleRepository.deleteById(id);
        return true;
    }

    /**
     * 查询所有的文章
     * @param searchContent
     * @return
     */
    public List<TitleDocument> getTitle(String searchContent) {
        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(searchContent);
        System.out.println("查询的语句:"+builder);
        Iterable<TitleDocument> searchResult = titleRepository.search(builder);
        Iterator<TitleDocument> iterator = searchResult.iterator();
        List<TitleDocument> list=new ArrayList<>();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }

    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @param searchContent
     * @return
     */
    public List<TitleDocument> pageTitle(Integer pageNumber, Integer pageSize, String searchContent) {
//        分页参数
        PageRequest pageRequest = PageRequest.of(pageNumber, pageSize);
        QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder(searchContent);
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withPageable(pageRequest).withQuery(queryBuilder).build();
        System.out.println("查询的语句:" + searchQuery.getQuery().toString());
        Page<TitleDocument> search = titleRepository.search(searchQuery);
        return search.getContent();
    }
    public boolean deleteDoc() {
        titleRepository.deleteAll();
        return true;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
控制器类
@RestController
@RequestMapping
public class TitleController {
    @Autowired
    private TitleDocumentService titleService;
    private TitleRepository repository;
    /**
     * 给索引添加数据
     *
     * @return
     */
    @PostMapping("save")
    public String save(Long id, String titlename, String description) throws NoSuchAlgorithmException {
        titleService.save(id, titlename, description);
        return "success";
    }

    /**
     * 删除
     *
     * @return
     */
    @PostMapping("delete")
    public String delete(Long id) {
        titleService.delete(id);
        return "success";
    }

    @GetMapping("get")
    public List<TitleDocument> getTitle(String param) {
        List<TitleDocument> titleDocumentList = titleService.getTitle(param);
        return titleDocumentList;
    }

    @GetMapping("page")
    public List<TitleDocument> pageTitle(Integer pageNumber, Integer pageSize, String searchContent) {
        List<TitleDocument> documentList = titleService.pageTitle(pageNumber, pageSize, searchContent);
        return documentList;
    }
    @PostMapping("deleteDoc")
    public String deleteDoc(){
        titleService.deleteDoc();
        return  "success"  ;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

测试类结构

在这里插入图片描述

package com.njbdqn.springes.controller;

import com.njbdqn.springes.service.TitleDocumentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TitleControllerTest {
    @Autowired
    private TitleDocumentService titleService;
    @Test
    public void save() throws Exception {
        for (int i = 0; i < 20; i++) {
            titleService.save(Long.parseLong(String.valueOf(i)),"测试名称", "测试内容");
            System.out.println("插入第"+i+"条");
        }
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

成功即可在es中可以查看到相应数据
并且插入的过程会在控制台打印

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/68805
推荐阅读
相关标签
  

闽ICP备14008679号