diff --git a/apps/oppsummeringsdokument-service/build.gradle b/apps/oppsummeringsdokument-service/build.gradle index b2084bedec4..10e2d66412f 100644 --- a/apps/oppsummeringsdokument-service/build.gradle +++ b/apps/oppsummeringsdokument-service/build.gradle @@ -80,7 +80,6 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-webflux' implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' - implementation 'org.opensearch.client:spring-data-opensearch:1.0.1' implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch' implementation 'org.springframework.cloud:spring-cloud-starter-vault-config' diff --git a/apps/oppsummeringsdokument-service/src/main/java/no/nav/testnav/apps/oppsummeringsdokumentservice/adapter/OppsummeringsdokumentAdapter.java b/apps/oppsummeringsdokument-service/src/main/java/no/nav/testnav/apps/oppsummeringsdokumentservice/adapter/OppsummeringsdokumentAdapter.java index 936ab2dbad3..37e50555926 100644 --- a/apps/oppsummeringsdokument-service/src/main/java/no/nav/testnav/apps/oppsummeringsdokumentservice/adapter/OppsummeringsdokumentAdapter.java +++ b/apps/oppsummeringsdokument-service/src/main/java/no/nav/testnav/apps/oppsummeringsdokumentservice/adapter/OppsummeringsdokumentAdapter.java @@ -9,29 +9,26 @@ import no.nav.testnav.apps.oppsummeringsdokumentservice.repository.OppsummeringsdokumentRepository; import no.nav.testnav.apps.oppsummeringsdokumentservice.repository.model.OppsummeringsdokumentModel; import no.nav.testnav.libs.dto.oppsummeringsdokumentservice.v2.Populasjon; -import org.opensearch.client.RestHighLevelClient; -import org.opensearch.data.client.orhlc.NativeSearchQueryBuilder; -import org.opensearch.index.query.QueryBuilder; -import org.opensearch.index.query.QueryBuilders; -import org.opensearch.index.query.RangeQueryBuilder; -import org.opensearch.search.sort.SortBuilders; -import org.opensearch.search.sort.SortOrder; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.UncategorizedElasticsearchException; +import org.springframework.data.elasticsearch.client.elc.NativeQueryBuilder; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.SearchHit; +import org.springframework.data.elasticsearch.core.query.Criteria; +import org.springframework.data.elasticsearch.core.query.CriteriaQuery; import org.springframework.stereotype.Component; import java.time.LocalDate; -import java.util.ArrayList; import java.util.List; import java.util.Objects; -import java.util.Optional; import java.util.stream.Collectors; +import static java.util.Objects.isNull; + @Slf4j @Component @RequiredArgsConstructor @@ -41,7 +38,6 @@ public class OppsummeringsdokumentAdapter { private final ObjectMapper objectMapper; private final OppsummeringsdokumentRepository repository; private final ElasticsearchOperations operations; - private final RestHighLevelClient client; private final AaregSyntConsumer aaregSyntConsumer; public void deleteAllBy(String miljo, Populasjon populasjon) { @@ -74,30 +70,28 @@ public Oppsummeringsdokument get(String id) { } public List getAllCurrentDocumentsBy(String miljo) { - return getAllCurrentDocumentsBy(new NativeSearchQueryBuilder() + return getAllCurrentDocumentsBy(new NativeQueryBuilder() .withQuery( - QueryBuilders.matchQuery(MILJO, miljo) - ) - ); + new CriteriaQuery(new Criteria(MILJO).is(miljo) + ) + )); } public Page getAllCurrentDocumentsBy(String miljo, Integer page) { var pageable = PageRequest.of(page, 1); - return getAllCurrentDocumentsBy(new NativeSearchQueryBuilder() + return getAllCurrentDocumentsBy(new NativeQueryBuilder() .withQuery( - QueryBuilders.matchQuery(MILJO, miljo) + new CriteriaQuery(new Criteria(MILJO).is(miljo) + ) ).withPageable(pageable), pageable ); } public List getAllCurrentDocumentsBy(String miljo, String ident) { - var queryBuilders = new ArrayList(); - queryBuilders.add(QueryBuilders.matchQuery(MILJO, miljo)); - - queryBuilders.add(QueryBuilders.matchQuery("virksomheter.personer.ident", ident)); - - return getAllCurrentDocumentsBy(new NativeSearchQueryBuilder().withQuery(combinedOnANDOperator(queryBuilders))); + return getAllCurrentDocumentsBy(new NativeQueryBuilder() + .withQuery(new CriteriaQuery(new Criteria(MILJO).is(miljo).and("virksomheter.personer.ident").is(ident))) + ); } public Page getAllCurrentDocumentsBy( @@ -109,19 +103,16 @@ public Page getAllCurrentDocumentsBy( Integer page ) { var pageable = PageRequest.of(page, 1); - var queryBuilders = new ArrayList(); - - queryBuilders.add(QueryBuilders.matchQuery(MILJO, miljo)); - getKalendermaanedBetween(fom, tom).ifPresent(queryBuilders::add); - Optional.ofNullable(ident).ifPresent(value -> queryBuilders.add( - QueryBuilders.matchQuery("virksomheter.personer.ident", value) - )); - Optional.ofNullable(typeArbeidsforhold).ifPresent(value -> queryBuilders.add( - QueryBuilders.matchQuery("virksomheter.personer.arbeidsforhold.typeArbeidsforhold", value) - )); + + var criteria = new Criteria(MILJO).is(miljo); + criteria = isNull(fom) ? criteria : criteria.and("kalendermaaned").greaterThanEqual(fom.withDayOfMonth(1)); + criteria = isNull(tom) ? criteria : criteria.and("kalendermaaned").lessThanEqual(tom.withDayOfMonth(tom.lengthOfMonth())); + criteria = isNull(ident) ? criteria : criteria.and("virksomheter.personer.ident").is(ident); + criteria = isNull(typeArbeidsforhold) ? criteria : criteria.and("virksomheter.personer.arbeidsforhold.typeArbeidsforhold").is(typeArbeidsforhold); + return getAllCurrentDocumentsBy( - new NativeSearchQueryBuilder() - .withQuery(combinedOnANDOperator(queryBuilders)) + new NativeQueryBuilder() + .withQuery(new CriteriaQuery(criteria)) .withPageable(pageable), pageable ); @@ -145,9 +136,9 @@ public Oppsummeringsdokument getCurrentDocumentBy(LocalDate kalendermaaned, Stri return list.stream().findFirst().orElse(null); } - private Page getAllCurrentDocumentsBy(NativeSearchQueryBuilder builder, Pageable pageable) { + private Page getAllCurrentDocumentsBy(NativeQueryBuilder builder, Pageable pageable) { -// builder.withSorts(SortBuilders.fieldSort("lastModified").order(SortOrder.ASC)); + builder.withSort(Sort.by("lastModified").ascending()); var searchHist = operations.search( builder.build(), OppsummeringsdokumentModel.class @@ -162,8 +153,8 @@ private Page getAllCurrentDocumentsBy(NativeSearchQueryBu ); } - private List getAllCurrentDocumentsBy(NativeSearchQueryBuilder builder) { - builder.withSorts(SortBuilders.fieldSort("lastModified").order(SortOrder.ASC)); + private List getAllCurrentDocumentsBy(NativeQueryBuilder builder) { + builder.withSort(Sort.by("lastModified").ascending()); var list = operations.search( builder.build(), OppsummeringsdokumentModel.class @@ -173,42 +164,16 @@ private List getAllCurrentDocumentsBy(NativeSearchQueryBu } private List getAllCurrentDocumentsBy(String miljo, String orgnummer, LocalDate fom, LocalDate tom) { - var queryBuilders = new ArrayList(); - queryBuilders.add(QueryBuilders.matchQuery(MILJO, miljo)); - queryBuilders.add(QueryBuilders.matchQuery("opplysningspliktigOrganisajonsnummer", orgnummer)); + var criteria = new Criteria(MILJO).is(miljo).and("opplysningspliktigOrganisajonsnummer").is(orgnummer); + criteria = isNull(fom) ? criteria : criteria.and("kalendermaaned").greaterThanEqual(fom.withDayOfMonth(1)); + criteria = isNull(tom) ? criteria : criteria.and("kalendermaaned").lessThanEqual(tom.withDayOfMonth(tom.lengthOfMonth())); - getKalendermaanedBetween(fom, tom).ifPresent(queryBuilders::add); - - return getAllCurrentDocumentsBy(new NativeSearchQueryBuilder() - .withQuery(combinedOnANDOperator(queryBuilders)) + return getAllCurrentDocumentsBy(new NativeQueryBuilder() + .withQuery(new CriteriaQuery(criteria)) ); } - private Optional getKalendermaanedBetween(LocalDate fom, LocalDate tom) { - if (fom == null && tom == null) { - return Optional.empty(); - } - var builder = QueryBuilders.rangeQuery("kalendermaaned"); - - if (fom != null) { - builder.gte(fom.withDayOfMonth(1)); - } - - if (tom != null) { - builder.lte(tom.withDayOfMonth(tom.lengthOfMonth())); - } - return Optional.of(builder); - } - - private QueryBuilder combinedOnANDOperator(List list) { - var queryBuilder = QueryBuilders.boolQuery(); - for (var item : list) { - queryBuilder.must(item); - } - return queryBuilder; - } - /** * TODO Find a way to do this operation by elastic search */ diff --git a/apps/oppsummeringsdokument-service/src/main/java/no/nav/testnav/apps/oppsummeringsdokumentservice/config/OpensearchConfig.java b/apps/oppsummeringsdokument-service/src/main/java/no/nav/testnav/apps/oppsummeringsdokumentservice/config/OpensearchConfig.java index 877dffb3077..f770e465fcf 100644 --- a/apps/oppsummeringsdokument-service/src/main/java/no/nav/testnav/apps/oppsummeringsdokumentservice/config/OpensearchConfig.java +++ b/apps/oppsummeringsdokument-service/src/main/java/no/nav/testnav/apps/oppsummeringsdokumentservice/config/OpensearchConfig.java @@ -2,31 +2,25 @@ import lombok.RequiredArgsConstructor; import no.nav.testnav.apps.oppsummeringsdokumentservice.config.credentials.ElasticSearchCredentials; -import org.opensearch.client.RestHighLevelClient; -import org.opensearch.data.client.orhlc.AbstractOpenSearchConfiguration; -import org.opensearch.data.client.orhlc.ClientConfiguration; -import org.opensearch.data.client.orhlc.RestClients; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.data.elasticsearch.client.ClientConfiguration; +import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration; import java.time.Duration; @Configuration @RequiredArgsConstructor -public class OpensearchConfig extends AbstractOpenSearchConfiguration { +public class OpensearchConfig extends ElasticsearchConfiguration { private final ElasticSearchCredentials elasticSearchCredentials; @Override - @Bean - public RestHighLevelClient opensearchClient() { - ClientConfiguration clientConfiguration = ClientConfiguration.builder() + public ClientConfiguration clientConfiguration() { + return ClientConfiguration.builder() .connectedTo(elasticSearchCredentials.getEndpoints().replace("https://", "")) .usingSsl() .withBasicAuth(elasticSearchCredentials.getUsername(), elasticSearchCredentials.getPassword()) .withConnectTimeout(Duration.ofSeconds(10)) .withSocketTimeout(Duration.ofSeconds(5)) .build(); - - return RestClients.create(clientConfiguration).rest(); } }