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);
}
}