一、SpringBoot整合ES数据库
1、配置原生的依赖。
<properties>
<java.version>1.8java.version>
<elasticsearch.version>7.6.2elasticsearch.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-elasticsearchartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.75version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>joda-timegroupId>
<artifactId>joda-timeartifactId>
<version>2.10.5version>
dependency>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>transportartifactId>
<version>7.6.2version>
<exclusions>
<exclusion>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.elasticsearch.plugingroupId>
<artifactId>transport-netty4-clientartifactId>
<version>7.6.2version>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiterartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<version>2.3.3.RELEASEversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
2、构建对象。
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
//记得关闭
client.close();
3、分析对应的方法。
@Controller 控制器(注入服务)
用于标注控制层,相当于struts中的action层
@Service 服务(注入dao)
用于标注服务层,主要用来进行业务的逻辑处理
@Repository(实现dao访问)
用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件
@Component (把普通pojo实例化到spring容器中,相当于配置文件中的 )
泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。
@Data : 注解在类上, 为类提供读写属性, 此外还提供了 equals()、hashCode()、toString() 方法
@Getter/@Setter : 注解在类上, 为类提供读写属性
@ToString : 注解在类上, 为类提供 toString() 方法
@Slf4j : 注解在类上, 为类提供一个属性名为 log 的 log4j 的日志对象
@Log4j : 注解在类上, 为类提供一个属性名为 log 的 log4j 的日志对象
3、构建实体类和测试类
//User类
@Data
@Component
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
}
//测试程序
package com.www.es.springboot.demo;
import com.alibaba.fastjson.JSON;
import com.www.es.springboot.demo.pojo.User;
import com.www.es.springboot.demo.utils.ESconst;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
//面向对象来操作
@Autowired
@Qualifier("restHighLevelClient" )
private RestHighLevelClient client;
//测试索引的创建Request
@Test
void testCreateIndex() throws IOException {
// 1、创建索引请求
CreateIndexRequest request = new CreateIndexRequest("index1" );
// 2、客户端执行请求IndicesClient,请求后获得响应
CreateIndexResponse createIndexResponse =
client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
//测试获取索引
@Test
void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest( "index");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
//测试删除索引
@Test
void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("index1");
//删尉
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
//测试添加文档
@Test
void testAddDocument() throws IOException {
User user = new User("唐三藏",25);
//创建请求
IndexRequest indexRequest = new IndexRequest("index");
indexRequest.id("1");
indexRequest.timeout(TimeValue.timeValueSeconds(1));
indexRequest.timeout("1s");
//将我们的数据放入请求
indexRequest.source(JSON.toJSONString(user), XContentType.JSON);
//客户端发送请求
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
//获取响应的结果
System.out.println(indexResponse.toString());
System.out.println(indexResponse.status());
}
//获取文档,判断是否存在get /index/doc/
@Test
void testIsExists() throws IOException {
GetRequest getRequest = new GetRequest( "index", "1");
//不获取返回的_ source 的上下文了
//getRequest.fetchSourceContext (new FetchSourceContext(false));
//getRequest.storedFields("_none_");
boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
GetResponse getResponse = client.get(getRequest,RequestOptions.DEFAULT);
System.out.println(getResponse .getSourceAsString()); //打印文档的内容
System.out.println(getResponse);
}
//获取文档的信息
@Test
void testGetDocuments() throws IOException {
GetRequest getRequest = new GetRequest( "index", "1");
GetResponse getResponse = client.get(getRequest,RequestOptions.DEFAULT);
System.out.println(getResponse.getSourceAsString()); //打印文档的内容
System.out.println(getResponse);
}
//更新文档的信息
@Test
void testUpdateDocuments() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("index", "2");
updateRequest.timeout("1s");
User user = new User("杀阡陌", 18);
UpdateRequest doc = updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(doc);
System.out.println(updateResponse.status());
System.out.println(updateResponse);
}
//删除文档信息
@Test
void testDeleteDocuments() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("index", "2");
deleteRequest.timeout("1s");
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(deleteResponse.status());
System.out.println(deleteRequest);
}
// 特殊的,真的项目一般都会批量插入数据!
@Test
void testBulkRequest() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
ArrayList<User> userList = new ArrayList<>();
userList.add(new User("木sd1",3));
userList.add(new User("木s2",4));
userList.add(new User("木d3",5));
userList.add(new User("木e4",6));
userList.add(new User("木m5",7));
userList.add(new User("木g6",8));
userList.add(new User("木x7",9));
// 用下迭代器
/*for(int i = 0 ; i < userList.size();i++){
bulkRequest.add(
new IndexRequest("index")
.id(""+(i+1))
.source(JSON.toJSONString(userList.get(i)), XContentType.JSON));
}*/
for(Iterator<User> user = userList.iterator(); user.hasNext();){
System.out.println(user);
/*System.out.println(user.next().getClass().getName());
System.out.println(user.next());*/
bulkRequest.add(
new IndexRequest("index")
.source(JSON.toJSONString(user.next()), XContentType.JSON));
}
BulkResponse bulkItemResponses = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulkItemResponses.hasFailures());
}
// 搜索查询
// SearchRequest搜索请求
// SearchSourceBuiLder条件构造
// HighlightBuiLder构建高亮
// TermQueryBuilder精确查询
// MatchAlLQueryBuiLder
// xxx QueryBuilder对应我们刚才看到的命令!
@Test
void testSearch() throws IOException {
SearchRequest searchRequest = new SearchRequest(ESconst.ES_DATA_INDEX);
//构建搜索条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
final HighlightBuilder highlightBuilder = new HighlightBuilder();
SearchSourceBuilder highlighter = sourceBuilder.highlighter(highlightBuilder);
System.out.println(highlighter);
// 查询条件,我们可以使用 QueryBuilders 工具来实现
// QueryBuilders.termQuery精确
// QueryBuilders.matchAllQuery()匹配所有
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("age", "3");
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
//sourceBuilder.query(termQueryBuilder);
sourceBuilder.query(matchAllQueryBuilder);
searchRequest.source(sourceBuilder);
sourceBuilder.timeout(new TimeValue(120, TimeUnit.SECONDS));
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(searchResponse.getHits().getHits()));
System.out.println("=========================");
for(SearchHit documentFields:searchResponse.getHits().getHits()){
System.out.println(documentFields.getSourceAsMap());
}
}
}