自从上一家公司辞职之后,也没有什么机会做这个es相关的了,最近公司要做学习分享,就又拾起es这个话题。
使用SpringBootDataElasticsearch
去springBootData官网看,这次用的版本号如下:
首先要起一个Elasticsearch,为了方便,我用docker来起,这里贴上docker-compose.yml
version: '2.2'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.8.5
container_name: elasticsearch
environment:
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata1:/usr/share/elasticsearch/data
ports:
- 9200:9200
- 9300:9300
networks:
- es_net
elasticsearch2:
image: docker.elastic.co/elasticsearch/elasticsearch:6.8.5
container_name: elasticsearch2
environment:
- cluster.name=docker-cluster
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- "discovery.zen.ping.unicast.hosts=elasticsearch"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata2:/usr/share/elasticsearch/data
networks:
- es_net
volumes:
esdata1:
driver: local
esdata2:
driver: local
networks:
es_net:
启动很简单,直接docker-compose up 就可以启动了。
启动docker
打开127.0.0.1:9200,我们可以看到es的相应。
-
SpringData 接入:
- 直接再maven配置文件中引入spring-boot-starter-data-elasticsearch,就可以用了,因为是starter,可以直接开箱即用。
这里我直接贴上pom.xml配置文件
<?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.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>pers.yawb</groupId>
<artifactId>elasticsearch-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>elasticsearch-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- application.yml中直接配置就可以用了。贴上~配置
spring:
data:
elasticsearch:
repositories:
enabled: true
cluster-nodes: 127.0.0.1:9300
cluster-name: docker-cluster
logging:
file: data/logs/elasticsearchdemo
config: classpath:logback-spring.xml
level:
org.pringframework.data.elasticsearch: DEBUG
下一步就可以写我们熟悉的MVC了,自己写controller,持久层,业务逻辑层
我又贴一下代码~
/**
* 仓库
* @author Yawb
* @date 2019/12/9 14:31
*/
public interface BankRepository extends ElasticsearchRepository<Bank, Long> {
List<Bank> findByFirstname(String firstName);
@Query("{\n" +
" \"query\": { \"match_phrase\": { \"firstname\": \"?0\" } }\n" +
"}")
List<Bank> findByQueryFirstName(String firstName);
}
这里持久层又回到了springData那一套东西了,可以这样子搞。
我们看一下实现增删查改的那几个方法:
ElasticsearchRepository类:
index(T t), delete(T t),search(Query q)
注意,es没有update这个逻辑,先插入一条新的数据,插入成功之后把旧的数据删除。
最后,写个test方法~
@Test
public void test() {
System.out.println("hhsxxx");
Page<Bank> banks = bankService.searchBuild("Winnie", "Mill Lane");
banks.get().forEach(System.out::println);
List<Bank> banks1 = bankService.searchTemplate("Mill Lane");
banks1.forEach(System.out::println);
}