增加 elasticsearch 示例(elasticsearch 部分)

This commit is contained in:
YunaiV
2019-11-04 23:26:40 +08:00
parent 8c9c8f7dd2
commit 413682d696
22 changed files with 276 additions and 25 deletions

View File

@@ -0,0 +1,8 @@
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号',
`username` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '账号',
`password` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '密码',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

View File

@@ -1,19 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 使用驼峰命名法转换字段。 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer"/>
<typeAlias alias="Long" type="java.lang.Long"/>
<typeAlias alias="HashMap" type="java.util.HashMap"/>
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap"/>
<typeAlias alias="ArrayList" type="java.util.ArrayList"/>
<typeAlias alias="LinkedList" type="java.util.LinkedList"/>
</typeAliases>
</configuration>

View File

@@ -3,6 +3,7 @@ CREATE TABLE `users` (
`username` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '账号',
`password` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '密码',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`deleted` bit(1) DEFAULT NULL COMMENT '是否删除。0-未删除1-删除',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

View File

@@ -14,8 +14,8 @@ mybatis:
# mapper 配置内容
mapper:
mappers: cn.iocoder.springboot.lab12.mybatis.util.BaseMapper
not-empty: false
mappers: cn.iocoder.springboot.lab12.mybatis.util.BaseMapper # 设置基础 Mapper 接口
not-empty: true # 在 INSERT 和 UPDATE 操作时,是否会判断字段是否为空。即 <if test='xxx != null' />
identity: MYSQL
# PageHelper 配置内容

View File

@@ -0,0 +1,31 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lab-15-spring-data-elasticsearch</artifactId>
<dependencies>
<!-- 自动化配置 Spring Data Elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- 方便等会写单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,7 @@
package cn.iocoder.springboot.lab15.springdataelasticsearch;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
}

View File

@@ -0,0 +1,26 @@
package cn.iocoder.springboot.lab15.springdataelasticsearch.constant;
/**
* ES 字段分析器的枚举类
*
* 关于 IK 分词,文章 https://blog.csdn.net/xsdxs/article/details/72853288 不错。
* 目前项目使用的 ES 版本是 6.7.1 ,可以在 https://www.elastic.co/cn/downloads/past-releases/elasticsearch-6-7-1 下载。
* 如果不知道怎么安装 ES ,可以看 https://blog.csdn.net/chengyuqiang/article/details/78837712 简单。
*/
public class FieldAnalyzer {
/**
* IK 最大化分词
*
* 会将文本做最细粒度的拆分
*/
public static final String IK_MAX_WORD = "ik_max_word";
/**
* IK 智能分词
*
* 会做最粗粒度的拆分
*/
public static final String IK_SMART = "ik_smart";
}

View File

@@ -0,0 +1,114 @@
package cn.iocoder.springboot.lab15.springdataelasticsearch.dataobject;
import cn.iocoder.springboot.lab15.springdataelasticsearch.constant.FieldAnalyzer;
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;
@Document(indexName = "product", // 索引名
type = "product", // 类型。未来的版本即将废弃
shards = 1, // 默认索引分区数
replicas = 0, // 每个分区的备份数
refreshInterval = "-1" // 刷新间隔
)
public class ESProductDO {
/**
* ID 主键
*/
@Id
private Integer id;
/**
* SPU 名字
*/
@Field(analyzer = FieldAnalyzer.IK_MAX_WORD, type = FieldType.Text)
private String name;
/**
* 卖点
*/
@Field(analyzer = FieldAnalyzer.IK_MAX_WORD, type = FieldType.Text)
private String sellPoint;
/**
* 描述
*/
@Field(analyzer = FieldAnalyzer.IK_MAX_WORD, type = FieldType.Text)
private String description;
/**
* 分类编号
*/
private Integer cid;
/**
* 分类名
*/
@Field(analyzer = FieldAnalyzer.IK_MAX_WORD, type = FieldType.Text)
private String categoryName;
public Integer getId() {
return id;
}
public ESProductDO setId(Integer id) {
this.id = id;
return this;
}
public String getName() {
return name;
}
public ESProductDO setName(String name) {
this.name = name;
return this;
}
public String getSellPoint() {
return sellPoint;
}
public ESProductDO setSellPoint(String sellPoint) {
this.sellPoint = sellPoint;
return this;
}
public String getDescription() {
return description;
}
public ESProductDO setDescription(String description) {
this.description = description;
return this;
}
public Integer getCid() {
return cid;
}
public ESProductDO setCid(Integer cid) {
this.cid = cid;
return this;
}
public String getCategoryName() {
return categoryName;
}
public ESProductDO setCategoryName(String categoryName) {
this.categoryName = categoryName;
return this;
}
@Override
public String toString() {
return "ProductDO{" +
"id=" + id +
", name='" + name + '\'' +
", sellPoint='" + sellPoint + '\'' +
", description='" + description + '\'' +
", cid=" + cid +
", categoryName='" + categoryName + '\'' +
'}';
}
}

View File

@@ -0,0 +1,8 @@
package cn.iocoder.springboot.lab15.springdataelasticsearch.repository;
import cn.iocoder.springboot.lab15.springdataelasticsearch.dataobject.ESProductDO;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ProductRepository extends ElasticsearchRepository<ESProductDO, Integer> {
}

View File

@@ -0,0 +1,8 @@
spring:
data:
# Elasticsearch 配置项
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 127.0.0.1:9300
repositories:
enable: true

View File

@@ -0,0 +1,61 @@
package cn.iocoder.springboot.lab15.springdataelasticsearch.repository;
import cn.iocoder.springboot.lab15.springdataelasticsearch.Application;
import cn.iocoder.springboot.lab15.springdataelasticsearch.dataobject.ESProductDO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.Optional;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ProductRepositoryTest {
@Autowired
private ProductRepository productRepository;
@Test // 插入一条记录
public void testInsert() {
ESProductDO product = new ESProductDO();
product.setId(1); // 一般 ES 的 ID 编号,使用 DB 数据对应的编号。这里,先写死
product.setName("芋道源码");
product.setSellPoint("愿半生编码,如一生老友");
product.setDescription("我只是一个描述");
product.setCid(1);
product.setCategoryName("技术");
productRepository.save(product);
}
// 这里要注意,如果使用 save 方法来更新的话,必须是全量字段,否则其它字段会被覆盖。
// 所以,这里仅仅是作为一个示例。
@Test // 更新一条记录
public void testUpdate() {
ESProductDO product = new ESProductDO();
product.setId(1);
product.setCid(2);
product.setCategoryName("技术-Java");
productRepository.save(product);
}
@Test // 根据 ID 编号,删除一条记录
public void testDelete() {
productRepository.deleteById(1);
}
@Test // 根据 ID 编号,查询一条记录
public void testSelectById() {
Optional<ESProductDO> userDO = productRepository.findById(1);
System.out.println(userDO.isPresent());
}
@Test // 根据 ID 编号数组,查询多条记录
public void testSelectByIds() {
Iterable<ESProductDO> users = productRepository.findAllById(Arrays.asList(1, 4));
users.forEach(System.out::println);
}
}

View File

@@ -6,9 +6,17 @@ import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Document(indexName = "product", type = "product", shards = 1, replicas = 0, refreshInterval = "-1")
@Document(indexName = "product", // 索引名
type = "product", // 类型。未来的版本即将废弃
shards = 1, // 默认索引分区数
replicas = 0, // 每个分区的备份数
refreshInterval = "-1" // 刷新间隔
)
public class ESProductDO {
/**
* ID 主键
*/
@Id
private Integer id;

View File

@@ -5,7 +5,4 @@ import org.springframework.data.elasticsearch.repository.ElasticsearchRepository
public interface ProductRepository extends ElasticsearchRepository<ESProductDO, Integer> {
}

View File

@@ -13,6 +13,7 @@
<packaging>pom</packaging>
<modules>
<module>lab-15-spring-data-jest</module>
<module>lab-15-spring-data-elasticsearch</module>
</modules>