Skip to content

Commit

Permalink
Refactor DollySearchServiceConsumer and related classes to improve pa…
Browse files Browse the repository at this point in the history
…ckage structure and response handling #deploy-test-dolly-backend
  • Loading branch information
krharum committed Feb 20, 2025
1 parent 3480001 commit e42c3a5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package no.nav.dolly.consumer.dollysearchservice;
package no.nav.dolly.opensearch;

import lombok.extern.slf4j.Slf4j;
import no.nav.dolly.config.Consumers;
import no.nav.dolly.consumer.dollysearchservice.command.DollySearchServicePostCommand;
import no.nav.dolly.opensearch.command.DollySearchServicePostCommand;
import no.nav.testnav.libs.data.dollysearchservice.v1.SearchRequest;
import no.nav.testnav.libs.data.dollysearchservice.v1.SearchResponse;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.dolly.consumer.dollysearchservice.command;
package no.nav.dolly.opensearch.command;

import lombok.RequiredArgsConstructor;
import no.nav.testnav.libs.data.dollysearchservice.v1.SearchRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SearchResponse {
public static class RegistreResponseStatus {

private Long totalHitsBestillinger;
private Integer score;
private Float score;
private String took;
private Integer antall;
private Integer side;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import no.nav.dolly.consumer.dollysearchservice.DollySearchServiceConsumer;
import no.nav.dolly.elastic.ElasticTyper;
import no.nav.dolly.elastic.service.OpenSearchQueryBuilder;
import no.nav.dolly.mapper.MappingContextUtils;
import no.nav.dolly.opensearch.DollySearchServiceConsumer;
import no.nav.dolly.opensearch.dto.SearchRequest;
import no.nav.dolly.opensearch.dto.SearchResponse;
import org.opensearch.client.RequestOptions;
Expand All @@ -25,6 +24,7 @@
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Random;

Expand Down Expand Up @@ -52,12 +52,16 @@ public Mono<SearchResponse> search(List<ElasticTyper> registre, SearchRequest re

if (nonNull(registre)) {

response.setRegistreSearchResponse(execRegistreQuery(registre, request));
var registreResponse = execRegistreQuery(registre, request);
response.setRegistreSearchResponse(registreResponse);
personRequest.setIdenter(new HashSet<>(!registreResponse.getIdenter().isEmpty() ?
registreResponse.getIdenter() : List.of("99999999999")));
}

return dollySearchServiceConsumer.doPersonSearch(personRequest)
.map(personResultat -> {
response.setDollySearchResponse(personResultat);
response.getRegistreSearchResponse().setIdenter(null);
return response;
});
}
Expand All @@ -80,19 +84,13 @@ private SearchResponse.RegistreResponseStatus execRegistreQuery(List<ElasticType

try {
var registerResultat = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
var context = MappingContextUtils.getMappingContext();
context.setProperty("registre", registre);
context.setProperty("identer", getIdenter(registerResultat));
var registreResponse = getIdenter(registerResultat);
registreResponse.setRegistre(registre);
registreResponse.setSide(side);
registreResponse.setAntall(antall);
registreResponse.setSeed(seed);

var response = mapperFacade.map(registerResultat,
SearchResponse.RegistreResponseStatus.class,
context);

response.setSide(side);
response.setAntall(antall);
response.setSeed(seed);

return response;
return registreResponse;

} catch (IOException e) {
log.error("OpenSearch feil ved utføring av søk: {}", e.getMessage(), e);
Expand All @@ -102,18 +100,20 @@ private SearchResponse.RegistreResponseStatus execRegistreQuery(List<ElasticType
}
}

private static no.nav.dolly.elastic.dto.SearchResponse getIdenter(org.opensearch.action.search.SearchResponse response) {

return no.nav.dolly.elastic.dto.SearchResponse.builder()
.identer(Arrays.stream(response.getHits().getHits())
.map(SearchHit::getSourceAsMap)
.map(map -> (List<String>) map.get("identer"))
.flatMap(Collection::stream)
.distinct()
.limit(1000)
.toList())
.totalHits(getTotalHits(response.getHits()))
private static SearchResponse.RegistreResponseStatus getIdenter(org.opensearch.action.search.SearchResponse response) {

var identer = Arrays.stream(response.getHits().getHits())
.map(SearchHit::getSourceAsMap)
.map(map -> (List<String>) map.get("identer"))
.flatMap(Collection::stream)
.distinct()
.limit(1000)
.toList();
return SearchResponse.RegistreResponseStatus.builder()
.identer(identer)
.totalHitsBestillinger(getTotalHits(response.getHits()))
.took(response.getTook().getStringRep())
.antallIdenter(identer.size())
.score(response.getHits().getMaxScore())
.build();
}
Expand Down

0 comments on commit e42c3a5

Please sign in to comment.