Skip to content

Commit 4a3ce5a

Browse files
committed
add spring-boot-elasticsearch
1 parent d0c552b commit 4a3ce5a

File tree

7 files changed

+218
-0
lines changed

7 files changed

+218
-0
lines changed

spring-boot-elasticsearch/pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.neo</groupId>
7+
<artifactId>spring-boot-elasticsearch</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-elasticsearch</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.0.RELEASE</version>
18+
</parent>
19+
20+
<properties>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
<java.version>1.8</java.version>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-maven-plugin</artifactId>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
50+
51+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.neo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ElasticsearchApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ElasticsearchApplication.class, args);
11+
}
12+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
package com.neo.model;
3+
4+
import org.springframework.data.annotation.Id;
5+
import org.springframework.data.elasticsearch.annotations.Document;
6+
7+
@Document(indexName = "customer", type = "customer", shards = 1, replicas = 0, refreshInterval = "-1")
8+
public class Customer {
9+
10+
@Id
11+
private String id;
12+
13+
private String userName;
14+
15+
private String address;
16+
17+
public Customer() {
18+
}
19+
20+
public Customer(String userName, String address) {
21+
this.userName = userName;
22+
this.address = address;
23+
}
24+
25+
public String getId() {
26+
return this.id;
27+
}
28+
29+
public void setId(String id) {
30+
this.id = id;
31+
}
32+
33+
public String getUserName() {
34+
return userName;
35+
}
36+
37+
public void setUserName(String userName) {
38+
this.userName = userName;
39+
}
40+
41+
public String getAddress() {
42+
return address;
43+
}
44+
45+
public void setAddress(String address) {
46+
this.address = address;
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return String.format("Customer[id=%s, userName='%s', address='%s']", this.id,
52+
this.userName, this.address);
53+
}
54+
55+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
package com.neo.repository;
3+
4+
import com.neo.model.Customer;
5+
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
6+
7+
import java.util.List;
8+
9+
public interface CustomerRepository extends ElasticsearchRepository<Customer, String> {
10+
11+
public Customer findByUserName(String userName);
12+
13+
public int deleteByUserName(String userName);
14+
15+
public List<Customer> findByAddress(String address);
16+
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.data.elasticsearch.cluster-nodes=localhost:9300
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.neo;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
7+
import org.springframework.test.context.junit4.SpringRunner;
8+
9+
@RunWith(SpringRunner.class)
10+
@SpringBootTest
11+
public class ElasticsearchApplicationTests {
12+
13+
@Test
14+
public void contextLoads() {
15+
System.out.println("Spring Boot Test");
16+
}
17+
18+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.neo.repository;
2+
3+
4+
import com.neo.model.Customer;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
11+
@RunWith(SpringRunner.class)
12+
@SpringBootTest
13+
public class CustomerRepositoryTest {
14+
@Autowired
15+
private CustomerRepository repository;
16+
17+
@Test
18+
public void saveCustomers() {
19+
repository.save(new Customer("Alice", "北京"));
20+
repository.save(new Customer("Bob", "北京"));
21+
repository.save(new Customer("neo", "西安"));
22+
repository.save(new Customer("summer", "烟台"));
23+
}
24+
25+
@Test
26+
public void deleteCustomers() {
27+
// repository.deleteAll();
28+
repository.deleteByUserName("neo");
29+
}
30+
31+
@Test
32+
public void updateCustomers() {
33+
Customer customer= repository.findByUserName("summer");
34+
System.out.println(customer);
35+
customer.setAddress("北京市海淀区西直门");
36+
repository.save(customer);
37+
Customer xcustomer=repository.findByUserName("summer");
38+
System.out.println(xcustomer);
39+
}
40+
41+
@Test
42+
public void fetchAllCustomers() {
43+
System.out.println("Customers found with findAll():");
44+
System.out.println("-------------------------------");
45+
for (Customer customer : repository.findAll()) {
46+
System.out.println(customer);
47+
}
48+
System.out.println();
49+
}
50+
51+
@Test
52+
public void fetchIndividualCustomers() {
53+
System.out.println("Customer found with findByUserName('summer'):");
54+
System.out.println("--------------------------------");
55+
System.out.println(repository.findByUserName("summer"));
56+
57+
System.out.println("Customers found with findByAddress(\"北京\"):");
58+
System.out.println("--------------------------------");
59+
for (Customer customer : repository.findByAddress("北京")) {
60+
System.out.println(customer);
61+
}
62+
}
63+
64+
}

0 commit comments

Comments
 (0)