ElasticSearch是咱们常常用到的搜索引擎之一,本篇博客从零开始使用docker安装elasticsearch,elasticsearch-head而后整合Spring Boot对数据进行新增和查询。因为篇幅缘由,后面会分两篇blog实战使用分词器以及拼音搜索功能。html
添加咱们须要使用的依赖,除了elasticsearch
其余两个非必要,web
是为了方便调试,若是没有使用过lombok
的须要安装IDEA/Eclipse
插件。java
<!-- elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- web方便调试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
复制代码
由于个人电脑是Windows10,因此这里咱们选择使用docker来安装elasticsearch,操做简单。没有安装docker的能够试一下。docker win10安装教学。node
elasticsearch
,下载本身须要的版本,我这边就下载最新版。
// shell命令
docker network create somenetwork
docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:tag
复制代码
docker ps
看一下容器是否启动成功。
dockerhub
的时候发现elasticsearch-head
404了,执行docker pull mobz/elasticsearch-head
也不能够,可是好在docker pull mobz/elasticsearch-head:5
是能够拉取的,安装并启动。// shell命令
docker pull mobz/elasticsearch-head:5
docker run -d --name es_admin -p 9100:9100 mobz/elasticsearch-head:5
复制代码
// shell命令
// 进入容器
docker exec -it elasticsearch /bin/bash
// 修改配置文件
vi ./config/elasticsearch.yml
// 添加如下配置
http.cors.enabled: true
http.cors.allow-origin: "*"
// ctrl + c,:wq!保存配置
// 退出容器
exit
// 重启elasticsearch
docker restart elasticsearch
复制代码
再从新访问一下http://127.0.0.1:9100/,链接成功了。 nginx
# application.yml
server:
port: 8080
spring:
application:
name: elasticsearch-demo
elasticsearch:
rest:
uris: http://127.0.0.1:9200
connection-timeout: 1s
read-timeout: 30s
复制代码
// UserEntity
package com.example.elasticsearch.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Document(indexName = "user")
public class UserEntity {
@Id
private Long userId;
private String userName;
private Integer age;
private Integer sex;
}
// UserRepository
package com.example.elasticsearch.repository;
import com.example.elasticsearch.entity.UserEntity;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface UserRepository extends ElasticsearchRepository<UserEntity, Long> {
}
// ElasticSearchService
package com.example.elasticsearch.service;
import com.example.elasticsearch.entity.UserEntity;
import java.util.List;
public interface ElasticSearchService {
void saveUser( UserEntity userEntity );
void saveUser( List<UserEntity> userEntity );
UserEntity findById(Long id);
}
// ElasticSearchServiceImpl
package com.example.elasticsearch.service.impl;
import com.example.elasticsearch.entity.UserEntity;
import com.example.elasticsearch.repository.UserRepository;
import com.example.elasticsearch.service.ElasticSearchService;
import org.springframework.stereotype.Service;
import java.util.List;
import javax.annotation.Resource;
@Service
public class ElasticSearchServiceImpl implements ElasticSearchService {
@Resource
private UserRepository userRepository;
@Override
public void saveUser( UserEntity userEntity ) {
userRepository.save(userEntity);
}
@Override
public void saveUser( List<UserEntity> userEntity ) {
userEntity.containsAll(userEntity);
}
@Override
public UserEntity findById( Long id ) {
return userRepository.findById(id).get();
}
}
// Test
package com.example.elasticsearch;
import com.example.elasticsearch.entity.UserEntity;
import com.example.elasticsearch.service.ElasticSearchService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest
class ElasticsearchApplicationTests {
@Resource
private ElasticSearchService elasticSearchService;
@Test
void save() {
elasticSearchService.saveUser(UserEntity.builder()
.userId(1L).userName("David").age(18).sex(1)
.build());
}
@Test
void findById() {
UserEntity byId = elasticSearchService.findById(1L);
System.out.println(byId);
}
}
复制代码
数据插入成功后,head没法查看,由于咱们使用的head是5,可是es是7,解析格式不一样,因此咱们须要修改vendor.js
文件。web
// 将vendor.js拷贝至本地
docker cp es_admin:/usr/src/app/_site/vendor.js ./
// 修改6886和7573行 application/x-www-form-urlencoded修改成application/json;charset=UTF-8
// 将vendor.js拷贝到es_admines_admin原处
docker cp vendor.js es_admin:/usr/src/app/_site
复制代码
操做完成后直接刷新页面便可。 spring
ElasticSearch
是咱们常常用到的搜索引擎之一,本篇博客从零开始使用docker
安装elasticsearch
,elasticsearch-head
而后整合Spring Boot
对数据进行新增和查询。后面会分两篇blog实战使用分词器
以及拼音搜索
功能。使用docker能够为咱们很大的便利,docker对容器和镜像的管理使用起来很是简单,后面我还会使用docker来进行MongoDB实战,以及Linux的学习,你们能够到docker菜鸟教程学习一下,玩一玩儿就会了。docker