# springboot-demo
基于springboot后台框架,涉及技术,通用mapper,shiro,mysql,redis
## es模块
最近在学习es,起码要先有个es环境吧,然后再是整合到代码中使用一下,毕竟只有实践才会有深刻的记忆,这就是所谓的经验啊,下面开始吧,本文分两部分,第一部分配置es环境,第二部分整合到springboot中进行简单的操作,本人也是初次学习,如有错误欢迎指出修正,
本文示例代码不严谨,仅供参考
一、 docker 安装es
1、 拉取镜像
```
# 搜索镜像
docker search elasticsearch
# 拉取镜像
docker pull elasticsearch
```
2、创建本地映射文件,我的本地es映射主目录是/docker/es
```
cd /docker/es
mkdir data
mkdir plugins
mkdir config
cd config
vim es.yml
```
3、es.yml文件内容如下
```
cluster.name: elasticsearch-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: localhost
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
```
4、创建并启动容器
```
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 -p 5601:5601 --restart=always -v /docker/es/config/es.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /docker/es/plugins1:/usr/share/elasticsearch/plugins -v /docker/es/data1:/usr/share/elasticsearch/data --name my_es elasticsearch
# 命令解析
-e 指定运行内存
-d 后台运行
-p 指定端口
--restart=always 启动docker自动启动容器
-v 指定映射文件
--name 容器名称
最后的elasticsearch是刚才拉取的镜像名称
```
5、浏览器插件访问es,telnet9200可以通说明都可以说明搭建成功, http://127.0.0.1:9200/
二、 整合springboot里面去
1、pom.xml
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
```
2、application.yml
```
spring:
data:
elasticsearch:
cluster-name: elasticsearch-cluster
cluster-nodes: 127.0.0.1:9300
repositories:
enabled: true
```
3、 ElasticSearchConfig
```
@SpringBootConfiguration
public class ElasticSearchConfig {
@PostConstruct
void init() {
System.setProperty("es.set.netty.runtime.available.processors", "false");
}
}
```
4、 bean
```
ESDocument
@Data
@Document(indexName = "poms", type = "content")
public class ESDocument {
@Id
private String id;
@Field(analyzer = "ik_smart", searchAnalyzer = "ik_smart")
private String name;
private String projectId;
public ESDocument(String id, String name, String projectId) {
this.id = id;
this.name = name;
this.projectId = projectId;
}
public ESDocument() {
}
}
/**
* @Document 作用在类,标记实体类为文档对象,一般有两个属性
* indexName:对应索引库名称
* type:对应在索引库中的类型
* shards:分片数量,默认5
* replicas:副本数量,默认1
* @Id 作用在成员变量,标记一个字段作为id主键
* @Field 作用在成员变量,标记为文档的字段,并指定字段映射属性:
* type:字段类型,是枚举:FieldType,可以是text、long、short、date、integer、object等
* text:存储数据时候,会自动分词,并生成索引
* keyword:存储数据时候,不会分词建立索引
* Numerical:数值类型,分两类
* 基本数据类型:long、integer、short、byte、double、float、half_float
* 浮点数的高精度类型:scaled_float
* 需要指定一个精度因子,比如10或100。elasticsearch会把真实值乘以这个因子后存储,取出时再还原。
* Date:日期类型
* elasticsearch可以对日期格式化为字符串存储,但是建议我们存储为毫秒值,存储为long,节省空间。
* index:是否索引,布尔类型,默认是true
* store:是否存储,布尔类型,默认是false
* analyzer:分词器名称,这里的ik_max_word即使用ik分词器
*
*/
```
5、DocumentSearchService
```
void save(ESDocument esDocument);
void delete(String id);
void getById(String id);
void getByName(String name,String projectId);
```
6.DocumentSearchServiceImpl
```
@Autowired
private DocumentSearchRepository documentSearchRepository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Override
public void save(ESDocument esDocument) {
ESDocument save = documentSearchRepository.save(esDocument);
System.out.println(save.toString());
}
@Override
public void delete(String id) {
documentSearchRepository.deleteById(id);
}
@Override
public void getById(String id) {
ESDocument esDocument = documentSearchRepository.findById(id).orElse(new ESDocument());
System.out.println(esDocument.toString());
}
@Override
public void getByName(String name, String projectId) {
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("name",name)).withQuery(matchQuery("projectId",projectId)).build();
List<ESDocument> esDocuments = elasticsearchTemplate.queryForList(searchQuery, ESDocument.class);
esDocuments.forEach(e-> System.out.println(e.toString()));
}
```
7、DocumentSearchRepository
```
@Repository
public interface DocumentSearchRepository extends ElasticsearchRepository<ESDocument,String> {
}
```
8、test
```
@RunWith(SpringRunner.class)
@SpringBootTest
public class EsTests {
@Autowired
private DocumentSearchService documentSearchService;
@Test
public void save() {
documentSearchService.save(new ESDocument(UUID.randomUUID().toString(),"name1","1"));
}
@Test
public void getById(){
documentSearchService.getById("98c717e2-0e17-4887-86f6-e9cd347f97f7");
}
@Test
public void getByName(){
documentSearchService.getByName("name1","1");
}
@Test
public void delete(){
documentSearchService.delete("98c717e2-0e17-4887-86f6-e9cd347f97f7");
}
}
```
9、到这基本整合就结束了,展示了一个增删查,没有修改,修改本人暂时也没有看。,待后续学习继续分享经验吧,下面说一下整合过程遇到的坑
### 坑1
##### 报错信息
```
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren