栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

SpringBoot——整合ElasticSearch实现对es文档的基本操作

SpringBoot——整合ElasticSearch实现对es文档的基本操作

文章目录
  • 一、创建springboot项目
  • 二、导入es依赖坐标
  • 三、创建实体类
  • 四、创建dao层
  • 五、添加配置类
  • 六、测试

一、创建springboot项目

这里不做演示

二、导入es依赖坐标
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
        
            org.projectlombok
            lombok
            true
        
三、创建实体类
package com.example.springboot_es.user;

import lombok.Data;
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;


//indexName 指定索引名称
@document(indexName = "lx-sd")
@Data
public class Article {
    @Id
    @Field(index = false,type = FieldType.Integer)
    private Integer id;
    
    @Field(analyzer = "ik_smart",searchAnalyzer = "ik_smart",store = true,type = FieldType.Text)
    private String title;
    @Field(analyzer = "ik_smart",searchAnalyzer = "ik_smart",store = true,type = FieldType.Text)
    private String context;
    @Field(store = true,type = FieldType.Integer)
    private Integer hits;
}

四、创建dao层
package com.example.springboot_es.dao;

import com.example.springboot_es.user.Article;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;

import java.util.List;




@Component
public interface ArticleDao extends ElasticsearchRepository {

    
    List
findByTitle(String title); List
findByTitleOrContext(String title,String context); List
findByTitleOrContext(String title, String context, Pageable pageable); }
五、添加配置类
spring:
  elasticsearch:
    rest:
      uris: localhost:9200
      read-timeout: 30s
      connection-timeout: 5s
六、测试
package com.example.springboot_es;

import com.example.springboot_es.dao.ArticleDao;
import com.example.springboot_es.user.Article;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

import java.util.List;
import java.util.Optional;

@SpringBootTest
public class Estest {

    @Autowired
    private ElasticsearchRestTemplate template;

    @Autowired
    private ArticleDao articleDao;


    //通过springboot es向elasticsearch数据库储存一条数据
    @Test
    public void testSave() {
        //创建文档
        Article article = new Article();
        article.setId(1);
        article.setTitle("es搜索");
        article.setContext("成功了吗");
        //保存文档
        articleDao.save(article);
    }

    //修改
    @Test
    public void testUpdate() {
        //判断数据库中是否有你指定的id的文档,如果没有。就进行保存,如果有,就进行更新
        //创建文档
        Article article = new Article();
        article.setId(1);
        article.setTitle("es搜索1");
        article.setContext("成功了吗1");
        //保存文档
        articleDao.save(article);
    }

    //删除
    @Test
    public void testDelete() {
//        根据主键删除
        articleDao.deleteById(1);
    }


    //重新构建数据
    @Test
    public void makeData(){
        for (int i = 1; i <= 10; i++) {
            //创建文档
            Article article = new Article();
            article.setId(i);
            article.setTitle("es搜索"+i);
            article.setContext("成功了吗"+i);
            article.setHits(100+i);
            //保存数据
            articleDao.save(article);
        }
    }

    //查询所有
    @Test
    public void findAll(){
        Iterable
all = articleDao.findAll(); for (Article article : all) { System.out.println(article); } } //主键查询 @Test public void testFindById(){ Optional
id = articleDao.findById(1); System.out.println(id.get()); } //分页查询 @Test public void testFindAllWithPage(){ //设置分页条件 //page代表页码,从0开始 PageRequest pageRequest = PageRequest.of(1, 3); Page
all = articleDao.findAll(pageRequest); for (Article article : all) { System.out.println(article); } } //排序查询 @Test public void testFindWithSort(){ //设置排序条件 Sort sort = Sort.by(Sort.Order.desc("hits")); Iterable
all = articleDao.findAll(sort); for (Article article : all) { System.out.println(article); } } //分页加排序查询 @Test public void testFindAllWithPageAndSort(){ //设置排序条件 Sort sort = Sort.by(Sort.Order.desc("hits")); //设置分页条件 PageRequest pageable = PageRequest.of(1, 3, sort); Page
page = articleDao.findAll(pageable); for (Article article : page.getContent()) { System.out.println(article); } } //根据标题查询 @Test public void testFindByTitle(){ List
es = articleDao.findByTitle("es"); for (Article e : es) { System.out.println(e); } } //根据标题或内容查询 @Test public void testFindByTitleOrContext(){ List
es = articleDao.findByTitleOrContext("es", "1"); for (Article e : es) { System.out.println(e); } } //根据标题和内容查询(含分页) @Test public void testFindByTitleOrContextWithPage(){ //设置排序条件 Sort sort = Sort.by(Sort.Order.desc("hits")); //设置分页条件 PageRequest pageRequest = PageRequest.of(1, 3, sort); List
es = articleDao.findByTitleOrContext("es", "1", pageRequest); for (Article e : es) { System.out.println(e); } } }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/652576.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号