天天看点

Spring boot集成elasticsearch注解方式Spring boot集成elasticsearch注解方式

Spring boot集成elasticsearch注解方式

1.主类的配置

package com.song.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

@SpringBootApplication
@Configuration
@EnableElasticsearchRepositories(basePackages = "com")
@ComponentScan(basePackages = "com")
public class MainApplication {

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


}

           

@EnableElasticsearchRepositories(basePackages = “com”)这个必须配置,否则继承ElasticsearchRepository类的接口不会被注入

2.编写实体类

package com.entry;

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

/**
 * @create 2020-12-09  16:36
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "es_student")
public class Student {
    @Id //必须存在的,索引的id
    private int id;

    //不进行分词
    @Field(type = FieldType.Keyword)
    private int age;


    //使用ik_smark分词器进行分词
    //analyzer  存储时的分词类型
    //searchAnalyzer 查询时的分词类型
    // ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。
    @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_smart")
    private String name;

    //使用ik_smark分词器进行分词
    //ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合;
    @Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
    private String desc;
}

           

3.编写接口实现ElasticsearchRepository接口

Spring boot集成elasticsearch注解方式Spring boot集成elasticsearch注解方式

T代表实体类,ID代表实体类中的@id注解标记字段的类型

package com.entry;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
 * @create 2020-12-09  16:44
 */

public interface StudentRepository extends ElasticsearchRepository<Student,Integer> {
}
           

4.进行增删查接口编写

package com.entry.service;

/**
 * @create 2020-12-09  16:47
 */
public interface StudentService {

    //增加文档
    public boolean Add();

    //删除文档
    public boolean Del();


    //查询文档
    public void select();
}

           

4.进行增删查接口实现类编写

package com.entry.service;

import com.entry.Student;
import com.entry.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @create 2020-12-09  16:48
 */
@Service
public class StudentServiceImpl  implements StudentService{

    @Autowired
    StudentRepository studentRepository;

    //添加
    @Override
    public boolean Add() {
        Student s=new Student(1,10,"张三","男");
        studentRepository.save(s);
        return false;
    }

    //删除
    @Override
    public boolean Del() {
        Student s=new Student(1,10,"张三","男");
        studentRepository.delete(s);
        return false;
    }

    //查询
    @Override
    public void select() {
        Optional<Student> Student = studentRepository.findById(1);
        System.out.println(Student.get().toString());
    }

}

           

studentRepository接口中还有一系列方法,有兴趣的可以一一实验。