Skip to content

Commit 551750f

Browse files
committed
第九篇:SpringBoot整合elasticsearch
1 parent c675317 commit 551750f

10 files changed

Lines changed: 269 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
47.2 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip

springboot-elasticsearch/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.gf</groupId>
7+
<artifactId>springboot-elasticsearch</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>springboot-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.1.1.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>io.searchbox</groupId>
34+
<artifactId>jest</artifactId>
35+
<version>5.3.3</version>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-web</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
60+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.gf;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootElasticsearchApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootElasticsearchApplication.class, args);
11+
}
12+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.gf.entity;
2+
3+
4+
import io.searchbox.annotations.JestId;
5+
6+
public class Article {
7+
8+
@JestId
9+
private Integer id;
10+
private String author;
11+
private String title;
12+
private String content;
13+
14+
public Integer getId() {
15+
return id;
16+
}
17+
18+
public void setId(Integer id) {
19+
this.id = id;
20+
}
21+
22+
public String getAuthor() {
23+
return author;
24+
}
25+
26+
public void setAuthor(String author) {
27+
this.author = author;
28+
}
29+
30+
public String getTitle() {
31+
return title;
32+
}
33+
34+
public void setTitle(String title) {
35+
this.title = title;
36+
}
37+
38+
public String getContent() {
39+
return content;
40+
}
41+
42+
public void setContent(String content) {
43+
this.content = content;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
final StringBuilder sb = new StringBuilder( "{\"Article\":{" );
49+
sb.append( "\"id\":" )
50+
.append( id );
51+
sb.append( ",\"author\":\"" )
52+
.append( author ).append( '\"' );
53+
sb.append( ",\"title\":\"" )
54+
.append( title ).append( '\"' );
55+
sb.append( ",\"content\":\"" )
56+
.append( content ).append( '\"' );
57+
sb.append( "}}" );
58+
return sb.toString();
59+
}
60+
61+
62+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.gf.entity;
2+
3+
import org.springframework.data.elasticsearch.annotations.Document;
4+
5+
@Document( indexName = "gf" , type = "book")
6+
public class Book {
7+
private Integer id;
8+
private String bookName;
9+
private String author;
10+
11+
public Integer getId() {
12+
return id;
13+
}
14+
15+
public void setId(Integer id) {
16+
this.id = id;
17+
}
18+
19+
public String getBookName() {
20+
return bookName;
21+
}
22+
23+
public void setBookName(String bookName) {
24+
this.bookName = bookName;
25+
}
26+
27+
public String getAuthor() {
28+
return author;
29+
}
30+
31+
public void setAuthor(String author) {
32+
this.author = author;
33+
}
34+
35+
@Override
36+
public String toString() {
37+
final StringBuilder sb = new StringBuilder( "{\"Book\":{" );
38+
sb.append( "\"id\":" )
39+
.append( id );
40+
sb.append( ",\"bookName\":\"" )
41+
.append( bookName ).append( '\"' );
42+
sb.append( ",\"author\":\"" )
43+
.append( author ).append( '\"' );
44+
sb.append( "}}" );
45+
return sb.toString();
46+
}
47+
48+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.gf.repository;
2+
3+
4+
import com.gf.entity.Book;
5+
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
6+
7+
import java.util.List;
8+
9+
public interface BookRepository extends ElasticsearchRepository<Book, Integer>{
10+
11+
List<Book> findByBookNameLike(String bookName);
12+
13+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring.elasticsearch.jest.uris=http://127.0.0.1:9200
2+
3+
spring.data.elasticsearch.cluster-name=docker-cluster
4+
spring.data.elasticsearch.cluster-nodes=192.168.31.143:9300
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.gf;
2+
3+
import com.gf.entity.Article;
4+
import com.gf.entity.Book;
5+
import com.gf.repository.BookRepository;
6+
import io.searchbox.client.JestClient;
7+
import io.searchbox.core.Index;
8+
import io.searchbox.core.Search;
9+
import io.searchbox.core.SearchResult;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.test.context.junit4.SpringRunner;
15+
16+
import java.io.IOException;
17+
import java.util.List;
18+
19+
@RunWith(SpringRunner.class)
20+
@SpringBootTest
21+
public class SpringbootElasticsearchApplicationTests {
22+
23+
@Autowired
24+
BookRepository bookRepository;
25+
26+
@Test
27+
public void createIndex2(){
28+
Book book = new Book();
29+
book.setId(1);
30+
book.setBookName("西游记");
31+
book.setAuthor( "吴承恩" );
32+
bookRepository.index( book );
33+
}
34+
35+
@Test
36+
public void useFind() {
37+
List<Book> list = bookRepository.findByBookNameLike( "游" );
38+
for (Book book : list) {
39+
System.out.println(book);
40+
}
41+
42+
}
43+
44+
}

0 commit comments

Comments
 (0)