springboot集成ES

1.环境搭建

不会搭建的环境的朋友参考我的博客里面的 搭建ES环境的文章 https://blog.csdn.net/mclongyi/article/details/103570680

 

2.springboot相关

1.pom文件的配置

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <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>

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

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <!-- <version>3.0.9</version> -->
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.1.1</version>
        </dependency>
    </dependencies>

2.配置文件

server.port=8081
spring.application.name=es-server
spring.data.elasticsearch.cluster-name=docker-cluster
spring.data.elasticsearch.cluster-nodes=XXXX:9300 //服务端ip
spring.data.elasticsearch.repositories.enabled=true

 

3.代码

@Data
@Document(indexName = "studentindex",type = "student")
public class Student implements Serializable {

    private String id;

    @Field(analyzer = "ik_smart",type = FieldType.Text)
    private String name;
    private Integer age;
    @Field(analyzer = "ik_smart",type = FieldType.Text)
    private String desc;

}
/**
 * @author Administrator
 */
public interface StudentRepository extends ElasticsearchCrudRepository<Student,String> {
}
@Service
public class StudentService {

    @Autowired
    private StudentRepository studentRepository;

    public boolean add(Student student){
        studentRepository.save(student);
        return true;
    }

    public List<Student> findList(){
        Iterable<Student> all = studentRepository.findAll();
        List<Student> students = CollUtil.newArrayList(all);
        return students;
    }

}

 

测试

@Slf4j
@SpringBootTest
//@ActiveProfiles(value = "dev-zt")
@RunWith(SpringRunner.class)
public class StudentServiceTest {

    @Autowired
    private StudentService studentService;

    @Test
    public void addTest(){
        Student student=new Student();
        student.setAge(100);
        student.setDesc("我是明哥哥333");
        Random random=new Random();
        student.setId(UUID.randomUUID().toString());
        student.setName("555232323");
        studentService.add(student);
    }


    @Test
    public void getAll(){
        List<Student> list = studentService.findList();
        System.out.println(list);
    }



}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值