简单的集成
一、maven配置、配置文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sakyoka.test</groupId>
<artifactId>springboot-elasticsearch-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-elasticsearch-test</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- springboot start -->
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- <exclusions> <exclusion> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<finalName>springboot-elasticsearch-test</finalName>
<resources>
<resource>
<directory>${basedir}/src/main/webapp</directory>
<!--注意此次必须要放在此目录下才能被访问到-->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
</build>
</project>
注意:我安装的elasticsearch 是7.17.0版本,所以springboot版本2.5.x,2.4.x也可以
application.yml
server:
port: 9001
spring:
data:
elasticsearch:
client:
reactive:
endpoints: 192.168.19.129:9300 #服务器elasticsearch的endpoints地址
elasticsearch:
rest:
uris: 192.168.19.129:9200 #服务器elasticsearch的接口地址
devtools:
restart:
enabled: true
additional-paths: src/main/java
二、继承ElasticsearchRepository操作数据
测试PersonDao
package com.sakyoka.test.elasticsearch.person.dao;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import com.sakyoka.test.elasticsearch.person.model.PersonModel;
/**
*
* 描述:dao
* @author sakyoka
* @date 2022年9月2日 下午8:29:57
*/
public interface PersonDao extends ElasticsearchRepository<PersonModel, String>{
}
对象PersonModel,添加索引名称
package com.sakyoka.test.elasticsearch.person.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import lombok.Data;
/**
*
* 描述:个人数据
* @author sakyoka
* @date 2022年9月2日 下午8:33:24
*/
@Data
@Document(indexName = "person")
public class PersonModel {
@Id
private String id;
private String name;
private Integer sex;
private String job;
//private MoneyModel moneyModel;
}
业务类
package com.sakyoka.test.elasticsearch.person.service.impl;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;
import com.sakyoka.test.elasticsearch.person.dao.PersonDao;
import com.sakyoka.test.elasticsearch.person.model.PersonModel;
import com.sakyoka.test.elasticsearch.person.model.PersonQuery;
import com.sakyoka.test.elasticsearch.person.service.PersonService;
/**
*
* 描述:个人数据业务接口实现类
* @author sakyoka
* @date 2022年9月2日 下午8:26:56
*/
@Service
public class PersonServiceImpl implements PersonService{
@Autowired
PersonDao personDao;
@Autowired
ElasticsearchRestTemplate elasticsearchRestTemplate;
@Override
public Iterable<PersonModel> findAll() {
Iterable<PersonModel> iterable = personDao.findAll();
return iterable;
}
@Override
public void save(PersonModel personModel) {
personModel.setId(UUID.randomUUID().toString().replace("-", ""));
personDao.save(personModel);
}
@Override
public void deleteById(String id) {
}
@Override
public void update(PersonModel personModel) {
}
@Override
public PersonModel getById(String id) {
return null;
}
@Override
public void deleteAll() {
personDao.deleteAll();
}
}
控制层
package com.sakyoka.test.elasticsearch.person.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sakyoka.test.elasticsearch.person.model.PersonModel;
import com.sakyoka.test.elasticsearch.person.service.PersonService;
/**
*
* 描述:控制层
* @author sakyoka
* @date 2022年9月2日 下午8:36:54
*/
@RequestMapping("/person")
@RestController
public class PersonController {
@Autowired
PersonService personService;
@GetMapping
public Iterable<PersonModel> findAll() {
return personService.findAll();
}
@PostMapping
public String save(@RequestBody PersonModel personModel) {
personService.save(personModel);
return "ok";
}
@DeleteMapping
public String deleteAll() {
personService.deleteAll();
return "ok";
}
}
这里就简单测试下保存、删除、修改、查询
三、测试结果
保存
查询
删除
是不是好像我没有写修改方法,却要测试修改,这是因为继承ElasticsearchRepository方式save既是保存也是修改。
好了测试就到这里了,其实最麻烦应该就是查询了,各种组合,不过这工具就是为了大数据查询而生的,查询功能很强大。
拓展
出了继承ElasticsearchRepository之外还可以用ElasticsearchRestTemplate去操作数据当然还有其它client对象可以去操作。
另外也可以访问elasticsearch接口地址去访问http://192.168.19.129:9200/索引名称/_search