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