Skip to content

Commit 271164c

Browse files
authored
Feat/strategy for indexing and updating search (#400)
* remove duplicated code * log * possible reindex as strategy * possible set up job * set up scheduled indexing * add make method * test scheduling * set up scheduled job in separate service * set permission to admin to access refresh * reset permission * rename service * add test * rename - add docstring * delete test * test scheduled job every hour * update distroless image
1 parent 5495bd2 commit 271164c

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

.nais/test/job.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: batch/v1
2+
kind: Naisjob
3+
metadata:
4+
name: index-open-search
5+
namespace: dapla-metadata
6+
spec:
7+
schedule: "0 0 * * *"
8+
image: {{ image }}
9+
10+

klass-api/src/main/java/no/ssb/klass/KlassApiApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.context.annotation.Import;
1111
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration;
1212
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration;
13+
import org.springframework.scheduling.annotation.EnableScheduling;
1314

1415
@SpringBootApplication(exclude = {
1516
ElasticsearchDataAutoConfiguration.class,
@@ -18,6 +19,7 @@
1819
@Import(TomcatServletWebServerFactoryCustomizer.class)
1920
@ConfigurationPropertiesScan
2021
@ServletComponentScan
22+
@EnableScheduling
2123
public class KlassApiApplication extends SpringBootServletInitializer {
2224

2325
@Override

klass-api/src/main/java/no/ssb/klass/api/services/IndexService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package no.ssb.klass.api.services;
22

33
import no.ssb.klass.core.model.ClassificationSeries;
4+
import org.opensearch.index.reindex.ReindexRequest;
45
import org.springframework.scheduling.annotation.Async;
56
import org.springframework.transaction.annotation.Transactional;
67

@@ -36,4 +37,5 @@ public interface IndexService {
3637
*/
3738
@Transactional(readOnly = true)
3839
void indexSync(ClassificationSeries classificationSeries);
40+
3941
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package no.ssb.klass.api.services;
2+
3+
import no.ssb.klass.core.repository.ClassificationSeriesRepository;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.scheduling.annotation.Scheduled;
8+
import org.springframework.stereotype.Service;
9+
10+
import java.util.List;
11+
12+
/**
13+
* Service responsible for re-indexing classification series into the search index.
14+
*/
15+
@Service
16+
public class ReIndexService {
17+
private static final Logger log = LoggerFactory.getLogger(ReIndexService.class);
18+
19+
private final ClassificationSeriesRepository classificationRepository;
20+
private final IndexService indexService;
21+
22+
@Autowired
23+
public ReIndexService(ClassificationSeriesRepository classificationRepository, IndexService indexService) {
24+
this.classificationRepository = classificationRepository;
25+
this.indexService = indexService;
26+
}
27+
28+
/**
29+
* Executes a daily scheduled job
30+
* <p>
31+
* This method retrieves all classification IDs from repository and triggers
32+
* asynchronous indexing for each one. The job is scheduled to run every day at 23:30.
33+
* </p>
34+
*/
35+
@Scheduled(cron = "0 0 * * * *")
36+
public void runDailyIndexJob() {
37+
log.info("Starting scheduled index job at 23:30");
38+
39+
List<Long> ids = classificationRepository.findAllClassificationIds();
40+
for (Long id : ids) {
41+
indexService.indexAsync(id);
42+
}
43+
44+
log.info("Finished scheduled indexing for {} classifications", ids.size());
45+
}
46+
}

klass-api/src/main/resources/application-remote-open-search.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ opensearch.password=${OPEN_SEARCH_PASSWORD}
55
opensearch.ssl=True
66

77
logging.level.org.apache.http=DEBUG
8-
logging.level.org.opensearch.client=DEBUG
8+
logging.level.org.opensearch.client=DEBUG

0 commit comments

Comments
 (0)