一、docker安装es
docker run -it --name elasticsearch -d -p 9200:9200 -p 9300:9300 -p 5601:5601 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.3.0
说明:
- 9300是tcp通讯端口,集群间和TCPClient都走的它;
- 9200是http协议的RESTful接口,后续的API接口测试都是走的它。
- 单节点部署添加启动参数 -e "discovery.type=single-node"
访问看到如下即可安装成功
在这里可能会出现跨域拒绝访问问题:
进入elasticsearch容器内部,修改配置文件elasticsearch.yml
docker ps -a #拿到运行容器elasticsearch 的 id
docker exec -it ******(容器id) /bin/bash
cd ./config
vim elasticsearch.yml
在elasticsearch.yml中添加:
http.cors.enabled: true
http.cors.allow-origin: "*"
二、springboot简单使用
这里我们采用spring-boot-starter-data-elasticsearch
1、新建springboot项目加入pom
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.fengpt</groupId>
<artifactId>spirng-es</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spirng-es</name>
<description>Demo project for Spring Boot</description>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、增加配置application.yml
spring:
elasticsearch:
rest:
uris: http://127.0.0.1:9200
3、编写测试代码
dto
@Data
@Document(indexName = "test-index")
public class ElasticEntity {
@Id
private String id;
private String name;
private Integer age;
}
dao
@Repository
public interface EsRepository extends ElasticsearchRepository<ElasticEntity, String> {
}
service
Slf4j
@Service
public class EsService {
@Autowired
EsRepository esRepository;
public void add(ElasticEntity elasticEntity){
esRepository.save(elasticEntity);
}
public List<ElasticEntity> getAll(){
List<ElasticEntity> list=new ArrayList<>();
Iterable<ElasticEntity> iterable = esRepository.findAll();
iterable.forEach(list::add);
return list;
}
public List<ElasticEntity> getByName(String name) {
List<ElasticEntity> list=new ArrayList<>();
MatchQueryBuilder queryBuilder=new MatchQueryBuilder("name",name);
Iterable<ElasticEntity> personIterable=esRepository.search(queryBuilder);
personIterable.forEach(list::add);
return list;
}
public Page<ElasticEntity> pageQuery(Integer pageNo, Integer pageSize, String kw) {
SearchQuery searchQuery=new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchPhraseQuery("name",kw)).withPageable(PageRequest.of(pageNo,pageSize)).build();
return esRepository.search(searchQuery);
}
}
controller
@RestController
@Slf4j
public class EsController {
@Autowired
private EsService esService;
@PostMapping("/add")
public String add(@RequestBody ElasticEntity elasticEntity){
esService.add(elasticEntity);
return "sucess";
}
@GetMapping("/getAll")
public List<ElasticEntity> getAll(){
return esService.getAll();
}
@GetMapping("/getByName")
public List<ElasticEntity>getByName(@RequestParam("name") String name){
return esService.getByName(name);
}
@GetMapping("/pageQuery")
public Page<ElasticEntity> pageQuery(@RequestParam("pageNo") Integer pageNo,
@RequestParam("pageSize") Integer pageSize,
@RequestParam("kw") String kw){
return esService.pageQuery(pageNo,pageSize,kw);
}
}
4、测试
- 调用新增接口/add
- 调用查询接口/getAll
- 调用搜索接口 getByName?name=三
评论区