-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# 电影搜索服务 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
# 课程demo | ||
``` | ||
``` | ||
## 相关资料 | ||
- https://spring.io/projects/spring-data-elasticsearch#overview |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
version: '2.2' | ||
services: | ||
elasticsearch: | ||
image: docker.elastic.co/elasticsearch/elasticsearch:6.2.2 | ||
container_name: es622 | ||
environment: | ||
- cluster.name=springboot-demo | ||
- node.name=es622 | ||
- bootstrap.memory_lock=true | ||
- "ES_JAVA_OPTS=-Xms512m -Xmx512m" | ||
ulimits: | ||
memlock: | ||
soft: -1 | ||
hard: -1 | ||
volumes: | ||
- es6data:/usr/share/elasticsearch/data | ||
ports: | ||
- 9200:9200 | ||
- 9300:9300 | ||
volumes: | ||
es6data: | ||
driver: local |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
version: '2.2' | ||
services: | ||
elasticsearch: | ||
image: docker.elastic.co/elasticsearch/elasticsearch:7.1.0 | ||
container_name: es710 | ||
environment: | ||
- cluster.name=springboot-demo | ||
- node.name=es710 | ||
- bootstrap.memory_lock=true | ||
- "ES_JAVA_OPTS=-Xms512m -Xmx512m" | ||
- discovery.seed_hosts=es710 | ||
- cluster.initial_master_nodes=es710 | ||
ulimits: | ||
memlock: | ||
soft: -1 | ||
hard: -1 | ||
volumes: | ||
- es7data:/usr/share/elasticsearch/data | ||
ports: | ||
- 9200:9200 | ||
- 9300:9300 | ||
volumes: | ||
es7data: | ||
driver: local |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?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> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.3.RELEASE</version> | ||
<relativePath/> | ||
</parent> | ||
|
||
<groupId>geektime</groupId> | ||
<artifactId>sample-springboot-elasticsearch</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</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> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>elasticsearch</artifactId> | ||
<version>1.11.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.github.javafaker</groupId> | ||
<artifactId>javafaker</artifactId> | ||
<version>1.0.0</version> | ||
</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,3 @@ | ||
## Elasticsearch with Spring Boot [![Twitter](https://img.shields.io/twitter/follow/piotr_minkowski.svg?style=social&logo=twitter&label=Follow%20Me)](https://twitter.com/piotr_minkowski) | ||
|
||
Detailed description can be found here: [Elasticsearch with Spring Boot](https://piotrminkowski.wordpress.com/2019/03/29/elsticsearch-with-spring-boot/) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package geektime.demo.services.elasticsearch; | ||
|
||
public interface Constants { | ||
public static String EMPLOYEE_INDEX ="employees"; | ||
public static String EMPLOYEE_INDEX_TYPE ="_doc"; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package geektime.demo.services.elasticsearch; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; | ||
|
||
@SpringBootApplication | ||
@EnableElasticsearchRepositories | ||
public class SampleApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SampleApplication.class, args); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnProperty("initial-import.enabled") | ||
public SampleDataSet dataSet() { | ||
return new SampleDataSet(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package geektime.demo.services.elasticsearch; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.javafaker.Faker; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; | ||
import org.springframework.data.elasticsearch.core.query.IndexQuery; | ||
import geektime.demo.services.elasticsearch.model.Department; | ||
import geektime.demo.services.elasticsearch.model.Employee; | ||
import geektime.demo.services.elasticsearch.model.Organization; | ||
import geektime.demo.services.elasticsearch.repository.EmployeeRepository; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class SampleDataSet { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(SampleDataSet.class); | ||
|
||
@Autowired | ||
EmployeeRepository repository; | ||
@Autowired | ||
ElasticsearchTemplate template; | ||
|
||
@PostConstruct | ||
public void init() { | ||
for (int i = 0; i < 10; i++) { | ||
bulk(i); | ||
} | ||
} | ||
|
||
public void bulk(int ii) { | ||
try { | ||
// check if the index is existed | ||
if (!template.indexExists(Constants.EMPLOYEE_INDEX)) { | ||
template.createIndex(Constants.EMPLOYEE_INDEX); | ||
} | ||
ObjectMapper mapper = new ObjectMapper(); | ||
List<IndexQuery> queries = new ArrayList<>(); | ||
List<Employee> employees = rndEmployees(); | ||
for (Employee employee : employees) { | ||
IndexQuery indexQuery = new IndexQuery(); | ||
indexQuery.setId(employee.getId().toString()); | ||
indexQuery.setSource(mapper.writeValueAsString(employee)); | ||
//Set the index name & doc type | ||
indexQuery.setIndexName(Constants.EMPLOYEE_INDEX); | ||
indexQuery.setType(Constants.EMPLOYEE_INDEX_TYPE); | ||
queries.add(indexQuery); | ||
} | ||
if (queries.size() > 0) { | ||
template.bulkIndex(queries); | ||
} | ||
template.refresh(Constants.EMPLOYEE_INDEX); | ||
LOGGER.info("BulkIndex completed: {}", ii); | ||
} catch (Exception e) { | ||
LOGGER.error("Error bulk index", e); | ||
} | ||
} | ||
|
||
private List<Employee> rndEmployees(){ | ||
List<Employee> employees = new ArrayList<>(); | ||
int id = (int) repository.count(); | ||
LOGGER.info("Starting from id: {}", id); | ||
for (int i = id; i < 100 + id; i++) { | ||
Random r = new Random(); | ||
Faker faker = new Faker(); | ||
Employee employee = new Employee(); | ||
employee.setId((long) i); | ||
employee.setName(faker.name().username()); | ||
employee.setAge(r.nextInt(60)); | ||
employee.setPosition(faker.job().position()); | ||
int departmentId = r.nextInt(5000); | ||
employee.setDepartment(new Department((long) departmentId, faker.company().name())); | ||
int organizationId = departmentId % 100; | ||
employee.setOrganization(new Organization((long) organizationId, "TestO" + organizationId, "Test Street No. " + organizationId)); | ||
employees.add(employee); | ||
} | ||
return employees; | ||
|
||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package geektime.demo.services.elasticsearch.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
import geektime.demo.services.elasticsearch.model.Employee; | ||
import geektime.demo.services.elasticsearch.repository.EmployeeRepository; | ||
|
||
@RestController | ||
@RequestMapping("/employees") | ||
public class EmployeeController { | ||
|
||
@Autowired | ||
EmployeeRepository repository; | ||
|
||
@PostMapping | ||
public Employee add(@RequestBody Employee employee) { | ||
return repository.save(employee); | ||
} | ||
|
||
@GetMapping("/{name}") | ||
public List<Employee> findByName(@PathVariable("name") String name) { | ||
return repository.findByName(name); | ||
} | ||
|
||
@GetMapping("/organization/{organizationName}") | ||
public List<Employee> findByOrganizationName(@PathVariable("organizationName") String organizationName) { | ||
return repository.findByOrganizationName(organizationName); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package geektime.demo.services.elasticsearch.model; | ||
|
||
public class Department { | ||
private Long id; | ||
private String name; | ||
|
||
public Department() { | ||
} | ||
public Department(Long id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
public String getName() { | ||
return name; | ||
} | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Department{" + | ||
"id=" + id + | ||
", name='" + name + '\'' + | ||
'}'; | ||
} | ||
} |