Springboot整合ElasticSearch
第一步导入maven依赖,pom.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bdcloud</groupId>
<artifactId>Bdcloud-ElasticSearch</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.5.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.5.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
第二步,编写application.xml文件
spring:
application:
name: bdcloud-elasticsearch
data:
elasticsearch:
cluster-name: es-itcast-cluster
cluster-nodes: 10.212.130.5:9300,10.212.130.5:9301,10.212.130.5:9302
第三步,编写启动类
package com.bdcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EsApplication {
public static void main(String[] args) {
SpringApplication.run(EsApplication.class,args);
}
}
第四步,编写bean类
package com.bdcloud.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "itcast",type = "user",shards = 6,replicas = 1)
public class User {
@Id
private Long id;
@Field(store = true)
private String name;
@Field
private Integer age;
@Field
private String hobby;
}
第五步,编写测试类,测试ElasticSearchTemplate
package com.bdcloud;
import com.bdcloud.bean.User;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestSpringBootES {
@Autowired
private ElasticsearchTemplate esTemplate;
@Test
public void testCreate(){
User user = new User();
user.setName("原倩倩");
user.setAge(18);
user.setHobby("吃东西");
user.setId(1001L);
IndexQuery query = new IndexQueryBuilder().withObject(user).build();
String index = esTemplate.index(query);
System.out.println(index);
}
@Test
public void testSayHello(){
System.out.println("hello");
}
@Test
public void testBulk() {
List list = new ArrayList();
for (int i = 0; i < 5000; i++) {
User user = new User();
user.setId(1001L + i);
user.setAge(i % 50 + 10);
user.setName("张三" + i);
user.setHobby("足球、篮球、听音乐");
IndexQuery indexQuery = new
IndexQueryBuilder().withObject(user).build();
list.add(indexQuery);
}
Long start = System.currentTimeMillis();
this.esTemplate.bulkIndex(list);
System.out.println("用时:" + (System.currentTimeMillis() - start)); //用时:
}
/***
* 局部更新
*/
@Test
public void testUpdate(){
IndexRequest indexRequest = new IndexRequest();
indexRequest.source("age",30);
UpdateQuery updateQuery = new UpdateQueryBuilder().withId("1001").withClass(User.class).withIndexRequest(indexRequest).build();
UpdateResponse update = this.esTemplate.update(updateQuery);
System.out.println(update);
}
@Test
public void testDelete(){
this.esTemplate.delete(User.class,"1001");
}
@Test
public void testSearch(){
PageRequest pageRequest = PageRequest.of(0,10);//设置分页参数
SearchQuery query = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchQuery("name","张三"))
.withPageable(pageRequest).build();
AggregatedPage<User> users = this.esTemplate.queryForPage(query, User.class);
System.out.println("总页数="+users.getTotalPages());
for (User temp:users.getContent()
) {
System.out.println(temp);
}
}
}