|
| 1 | +package no.ssb.klass.api.services; |
| 2 | + |
| 3 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +import no.ssb.klass.core.model.*; |
| 8 | +import no.ssb.klass.core.repository.ClassificationSeriesRepository; |
| 9 | +import no.ssb.klass.core.util.TimeUtil; |
| 10 | +import org.opensearch.data.client.orhlc.OpenSearchRestTemplate; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 15 | +import org.springframework.beans.factory.annotation.Value; |
| 16 | +import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; |
| 17 | +import org.springframework.data.elasticsearch.core.query.IndexQuery; |
| 18 | +import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder; |
| 19 | +import org.springframework.scheduling.annotation.Async; |
| 20 | +import org.springframework.stereotype.Service; |
| 21 | +import org.springframework.transaction.annotation.Transactional; |
| 22 | + |
| 23 | +@Service |
| 24 | +public class IndexServiceImpl implements IndexService { |
| 25 | + |
| 26 | + private static final Logger log = LoggerFactory.getLogger(IndexServiceImpl.class); |
| 27 | + |
| 28 | + @Value("${klass.env.search.elasticsearch.index:klass}") |
| 29 | + protected String elasticsearchIndex; |
| 30 | + |
| 31 | + private final ClassificationSeriesRepository classificationRepository; |
| 32 | + private final OpenSearchRestTemplate elasticsearchOperations; |
| 33 | + |
| 34 | + @Autowired |
| 35 | + public IndexServiceImpl(ClassificationSeriesRepository classificationRepository, |
| 36 | + @Qualifier("opensearchRestTemplate") OpenSearchRestTemplate elasticsearchOperations) { |
| 37 | + this.classificationRepository = classificationRepository; |
| 38 | + this.elasticsearchOperations = elasticsearchOperations; |
| 39 | + } |
| 40 | + |
| 41 | + private IndexCoordinates getIndexCoordinates() { |
| 42 | + return IndexCoordinates.of(elasticsearchIndex); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + @Async |
| 47 | + @Transactional(readOnly = true) |
| 48 | + public void indexAsync(Long classificationSeriesId) { |
| 49 | + checkNotNull(classificationSeriesId); |
| 50 | + try { |
| 51 | + ClassificationSeries classification = classificationRepository.getOne(classificationSeriesId); |
| 52 | + indexSync(classification); |
| 53 | + } catch (Exception e) { |
| 54 | + log.warn("Failed to index classification {}: {}", classificationSeriesId, e.getMessage()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + @Transactional(readOnly = true) |
| 60 | + public void indexSync(ClassificationSeries classification) { |
| 61 | + Date start = TimeUtil.now(); |
| 62 | + for (Language language : Language.values()) { |
| 63 | + if (!classification.getName(language).isEmpty()) { |
| 64 | + Map<String, Object> doc = new HashMap<>(); |
| 65 | + doc.put("itemid", classification.getId()); |
| 66 | + doc.put("uuid", language.getLanguageCode() + "_" + classification.getUuid()); |
| 67 | + doc.put("language", language.getLanguageCode()); |
| 68 | + doc.put("type", classification.getClassificationType().getDisplayName(Language.EN)); |
| 69 | + doc.put("copyrighted", classification.isCopyrighted()); |
| 70 | + doc.put("published", classification.isPublished(language)); |
| 71 | + doc.put("title", classification.getName(language)); |
| 72 | + doc.put("description", classification.getDescription(language)); |
| 73 | + doc.put("family", classification.getClassificationFamily().getName(language)); |
| 74 | + doc.put("section", classification.getContactPerson().getSection()); |
| 75 | + |
| 76 | + List<String> codes = classification.getClassificationVersions().stream() |
| 77 | + .flatMap(version -> version.getAllClassificationItems().stream()) |
| 78 | + .map(item -> formatClassificationItem(language, item)) |
| 79 | + .toList(); |
| 80 | + doc.put("codes", codes); |
| 81 | + |
| 82 | + for (ClassificationVersion version : classification.getClassificationVersions()) { |
| 83 | + recursiveIndex(version, language); |
| 84 | + } |
| 85 | + |
| 86 | + updateElasticsearch(classification, doc); |
| 87 | + } |
| 88 | + } |
| 89 | + elasticsearchOperations.indexOps(getIndexCoordinates()).refresh(); |
| 90 | + log.info("Indexing: {} took (ms): {}", classification.getNameInPrimaryLanguage(), |
| 91 | + TimeUtil.millisecondsSince(start)); |
| 92 | + } |
| 93 | + |
| 94 | + private void recursiveIndex(ClassificationVersion version, Language language) { |
| 95 | + Map<String, Object> doc = new HashMap<>(); |
| 96 | + doc.put("itemid", version.getId()); |
| 97 | + doc.put("uuid", language.getLanguageCode() + "_" + version.getUuid()); |
| 98 | + doc.put("classificationId", version.getClassification().getId()); |
| 99 | + doc.put("language", language.getLanguageCode()); |
| 100 | + doc.put("type", "Version"); |
| 101 | + doc.put("title", version.getName(language)); |
| 102 | + doc.put("legalBase", version.getLegalBase(language)); |
| 103 | + doc.put("publications", version.getPublications(language)); |
| 104 | + doc.put("derivedFrom", version.getDerivedFrom(language)); |
| 105 | + doc.put("description", version.getIntroduction(language)); |
| 106 | + doc.put("section", version.getContactPerson().getSection()); |
| 107 | + doc.put("copyrighted", version.getOwnerClassification().isCopyrighted()); |
| 108 | + doc.put("published", version.isPublished(language)); |
| 109 | + |
| 110 | + List<String> codes = version.getAllClassificationItems().stream() |
| 111 | + .map(item -> formatClassificationItem(language, item)) |
| 112 | + .toList(); |
| 113 | + doc.put("codes", codes); |
| 114 | + |
| 115 | + indexVariants(version.getClassificationVariants(), language); |
| 116 | + indexCorrespondenceTables(version.getCorrespondenceTables(), language); |
| 117 | + |
| 118 | + updateElasticsearch(version, doc); |
| 119 | + } |
| 120 | + |
| 121 | + private void indexCorrespondenceTables(List<CorrespondenceTable> correspondenceTables, Language language) { |
| 122 | + for (CorrespondenceTable correspondenceTable : correspondenceTables) { |
| 123 | + Map<String, Object> doc = new HashMap<>(); |
| 124 | + doc.put("itemid", correspondenceTable.getId()); |
| 125 | + doc.put("uuid", language.getLanguageCode() + "_" + correspondenceTable.getUuid()); |
| 126 | + doc.put("language", language.getLanguageCode()); |
| 127 | + doc.put("type", "Correspondencetable"); |
| 128 | + doc.put("title", correspondenceTable.getName(language)); |
| 129 | + doc.put("description", correspondenceTable.getDescription(language)); |
| 130 | + updateElasticsearch(correspondenceTable, doc); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private void indexVariants(List<ClassificationVariant> variants, Language language) { |
| 135 | + for (ClassificationVariant variant : variants) { |
| 136 | + Map<String, Object> doc = new HashMap<>(); |
| 137 | + doc.put("itemid", variant.getId()); |
| 138 | + doc.put("uuid", language.getLanguageCode() + "_" + variant.getUuid()); |
| 139 | + doc.put("language", language.getLanguageCode()); |
| 140 | + doc.put("type", "Variant"); |
| 141 | + doc.put("title", variant.getFullName(language)); |
| 142 | + doc.put("copyrighted", variant.getOwnerClassification().isCopyrighted()); |
| 143 | + doc.put("published", variant.isPublished(language)); |
| 144 | + doc.put("description", variant.getIntroduction(language)); |
| 145 | + doc.put("section", variant.getContactPerson().getSection()); |
| 146 | + |
| 147 | + List<String> codes = variant.getAllClassificationItems().stream() |
| 148 | + .map(item -> formatClassificationItem(language, item)) |
| 149 | + .toList(); |
| 150 | + doc.put("codes", codes); |
| 151 | + |
| 152 | + updateElasticsearch(variant, doc); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private void updateElasticsearch(SoftDeletable entity, Map<String, Object> doc) { |
| 157 | + String uuid = (String) doc.get("uuid"); |
| 158 | + if (!entity.isDeleted()) { |
| 159 | + IndexQuery indexQuery = new IndexQueryBuilder() |
| 160 | + .withId(uuid) |
| 161 | + .withObject(doc) |
| 162 | + .build(); |
| 163 | + elasticsearchOperations.index(indexQuery, getIndexCoordinates()); |
| 164 | + } else { |
| 165 | + elasticsearchOperations.delete(uuid, getIndexCoordinates()); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private String formatClassificationItem(Language language, ClassificationItem item) { |
| 170 | + return item.getCode() + " - " + item.getOfficialName(language); |
| 171 | + } |
| 172 | +} |
0 commit comments