Skip to content

Commit 88c7399

Browse files
committed
Spring Boot 中使用 Elasticsearch
1 parent 3777be2 commit 88c7399

5 files changed

Lines changed: 242 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.ymq.elasticsearch.config;
2+
3+
4+
import org.elasticsearch.client.Client;
5+
import org.elasticsearch.client.transport.TransportClient;
6+
import org.elasticsearch.common.settings.Settings;
7+
import org.elasticsearch.common.transport.InetSocketTransportAddress;
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
12+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
13+
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
14+
15+
import java.net.InetAddress;
16+
17+
/**
18+
* 描述:
19+
*
20+
* @author yanpenglei
21+
* @create 2017-11-01 17:19
22+
**/
23+
@Configuration
24+
@EnableElasticsearchRepositories(basePackages = "io.ymq.elasticsearch")
25+
public class ElasticsearchConfig {
26+
/**
27+
* elk集群地址
28+
*/
29+
@Value("${elasticsearch.ip}")
30+
private String hostName;
31+
/**
32+
* 端口
33+
*/
34+
@Value("${elasticsearch.port}")
35+
private String port;
36+
/**
37+
* 集群名称
38+
*/
39+
@Value("${elasticsearch.cluster.name}")
40+
private String clusterName;
41+
42+
/**
43+
* 连接池
44+
*/
45+
@Value("${elasticsearch.pool}")
46+
private String poolSize;
47+
48+
@Bean
49+
public Client client() throws Exception {
50+
51+
Settings esSettings = Settings.builder().put("cluster.name", clusterName)
52+
.put("client.transport.sniff", true)//增加嗅探机制,找到ES集群
53+
.put("thread_pool.search.size", Integer.parseInt(poolSize))//增加线程池个数,暂时设为5
54+
.build();
55+
56+
return TransportClient.builder()
57+
.settings(esSettings)
58+
.build()
59+
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostName), Integer.parseInt(port)));
60+
}
61+
62+
@Bean
63+
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
64+
return new ElasticsearchTemplate(client());
65+
}
66+
67+
68+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package io.ymq.elasticsearch.model;
2+
3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.elasticsearch.annotations.Document;
5+
6+
@Document(indexName = "book_index", type = "books")
7+
public class Book {
8+
9+
@Id
10+
private String id;
11+
12+
private String title;
13+
14+
private String author;
15+
16+
private String releaseDate;
17+
18+
public Book() {
19+
}
20+
21+
public Book(String id, String title, String author, String releaseDate) {
22+
this.id = id;
23+
this.title = title;
24+
this.author = author;
25+
this.releaseDate = releaseDate;
26+
}
27+
28+
public String getId() {
29+
return id;
30+
}
31+
32+
public void setId(String id) {
33+
this.id = id;
34+
}
35+
36+
public String getTitle() {
37+
return title;
38+
}
39+
40+
public void setTitle(String title) {
41+
this.title = title;
42+
}
43+
44+
public String getAuthor() {
45+
return author;
46+
}
47+
48+
public void setAuthor(String author) {
49+
this.author = author;
50+
}
51+
52+
public String getReleaseDate() {
53+
return releaseDate;
54+
}
55+
56+
public void setReleaseDate(String releaseDate) {
57+
this.releaseDate = releaseDate;
58+
}
59+
60+
@Override
61+
public String toString() {
62+
return "Book{" +
63+
"id='" + id + '\'' +
64+
", title='" + title + '\'' +
65+
", author='" + author + '\'' +
66+
", releaseDate='" + releaseDate + '\'' +
67+
'}';
68+
}
69+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.ymq.elasticsearch.repository;
2+
3+
import io.ymq.elasticsearch.model.Book;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.Pageable;
6+
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
7+
8+
import java.util.List;
9+
10+
/**
11+
* 描述:
12+
*
13+
* @author yanpenglei
14+
* @create 2017-11-02 14:47
15+
**/
16+
public interface BookRepository extends ElasticsearchRepository<Book, String> {
17+
18+
Page<Book> findByAuthor(String author, Pageable pageable);
19+
20+
List<Book> findByTitle(String title);
21+
22+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.ymq.elasticsearch.service;
2+
3+
import io.ymq.elasticsearch.model.Book;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.PageRequest;
6+
7+
import java.util.List;
8+
9+
/**
10+
* 描述:
11+
*
12+
* @author yanpenglei
13+
* @create 2017-11-02 14:48
14+
**/
15+
public interface BookService {
16+
17+
Book save(Book book);
18+
19+
void delete(Book book);
20+
21+
Book findOne(String id);
22+
23+
Iterable<Book> findAll();
24+
25+
Page<Book> findByAuthor(String author, PageRequest pageRequest);
26+
27+
List<Book> findByTitle(String title);
28+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.ymq.elasticsearch.service;
2+
3+
import io.ymq.elasticsearch.model.Book;
4+
import io.ymq.elasticsearch.repository.BookRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.data.domain.Page;
7+
import org.springframework.data.domain.PageRequest;
8+
import org.springframework.stereotype.Service;
9+
10+
import java.util.List;
11+
12+
/**
13+
* 描述:
14+
*
15+
* @author yanpenglei
16+
* @create 2017-11-02 14:49
17+
**/
18+
@Service
19+
public class BookServiceImpl implements BookService {
20+
21+
@Autowired
22+
private BookRepository bookRepository;
23+
24+
25+
@Override
26+
public Book save(Book book) {
27+
return bookRepository.save(book);
28+
}
29+
30+
@Override
31+
public void delete(Book book) {
32+
bookRepository.delete(book);
33+
}
34+
35+
@Override
36+
public Book findOne(String id) {
37+
return bookRepository.findOne(id);
38+
}
39+
40+
@Override
41+
public Iterable<Book> findAll() {
42+
return bookRepository.findAll();
43+
}
44+
45+
@Override
46+
public Page<Book> findByAuthor(String author, PageRequest pageRequest) {
47+
return bookRepository.findByAuthor(author, pageRequest);
48+
}
49+
50+
@Override
51+
public List<Book> findByTitle(String title) {
52+
return bookRepository.findByTitle(title);
53+
}
54+
55+
}

0 commit comments

Comments
 (0)