Skip to content

Commit 24b1cbd

Browse files
authored
Merge pull request #6 from isystk/feature/solrj
SpringDataSolrを辞めてSolrJを導入
2 parents 96bcf12 + 0488843 commit 24b1cbd

File tree

8 files changed

+381
-128
lines changed

8 files changed

+381
-128
lines changed

batch/src/main/java/com/isystk/sample/batch/service/SolrStockService.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.isystk.sample.batch.service;
22

33
import com.google.common.collect.Lists;
4+
import com.isystk.sample.common.util.DateUtils;
45
import com.isystk.sample.common.util.ObjectMapperUtils;
56
import com.isystk.sample.domain.dto.StockCriteria;
67
import com.isystk.sample.domain.repository.StockRepository;
@@ -44,14 +45,20 @@ public void refresh() {
4445
.forEach(stock -> {
4546
SolrStock solrStock = ObjectMapperUtils.map(stock, SolrStock.class);
4647
solrStock.setStockId(stock.getId().intValue());
48+
solrStock.setCreatedAtDate(DateUtils.toDate(stock.getCreatedAt()));
4749
solrStockList.add(solrStock);
4850
});
4951

50-
// Solrをすべて削除
51-
solrStockRepository.deleteAll();
52+
try {
53+
// Solrをすべて削除
54+
solrStockRepository.deleteStockByQuery("*:*");
55+
56+
// Solrに保存
57+
solrStockRepository.addStocks(solrStockList);
58+
} catch (Exception e) {
59+
throw new RuntimeException(e);
60+
}
5261

53-
// Solrに保存
54-
solrStockRepository.saveAll(solrStockList);
5562
}
5663

5764
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ subprojects {
7070
runtimeOnly("org.springframework.boot:spring-boot-devtools")
7171

7272
/* solr */
73-
implementation("org.springframework.boot:spring-boot-starter-data-solr:${springBootSolrVersion}")
73+
implementation("org.apache.solr:solr-solrj:${solrJVersion}")
7474

7575
/* spring security */
7676
implementation("org.springframework.boot:spring-boot-starter-security")
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
package com.isystk.sample.solr;
22

3-
import org.apache.solr.client.solrj.SolrClient;
43
import org.apache.solr.client.solrj.impl.HttpSolrClient;
54
import org.springframework.beans.factory.annotation.Value;
65
import org.springframework.context.annotation.Bean;
7-
import org.springframework.data.solr.core.SolrTemplate;
8-
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
6+
import org.springframework.context.annotation.Configuration;
97

10-
@EnableSolrRepositories(basePackages = "com.isystk.sample.solr.repository")
118
public class BaseSolrConfig {
129

1310
@Value("#{'${spring.data.solr.host:*}'}")
14-
String solrURL;
11+
String solrHost;
1512

1613
@Bean
17-
public SolrClient solrClient() {
18-
return new HttpSolrClient.Builder(solrURL).build();
14+
public HttpSolrClient stockSolrClient() {
15+
return new HttpSolrClient.Builder(solrHost + "/stock").build();
1916
}
2017

21-
@Bean
22-
public SolrTemplate solrTemplate(SolrClient client) throws Exception {
23-
return new SolrTemplate(client);
24-
}
2518
}
Lines changed: 13 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,37 @@
11
package com.isystk.sample.solr.dto;
22

33
import com.isystk.sample.common.dto.Dto;
4-
import org.springframework.data.annotation.Id;
5-
import org.springframework.data.solr.core.mapping.Indexed;
6-
import org.springframework.data.solr.core.mapping.SolrDocument;
4+
import java.util.Date;
5+
import lombok.Data;
6+
import org.apache.solr.client.solrj.beans.Field;
77

88
import java.time.LocalDateTime;
99

10-
@SolrDocument(collection = "stock")
10+
@Data
1111
public class SolrStock implements Dto {
1212

13-
@Id
14-
@Indexed(name = "id")
13+
@Field("id")
1514
private String id;
1615

17-
@Indexed(name = "stock_id")
16+
@Field("stock_id")
1817
private Integer stockId;
1918

20-
@Indexed(name = "name")
19+
@Field("name")
2120
private String name;
2221

23-
@Indexed(name = "detail")
22+
@Field("detail")
2423
private String detail;
2524

26-
@Indexed(name = "price")
25+
@Field("price")
2726
private Integer price;
2827

29-
@Indexed(name = "imgpath")
28+
@Field("imgpath")
3029
private String imgpath;
3130

32-
@Indexed(name = "quantity")
31+
@Field("quantity")
3332
private Integer quantity;
3433

35-
@Indexed(name = "created_at")
36-
private LocalDateTime createdAt;
34+
@Field("created_at")
35+
private Date createdAtDate;
3736

38-
39-
public String getId() {
40-
return id;
41-
}
42-
43-
public void setId(String id) {
44-
this.id = id;
45-
}
46-
47-
public Integer getStockId() {
48-
return stockId;
49-
}
50-
51-
public void setStockId(Integer stockId) {
52-
this.stockId = stockId;
53-
}
54-
55-
public String getName() {
56-
return name;
57-
}
58-
59-
public void setName(String name) {
60-
this.name = name;
61-
}
62-
63-
public String getDetail() {
64-
return detail;
65-
}
66-
67-
public void setDetail(String detail) {
68-
this.detail = detail;
69-
}
70-
71-
public Integer getPrice() {
72-
return price;
73-
}
74-
75-
public void setPrice(Integer price) {
76-
this.price = price;
77-
}
78-
79-
public String getImgpath() {
80-
return imgpath;
81-
}
82-
83-
public void setImgpath(String imgpath) {
84-
this.imgpath = imgpath;
85-
}
86-
87-
public Integer getQuantity() {
88-
return quantity;
89-
}
90-
91-
public void setQuantity(Integer quantity) {
92-
this.quantity = quantity;
93-
}
94-
95-
public LocalDateTime getCreatedAt() {
96-
return createdAt;
97-
}
98-
99-
public void setCreatedAt(LocalDateTime createdAt) {
100-
this.createdAt = createdAt;
101-
}
10237
}

0 commit comments

Comments
 (0)