Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifisering av elastic-søk #289

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,17 @@ private val logger = LoggerFactory.getLogger(ElasticUpdater::class.java)
@Service
class ElasticUpdater(
private val conceptRepository: MongoTemplate,
private val conceptSearchRepository: ConceptSearchRepository,
private val currentConceptRepository: CurrentConceptRepository
) {

fun reindexElastic() = runBlocking {
launch {
try {
conceptSearchRepository.deleteAll()
currentConceptRepository.deleteAll()
} catch (_: Exception) { }

conceptRepository.findAll<BegrepDBO>()
.forEach {
conceptSearchRepository.save(it)
if (it.shouldBeCurrent(currentConceptRepository.findByIdOrNull(it.originaltBegrep))) currentConceptRepository.save(CurrentConcept(it))
}

Expand Down
12 changes: 0 additions & 12 deletions src/main/kotlin/no/fdk/concept_catalog/model/Begrep.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ package no.fdk.concept_catalog.model
import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonInclude
import org.springframework.data.elasticsearch.annotations.Field
import org.springframework.data.elasticsearch.annotations.FieldType
import org.springframework.data.elasticsearch.annotations.Mapping
import org.springframework.data.elasticsearch.annotations.Setting
import org.springframework.data.mongodb.core.index.CompoundIndex
import org.springframework.data.mongodb.core.index.CompoundIndexes
import org.springframework.data.mongodb.core.index.Indexed
import org.springframework.data.mongodb.core.mapping.Document
import java.time.Instant
import java.time.LocalDate
Expand All @@ -21,9 +16,6 @@ import java.time.LocalDate
CompoundIndex(name = "originalt_begrep", def = "{'originaltBegrep' : 1}"),
CompoundIndex(name = "originalt_begrep_er_publisert", def = "{'originaltBegrep' : 1, 'erPublisert': 1}")
])
@org.springframework.data.elasticsearch.annotations.Document(indexName = "concepts")
@Setting(settingPath = "/elastic/settings.json")
@Mapping(mappingPath = "/elastic/mappings.json")
@JsonInclude(JsonInclude.Include.NON_NULL)
data class BegrepDBO (
val id: String,
Expand All @@ -34,7 +26,6 @@ data class BegrepDBO (
val statusURI: String? = null,
val erPublisert: Boolean = false,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", timezone = "Europe/Oslo")
@Field(type = FieldType.Date)
val publiseringsTidspunkt: Instant? = null,
val anbefaltTerm: Term?,
val tillattTerm: Map<String, List<String>>?,
Expand All @@ -50,12 +41,9 @@ data class BegrepDBO (
val fagområdeKoder: List<String>?,
val omfang: URITekst?,
val kontaktpunkt: Kontaktpunkt?,
@Field(type = FieldType.Date)
val gyldigFom: LocalDate?,
@Field(type = FieldType.Date)
val gyldigTom: LocalDate?,
val endringslogelement: Endringslogelement?,
@Field(type = FieldType.Date)
val opprettet: Instant? = null,
val opprettetAv: String? = null,
val seOgså: List<String>?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package no.fdk.concept_catalog.model
import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.annotation.JsonInclude
import org.springframework.data.annotation.Id
import org.springframework.data.elasticsearch.annotations.*
import org.springframework.data.elasticsearch.annotations.Document
import org.springframework.data.elasticsearch.annotations.Field
import org.springframework.data.elasticsearch.annotations.FieldType
import java.time.Instant
import java.time.LocalDate

@Document(indexName = "concepts-current")
@Setting(settingPath = "/elastic/settings.json")
@Mapping(mappingPath = "/elastic/mappings.json")
@JsonInclude(JsonInclude.Include.NON_NULL)
data class CurrentConcept(
val idOfThisVersion: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class SearchFilters(
val assignedUser: SearchFilter<List<String>>? = null,
val status: SearchFilter<List<String>>? = null,
val published: BooleanFilter? = null,
val onlyCurrentVersions: Boolean = true,
val subject: SearchFilter<List<String>>? = null,
val originalId: SearchFilter<List<String>>? = null,
val internalFields: SearchFilter<Map<String, List<String>>>? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package no.fdk.concept_catalog.service

import no.fdk.concept_catalog.elastic.ConceptSearchRepository
import no.fdk.concept_catalog.elastic.CurrentConceptRepository
import no.fdk.concept_catalog.model.*
import java.util.UUID
Expand All @@ -21,7 +20,6 @@ private val logger = LoggerFactory.getLogger(ChangeRequestService::class.java)
class ChangeRequestService(
private val changeRequestRepository: ChangeRequestRepository,
private val conceptRepository: ConceptRepository,
private val conceptSearchRepository: ConceptSearchRepository,
private val currentConceptRepository: CurrentConceptRepository,
private val conceptService: ConceptService
) {
Expand Down Expand Up @@ -82,12 +80,10 @@ class ChangeRequestService(
val conceptToUpdate = when {
dbConcept == null -> createNewConcept(Virksomhet(id=catalogId), user)
.updateLastChangedAndByWhom(user)
.also { conceptSearchRepository.save(it) }
.also { currentConceptRepository.save(CurrentConcept(it)) }
.let { conceptRepository.save(it) }
dbConcept.erPublisert -> dbConcept.createNewRevision(user)
.updateLastChangedAndByWhom(user)
.also { conceptSearchRepository.save(it) }
.let { conceptRepository.save(it) }
else -> dbConcept
}
Expand All @@ -98,7 +94,6 @@ class ChangeRequestService(
logger.error("update of concept failed when accepting ${changeRequest.id}, reverting acceptation", ex)
changeRequest.copy(status = ChangeRequestStatus.OPEN).run { changeRequestRepository.save(this) }
if (conceptToUpdate.id != dbConcept?.id) {
conceptSearchRepository.delete(conceptToUpdate)
if (conceptToUpdate.id == conceptToUpdate.originaltBegrep) {
currentConceptRepository.delete(CurrentConcept(conceptToUpdate))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ class ConceptSearchService(
private val elasticsearchOperations: ElasticsearchOperations
) {

fun searchConcepts(orgNumber: String, search: SearchOperation): SearchHits<BegrepDBO> =
elasticsearchOperations.search(
search.toElasticQuery(orgNumber),
BegrepDBO::class.java,
IndexCoordinates.of("concepts")
)

fun searchCurrentConcepts(orgNumber: String, search: SearchOperation): SearchHits<CurrentConcept> =
elasticsearchOperations.search(
search.toElasticQuery(orgNumber),
Expand Down Expand Up @@ -76,12 +69,7 @@ class ConceptSearchService(

private fun QueryFields.paths(): List<String> =
listOf(
// Boosting hits in anbefaltTerm
if (anbefaltTerm) listOf(
"anbefaltTerm.navn.nb^10",
"anbefaltTerm.navn.en^5",
"anbefaltTerm.navn.nn^5"
)
if (anbefaltTerm) languagePaths("anbefaltTerm.navn")
else emptyList(),

if (frarådetTerm) languagePaths("frarådetTerm")
Expand All @@ -98,6 +86,6 @@ class ConceptSearchService(
).flatten()

private fun languagePaths(basePath: String): List<String> =
listOf("$basePath.nb", "$basePath.en", "$basePath.nn")
listOf("$basePath.nb")

}
31 changes: 9 additions & 22 deletions src/main/kotlin/no/fdk/concept_catalog/service/ConceptService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package no.fdk.concept_catalog.service

import com.fasterxml.jackson.databind.ObjectMapper
import no.fdk.concept_catalog.configuration.ApplicationProperties
import no.fdk.concept_catalog.elastic.ConceptSearchRepository
import no.fdk.concept_catalog.elastic.CurrentConceptRepository
import no.fdk.concept_catalog.model.*
import no.fdk.concept_catalog.repository.ConceptRepository
Expand All @@ -27,7 +26,6 @@ private val logger = LoggerFactory.getLogger(ConceptService::class.java)
class ConceptService(
private val conceptRepository: ConceptRepository,
private val conceptSearchService: ConceptSearchService,
private val conceptSearchRepository: ConceptSearchRepository,
private val currentConceptRepository: CurrentConceptRepository,
private val mongoOperations: MongoOperations,
private val applicationProperties: ApplicationProperties,
Expand All @@ -37,7 +35,6 @@ class ConceptService(
) {

fun deleteConcept(concept: BegrepDBO) {
conceptSearchRepository.delete(concept)
if (concept.id == concept.originaltBegrep) {
currentConceptRepository.delete(CurrentConcept(concept))
}
Expand Down Expand Up @@ -168,7 +165,6 @@ class ConceptService(
): List<Begrep> {
val locations = conceptsAndOperations.map { historyService.updateHistory(it.key, it.value, user, jwt) }
try {
conceptSearchRepository.saveAll(conceptsAndOperations.keys)
conceptsAndOperations.keys
.filter { it.id == it.originaltBegrep }
.map { CurrentConcept(it) }
Expand Down Expand Up @@ -219,25 +215,17 @@ class ConceptService(
.distinctBy {concept -> concept.originaltBegrep }
.map { it.toDTO(it.versjonsnr, it.id, findIdOfUnpublishedRevision(it)) }

fun searchConcepts(orgNumber: String, search: SearchOperation): Paginated =
if (search.filters.onlyCurrentVersions) {
val hits = conceptSearchService.searchCurrentConcepts(orgNumber, search)
fun searchConcepts(orgNumber: String, search: SearchOperation): Paginated {
val hits = conceptSearchService.searchCurrentConcepts(orgNumber, search)

hits.map { it.content }
.map { it.toDBO() }
.map { it.withHighestVersionDTO() }
.toList()
.paginate(hits.totalHits, search.pagination)
} else {
val hits = conceptSearchService.searchConcepts(orgNumber, search)

hits.map { it.content }
.map { it.withHighestVersionDTO() }
.toList()
.paginate(hits.totalHits, search.pagination)
}
return hits.map { it.content }
.map { it.toDBO() }
.map { it.withHighestVersionDTO() }
.toList()
.asPaginatedWrapDTO(hits.totalHits, search.pagination)
}

private fun List<Begrep>.paginate(totalHits: Long, pagination: Pagination): Paginated {
private fun List<Begrep>.asPaginatedWrapDTO(totalHits: Long, pagination: Pagination): Paginated {
return Paginated(
hits = this,
page = PageMeta(
Expand Down Expand Up @@ -297,7 +285,6 @@ class ConceptService(

conceptPublisher.send(concept.ansvarligVirksomhet.id)

conceptSearchRepository.save(published)
currentConceptRepository.save(CurrentConcept(published))
return conceptRepository.save(published)
.withHighestVersionDTO()
Expand Down
45 changes: 0 additions & 45 deletions src/main/resources/elastic/mappings.json

This file was deleted.

24 changes: 0 additions & 24 deletions src/main/resources/elastic/settings.json

This file was deleted.

31 changes: 4 additions & 27 deletions src/test/kotlin/no/fdk/concept_catalog/contract/SearchConcepts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,14 @@ class SearchConcepts : ApiTestContext() {
fun `Query returns correct results when searching in merknad`() {
val rsp = authorizedRequest(
"/begreper/search?orgNummer=123456789",
port, mapper.writeValueAsString(SearchOperation("merknad")), JwtToken(Access.ORG_WRITE).toString(),
port, mapper.writeValueAsString(SearchOperation("asdf")), JwtToken(Access.ORG_WRITE).toString(),
HttpMethod.POST
)

assertEquals(HttpStatus.OK.value(), rsp["status"])

val result: Paginated = mapper.readValue(rsp["body"] as String)
assertEquals(listOf(BEGREP_0), result.hits)
assertEquals(listOf(BEGREP_1), result.hits)

}

Expand Down Expand Up @@ -478,27 +478,6 @@ class SearchConcepts : ApiTestContext() {
assertEquals(listOf(BEGREP_0), result.hits)
}

@Test
fun `Query with false value for current version filter returns correct results`() {
val rsp = authorizedRequest(
"/begreper/search?orgNummer=123456789",
port,
mapper.writeValueAsString(
SearchOperation(
"definisjon",
filters = SearchFilters(onlyCurrentVersions = false)
)
),
JwtToken(Access.ORG_WRITE).toString(),
HttpMethod.POST
)

assertEquals(HttpStatus.OK.value(), rsp["status"])

val result: Paginated = mapper.readValue(rsp["body"] as String)
assertEquals(listOf(BEGREP_0, BEGREP_0_OLD), result.hits)
}

@Test
fun `Query returns no results`() {
val rsp = authorizedRequest(
Expand Down Expand Up @@ -578,7 +557,6 @@ class SearchConcepts : ApiTestContext() {
fun `Query returns sorted results ordered by sistEndret ascending`() {
val searchOp = SearchOperation(
query = "",
filters = SearchFilters(onlyCurrentVersions = false),
sort = SortField(field = SortFieldEnum.SIST_ENDRET, direction = SortDirection.ASC)
)
val rsp = authorizedRequest(
Expand All @@ -590,14 +568,13 @@ class SearchConcepts : ApiTestContext() {
assertEquals(HttpStatus.OK.value(), rsp["status"])

val result: Paginated = mapper.readValue(rsp["body"] as String)
assertEquals(listOf(BEGREP_0_OLD, BEGREP_2, BEGREP_0, BEGREP_1), result.hits)
assertEquals(listOf(BEGREP_2, BEGREP_0, BEGREP_1), result.hits)
}

@Test
fun `Query returns sorted results ordered by anbefaltTerm descending`() {
val searchOp = SearchOperation(
query = "",
filters = SearchFilters(onlyCurrentVersions = false),
sort = SortField(field = SortFieldEnum.ANBEFALT_TERM_NB, direction = SortDirection.DESC)
)
val rsp = authorizedRequest(
Expand All @@ -608,7 +585,7 @@ class SearchConcepts : ApiTestContext() {
assertEquals(HttpStatus.OK.value(), rsp["status"])

val result: Paginated = mapper.readValue(rsp["body"] as String)
assertEquals(listOf(BEGREP_0_OLD, BEGREP_0, BEGREP_2, BEGREP_1), result.hits)
assertEquals(listOf(BEGREP_0, BEGREP_2, BEGREP_1), result.hits)
}

@Test
Expand Down
Loading
Loading