Skip to content

Commit 37a4eb4

Browse files
author
Jorgen-5
committed
Add stemming
1 parent 3d78703 commit 37a4eb4

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

klass-api/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM gcr.io/distroless/java17
1+
FROM eclipse-temurin:17-jre
22
ENV TZ="Europe/Oslo"
33
WORKDIR /app
44
COPY target/*.war app.war

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.*;
66
import java.util.stream.Collectors;
77

8+
import jakarta.annotation.PostConstruct;
89
import no.ssb.klass.core.model.*;
910
import no.ssb.klass.core.repository.ClassificationSeriesRepository;
1011
import no.ssb.klass.core.util.TimeUtil;
@@ -14,6 +15,7 @@
1415
import org.springframework.beans.factory.annotation.Autowired;
1516
import org.springframework.beans.factory.annotation.Qualifier;
1617
import org.springframework.beans.factory.annotation.Value;
18+
import org.springframework.data.elasticsearch.core.document.Document;
1719
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
1820
import org.springframework.data.elasticsearch.core.query.IndexQuery;
1921
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
@@ -45,6 +47,67 @@ private IndexCoordinates getIndexCoordinates() {
4547
return IndexCoordinates.of(elasticsearchIndex);
4648
}
4749

50+
51+
@PostConstruct
52+
private void createIndexWithStemmingAnalyzer() {
53+
try {
54+
var indexOps = elasticsearchOperations.indexOps(getIndexCoordinates());
55+
56+
if (indexOps.exists()) {
57+
log.info("Index '{}' already exists — skipping creation.", elasticsearchIndex);
58+
return;
59+
}
60+
61+
Map<String, Object> settings = Map.of(
62+
"analysis", Map.of(
63+
"analyzer", Map.of(
64+
"norwegian_stemmer_analyzer", Map.of(
65+
"type", "custom",
66+
"tokenizer", "standard",
67+
"filter", List.of("lowercase", "norwegian_stemmer")
68+
)
69+
),
70+
"filter", Map.of(
71+
"norwegian_stemmer", Map.of(
72+
"type", "stemmer",
73+
"name", "norwegian"
74+
)
75+
)
76+
)
77+
);
78+
79+
Map<String, Object> mappings = Map.of(
80+
"properties", Map.of(
81+
"title", Map.of(
82+
"type", "text",
83+
"analyzer", "norwegian_stemmer_analyzer"
84+
),
85+
"description", Map.of(
86+
"type", "text",
87+
"analyzer", "norwegian_stemmer_analyzer"
88+
),
89+
"family", Map.of(
90+
"type", "keyword"
91+
),
92+
"section", Map.of(
93+
"type", "keyword"
94+
)
95+
)
96+
);
97+
98+
boolean created = indexOps.create(settings);
99+
if (created) {
100+
indexOps.putMapping(Document.from(mappings));
101+
log.info("Created index '{}' with Norwegian stemming analyzer.", elasticsearchIndex);
102+
} else {
103+
log.warn("Failed to create index '{}'", elasticsearchIndex);
104+
}
105+
106+
} catch (Exception e) {
107+
log.error("Error creating index '{}': {}", elasticsearchIndex, e.getMessage(), e);
108+
}
109+
}
110+
48111
@Override
49112
@Async
50113
@Transactional(readOnly = true)

klass-api/src/test/java/no/ssb/klass/api/applicationtest/config/ApplicationTestConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ public IndexService indexService() {
5050
// For integration tests with mock-search profile, return a fully mocked SearchService
5151
return Mockito.mock(IndexService.class);
5252
}
53+
5354
}

0 commit comments

Comments
 (0)