dev add pagination for search in internet images
Test Workflow / test (push) Successful in 4s

This commit is contained in:
vladp
2026-02-10 11:58:32 +07:00
parent 33883faa98
commit 792e0048cc
4 changed files with 29 additions and 5 deletions
@@ -7,4 +7,10 @@ import lombok.Data;
public class ImageSearchRequest {
@JsonProperty("file_id")
private String fileId;
@JsonProperty("page")
private Integer page = 1;
@JsonProperty("page_size")
private Integer pageSize = 10;
}
@@ -42,4 +42,12 @@ public class YandexSearchResponse {
@JsonProperty("host")
private String host;
}
private int page;
private int pageSize;
private int totalResults;
private int totalPages;
}
@@ -59,12 +59,15 @@ public class ImageFoundRequestHandler implements RequestHandler {
// GoogleVisionSearchResponse googleVisionSearchResponse = googleVisionSearchService.searchByFileEntity(fileId);
String yandexReverseSearchResponse = yandexSearchService.searchReverseByPublicUrl(fileEntity);
int page = imageSearchRequest.getPage() != null ? imageSearchRequest.getPage() : 1;
int pageSize = imageSearchRequest.getPageSize() != null ? imageSearchRequest.getPageSize() : 10;
tariffInfoService.writeOffTokens(fileEntity.getUserId(), TariffConstants.TOKEN_VALUE_FOR_SEARCH);
checkCounterService.incrementCheckCount(fileEntity.getUserId(), fileEntity.getMimeType());
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
MessageCode.SUCCESS.getDescription(),mapper.mapToYandexResponse(yandexReverseSearchResponse));
MessageCode.SUCCESS.getDescription(),mapper.mapToYandexResponse(yandexReverseSearchResponse, page,
pageSize));
}
}
@@ -9,7 +9,7 @@ import java.util.List;
public class SearchApiToYandexResponseMapper {
private final ObjectMapper objectMapper = new ObjectMapper();
public YandexSearchResponse mapToYandexResponse(String searchApiJson) throws Exception {
public YandexSearchResponse mapToYandexResponse(String searchApiJson, int page, int pageSize) throws Exception {
JsonNode root = objectMapper.readTree(searchApiJson);
YandexSearchResponse response = new YandexSearchResponse();
@@ -18,9 +18,12 @@ public class SearchApiToYandexResponseMapper {
JsonNode visualMatches = root.path("visual_matches");
if (visualMatches.isArray()) {
for (JsonNode match : visualMatches) {
YandexSearchResponse.ImageResult result = new YandexSearchResponse.ImageResult();
int startIndex = (page - 1) * pageSize;
int endIndex = Math.min(startIndex + pageSize, visualMatches.size());
for (int i = startIndex; i < endIndex; i++) {
JsonNode match = visualMatches.get(i);
YandexSearchResponse.ImageResult result = new YandexSearchResponse.ImageResult();
JsonNode imageNode = match.path("image");
if (!imageNode.isMissingNode()) {
@@ -30,7 +33,6 @@ public class SearchApiToYandexResponseMapper {
}
result.setPageUrl(match.path("link").asText());
result.setPageTitle(match.path("title").asText());
String source = match.path("source").asText();
@@ -40,6 +42,11 @@ public class SearchApiToYandexResponseMapper {
imageResults.add(result);
}
}
response.setPage(page);
response.setPageSize(pageSize);
response.setTotalResults(visualMatches.size());
response.setTotalPages((int) Math.ceil((double) visualMatches.size() / pageSize));
}
response.setImages(imageResults);