ElasticSearch是我们经常用到的搜索引擎之一,本篇博客从零开始使用docker安装elasticsearch,elasticsearch-head然后整合Spring Boot对数据进行新增和查询。由于篇幅原因,后面会分两篇blog实战使用分词器以及拼音搜索功能。
添加我们需要使用的依赖,除了 elasticsearch 其他两个非必要, web 是为了方便调试,如果没有使用过 lombok 的需要安装 IDEA/Eclipse 插件。
<!-- 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安装教学 。
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/ ,连接成功了。
# 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 文件。
// 将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 复制代码
操作完成后直接刷新页面即可。