Skip to content

Commit

Permalink
add spring-boot-elasticsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
ityouknow committed Apr 3, 2018
1 parent d0c552b commit 4a3ce5a
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 0 deletions.
51 changes: 51 additions & 0 deletions spring-boot-elasticsearch/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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.neo</groupId>
<artifactId>spring-boot-elasticsearch</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>spring-boot-elasticsearch</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.neo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ElasticsearchApplication {

public static void main(String[] args) {
SpringApplication.run(ElasticsearchApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

package com.neo.model;

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 userName;

private String address;

public Customer() {
}

public Customer(String userName, String address) {
this.userName = userName;
this.address = address;
}

public String getId() {
return this.id;
}

public void setId(String id) {
this.id = id;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return String.format("Customer[id=%s, userName='%s', address='%s']", this.id,
this.userName, this.address);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

package com.neo.repository;

import com.neo.model.Customer;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.List;

public interface CustomerRepository extends ElasticsearchRepository<Customer, String> {

public Customer findByUserName(String userName);

public int deleteByUserName(String userName);

public List<Customer> findByAddress(String address);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.data.elasticsearch.cluster-nodes=localhost:9300
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.neo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchApplicationTests {

@Test
public void contextLoads() {
System.out.println("Spring Boot Test");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.neo.repository;


import com.neo.model.Customer;
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.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepositoryTest {
@Autowired
private CustomerRepository repository;

@Test
public void saveCustomers() {
repository.save(new Customer("Alice", "北京"));
repository.save(new Customer("Bob", "北京"));
repository.save(new Customer("neo", "西安"));
repository.save(new Customer("summer", "烟台"));
}

@Test
public void deleteCustomers() {
// repository.deleteAll();
repository.deleteByUserName("neo");
}

@Test
public void updateCustomers() {
Customer customer= repository.findByUserName("summer");
System.out.println(customer);
customer.setAddress("北京市海淀区西直门");
repository.save(customer);
Customer xcustomer=repository.findByUserName("summer");
System.out.println(xcustomer);
}

@Test
public void fetchAllCustomers() {
System.out.println("Customers found with findAll():");
System.out.println("-------------------------------");
for (Customer customer : repository.findAll()) {
System.out.println(customer);
}
System.out.println();
}

@Test
public void fetchIndividualCustomers() {
System.out.println("Customer found with findByUserName('summer'):");
System.out.println("--------------------------------");
System.out.println(repository.findByUserName("summer"));

System.out.println("Customers found with findByAddress(\"北京\"):");
System.out.println("--------------------------------");
for (Customer customer : repository.findByAddress("北京")) {
System.out.println(customer);
}
}

}

0 comments on commit 4a3ce5a

Please sign in to comment.