|
5 | 5 | import java.util.*; |
6 | 6 | import java.util.stream.Collectors; |
7 | 7 |
|
| 8 | +import jakarta.annotation.PostConstruct; |
8 | 9 | import no.ssb.klass.core.model.*; |
9 | 10 | import no.ssb.klass.core.repository.ClassificationSeriesRepository; |
10 | 11 | import no.ssb.klass.core.util.TimeUtil; |
|
14 | 15 | import org.springframework.beans.factory.annotation.Autowired; |
15 | 16 | import org.springframework.beans.factory.annotation.Qualifier; |
16 | 17 | import org.springframework.beans.factory.annotation.Value; |
| 18 | +import org.springframework.data.elasticsearch.core.document.Document; |
17 | 19 | import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; |
18 | 20 | import org.springframework.data.elasticsearch.core.query.IndexQuery; |
19 | 21 | import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder; |
@@ -45,6 +47,67 @@ private IndexCoordinates getIndexCoordinates() { |
45 | 47 | return IndexCoordinates.of(elasticsearchIndex); |
46 | 48 | } |
47 | 49 |
|
| 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 | + |
48 | 111 | @Override |
49 | 112 | @Async |
50 | 113 | @Transactional(readOnly = true) |
|
0 commit comments