21 springboot集成elasticsearch
2018-05-10 本文已影响0人
lijiaccy
首先安装elasticsearch(自己google)。注意的是:版本不同有各种坑,我这用的是6.X版本
官网下载,然后采用springboot官网上的一个例子。项目地址
配置文件
spring.data.elasticsearch.cluster-nodes=localhost:9300
#下面的按需求去加,可以不加,这就注释掉了,具体的在es配置文件里
#spring.data.elasticsearch.cluster-name=my-application
首先是pom主要文件。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
然后是bean。
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "customer", type = "customer", shards = 1, replicas = 0, refreshInterval = "-1")
public class Customer {
@Id
private String id;
private String firstName;
private String lastName;
public Customer() {
}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Customer(String id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return String.format("Customer[id=%s, firstName='%s', lastName='%s']", this.id,
this.firstName, this.lastName);
}
}
里面的index相当于db,type相当于table。然后就是属性。Id是主键。
操作类,这里是接口继承了ElasticsearchRepository
import java.util.List;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface CustomerRepository extends ElasticsearchRepository<Customer, String> {
public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}
最后是启动类和操作
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.annotation.Order;
@SpringBootApplication
public class App implements CommandLineRunner {
@Autowired
private CustomerRepository repository;
@Override
public void run(String... args) throws Exception {
this.repository.deleteAll();
saveCustomers();
fetchAllCustomers();
fetchIndividualCustomers();
}
private void saveCustomers() {
this.repository.save(new Customer("1","Alice", "Smith"));
this.repository.save(new Customer("2","Bob", "Smith"));
}
private void fetchAllCustomers() {
System.out.println("Customers found with findAll():");
System.out.println("==============================");
for (Customer customer : this.repository.findAll()) {
System.out.println(customer);
}
System.out.println();
}
private void fetchIndividualCustomers() {
System.out.println("Customer found with findByFirstName('Alice'):");
System.out.println("--------------------------------");
System.out.println(this.repository.findByFirstName("Alice"));
System.out.println("Customers found with findByLastName('Smith'):");
System.out.println("++++++++++++++++++++++++++++++++");
for (Customer customer : this.repository.findByLastName("Smith")) {
System.out.println(customer);
}
}
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
上面的实现了CommandLineRunner
,这个接口目的就是springboot加载之后就会执行这个接口的方法。
代码中注入了CustomerRepository
,然后以这个去操作es。最后得到的结果是

说明已经连接上es,并插入和查询成功。
这里面只继承了一个
ElasticsearchRepository
,然后用这个去做操作就行。最简单的的demo就好了