Spring boot 接入ES
最终效果:
效果丑,也就那么意思一下;因为sping boot已经集成了es,所以接入es只需要很简单的配置一下就好了。当然事先得下载好es,我用的是windows 6.5.3版本,没有遇到什么版本冲突问题,至于安装es和建立spring boot项目我就不细说了。
代码:
pom.xml
<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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
</dependencies>
引入需要的jar包就好
application.yml
spring:
datasource:
hikari:
jdbc-url: jdbc:mysql://url:port/database
username: username
password: password
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://url:port/database
username: username
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
thymeleaf:
cache: false
prefix: classpath:templates/
suffix: .html
mode: HTML
encoding: UTF-8
data:
elasticsearch:
cluster-name: es-test
cluster-nodes: localhost:9300
repositories:
enabled: true
main:
allow-bean-definition-overriding: true
server:
port: 80
mybatis:
type-aliases-package: com.xxx.xxx.xxx
cluster-name就是自己设置的es集群名称,可以在es安装目录下面的config/elasticsearch.yml配置
实体类
package com.xxx.xxx.model;
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;
import java.util.Date;
@Document(indexName = "test", type = "company")
public class Company {
@Id
private String id;
@Field(analyzer = "ik",searchAnalyzer = "ik",store = true,type = FieldType.Text)
private String cname;
private String ename;
private String iname;
private String scode;
private String amt;
private String person;
@Field
private Date btime;
@Field
private Date sbtime;
@Field
private Date ptime;
private String address;
private String tel;
private String fax;
private String web;
private String email;
private String sec;
@Field(analyzer = "ik",searchAnalyzer = "ik",store = true,type = FieldType.Text)
private String industry;
private String business;
private String recommend;
private String over;
private Date ctime;
@Field(analyzer = "ik",searchAnalyzer = "ik",store = true,type = FieldType.Text)
private String industry2;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id == null ? null : id.trim();
}
public String getCname()