-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
107 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 0 additions & 51 deletions
51
apps/dolly-backend/src/main/java/no/nav/dolly/elastic/service/QueryBuilder.java
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
apps/dolly-backend/src/main/java/no/nav/dolly/elastic/service/RandomSearchHelperService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package no.nav.dolly.elastic.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import no.nav.dolly.elastic.ElasticBestilling; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; | ||
import org.springframework.data.elasticsearch.core.SearchHit; | ||
import org.springframework.data.elasticsearch.core.SearchHits; | ||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; | ||
import org.springframework.data.elasticsearch.core.query.Criteria; | ||
import org.springframework.data.elasticsearch.core.query.CriteriaQueryBuilder; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.security.SecureRandom; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.stream.Collector; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class RandomSearchHelperService { | ||
|
||
private static final int PAGE_SIZE = 10; | ||
private static final int FACTOR = 2; | ||
|
||
private final ElasticsearchOperations elasticsearchOperations; | ||
private Random random = new SecureRandom(); | ||
|
||
public List<String> search(Criteria criteria) { | ||
|
||
var hits = search(criteria, 0); | ||
|
||
if (hits.getTotalHits() > PAGE_SIZE * FACTOR) { | ||
var noOfPages = (int) hits.getTotalHits() / (PAGE_SIZE * FACTOR); | ||
var currentPage = random.nextInt(noOfPages); | ||
log.info("Total hits={}, noOfPages={}, currentPage={}", hits.getTotalHits(), noOfPages, currentPage); | ||
hits = search(criteria, currentPage); | ||
} | ||
|
||
var identer = hits.getSearchHits().stream() | ||
.map(SearchHit::getContent) | ||
.map(ElasticBestilling::getIdenter) | ||
.flatMap(Collection::stream) | ||
.distinct() | ||
.collect(toShuffledList()); | ||
|
||
return identer.subList(0, Math.min(identer.size(), PAGE_SIZE)); | ||
} | ||
|
||
private static final Collector<?, ?, ?> SHUFFLER = Collectors.collectingAndThen( | ||
Collectors.toCollection(ArrayList::new), | ||
list -> { | ||
Collections.shuffle(list); | ||
return list; | ||
} | ||
); | ||
|
||
@SuppressWarnings("unchecked") | ||
private static <T> Collector<T, ?, List<T>> toShuffledList() { | ||
return (Collector<T, ?, List<T>>) SHUFFLER; | ||
} | ||
|
||
private SearchHits<ElasticBestilling> search(Criteria criteria, int pageNo) { | ||
|
||
return elasticsearchOperations.search(new CriteriaQueryBuilder(criteria) | ||
.withPageable(Pageable.ofSize(PAGE_SIZE * FACTOR).withPage(pageNo)) | ||
.build(), | ||
ElasticBestilling.class, IndexCoordinates.of("bestilling")); | ||
} | ||
} |