From a0a8029a4ea660cb49907b26aadc3e6a7c2d260f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Al=C3=AA=20Oliveira?= Date: Wed, 27 Nov 2024 12:14:09 -0300 Subject: [PATCH] - Code Smells and Cache Search improvements --- .../api/sn/search/TurSNSiteSearchAPI.java | 23 +++++++++++-------- .../sn/search/TurSNSiteSearchCachedAPI.java | 22 ++++-------------- .../TurSNRankingExpressionRepository.java | 1 - .../se/TurSEFilterQueryParameters.java | 1 - .../turing/commons/se/TurSEParameters.java | 3 ++- .../sn/bean/TurSNSiteSearchDocumentBean.java | 2 +- 6 files changed, 21 insertions(+), 31 deletions(-) diff --git a/turing-app/src/main/java/com/viglet/turing/api/sn/search/TurSNSiteSearchAPI.java b/turing-app/src/main/java/com/viglet/turing/api/sn/search/TurSNSiteSearchAPI.java index 897ae1da2e..28a6d24246 100644 --- a/turing-app/src/main/java/com/viglet/turing/api/sn/search/TurSNSiteSearchAPI.java +++ b/turing-app/src/main/java/com/viglet/turing/api/sn/search/TurSNSiteSearchAPI.java @@ -92,16 +92,18 @@ public ResponseEntity turSNSiteSearchSelectGet( HttpServletRequest request) { Locale locale = LocaleUtils.toLocale(localeRequest); if (existsByTurSNSiteAndLanguage(siteName, locale)) { + TurSEFilterQueryParameters turSEFilterQueryParameters = new TurSEFilterQueryParameters(filterQueriesDefault, + filterQueriesAnd, filterQueriesOr, fqOperator); + TurSEParameters turSEParameters = new TurSEParameters(q, turSEFilterQueryParameters, currentPage, sort, + rows, group, autoCorrectionDisabled); + URI uri = TurSNUtils.requestToURI(request); + TurSNSiteSearchContext turSNSiteSearchContext = new TurSNSiteSearchContext(siteName, turSEParameters, locale, + uri); if (searchCacheEnabled) { - return new ResponseEntity<>(turSNSiteSearchCachedAPI.searchCached(siteName, q, currentPage, - filterQueriesDefault, filterQueriesAnd, filterQueriesOr, fqOperator, sort, rows, group, - autoCorrectionDisabled, request, locale), HttpStatus.OK); - } - else { - return new ResponseEntity<>(turSNSearchProcess.search(new TurSNSiteSearchContext(siteName, - new TurSEParameters(q, new TurSEFilterQueryParameters(filterQueriesDefault, filterQueriesAnd, - filterQueriesOr, fqOperator), currentPage, sort, rows, group, autoCorrectionDisabled), locale, - TurSNUtils.requestToURI(request))), HttpStatus.OK); + String cacheKey = "%s_%s".formatted(siteName, request.getQueryString()); + return new ResponseEntity<>(turSNSiteSearchCachedAPI.searchCached(cacheKey, turSNSiteSearchContext), HttpStatus.OK); + } else { + return new ResponseEntity<>(turSNSearchProcess.search(turSNSiteSearchContext), HttpStatus.OK); } } else { return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); @@ -180,7 +182,8 @@ public ResponseEntity> turSNSiteSearchLatestImpersonate( return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); } - private String isLatestImpersonate(Optional turSNSearchLatestRequestBean, Principal principal) { + private String isLatestImpersonate(Optional turSNSearchLatestRequestBean, + Principal principal) { if (turSNSearchLatestRequestBean.isPresent() && turSNSearchLatestRequestBean.get().getUserId() != null) { return turSNSearchLatestRequestBean.get().getUserId(); } else { diff --git a/turing-app/src/main/java/com/viglet/turing/api/sn/search/TurSNSiteSearchCachedAPI.java b/turing-app/src/main/java/com/viglet/turing/api/sn/search/TurSNSiteSearchCachedAPI.java index 4e742f5438..32c679f66f 100644 --- a/turing-app/src/main/java/com/viglet/turing/api/sn/search/TurSNSiteSearchCachedAPI.java +++ b/turing-app/src/main/java/com/viglet/turing/api/sn/search/TurSNSiteSearchCachedAPI.java @@ -32,6 +32,7 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; +import java.net.URI; import java.util.List; import java.util.Locale; import java.util.concurrent.TimeUnit; @@ -50,22 +51,9 @@ public void cleanSearchCache() { log.info("Cleaning Search API"); } - @Cacheable(value = "searchAPI", key = "#request.queryString") - public TurSNSiteSearchBean searchCached(String siteName, String q, Integer currentPage, - List filterQueriesDefault, - List filterQueriesAnd, - List filterQueriesOr, - TurSNFilterQueryOperator fqOperator, - String sort, - Integer rows, - String group, - Integer autoCorrectionDisabled, - HttpServletRequest - request, - Locale locale) { - return turSNSearchProcess.search(new TurSNSiteSearchContext(siteName, - new TurSEParameters(q, new TurSEFilterQueryParameters(filterQueriesDefault, filterQueriesAnd, - filterQueriesOr, fqOperator), currentPage, sort, rows, group, autoCorrectionDisabled), locale, - TurSNUtils.requestToURI(request))); + @Cacheable(value = "searchAPI", key = "#cacheKey") + public TurSNSiteSearchBean searchCached(String cacheKey, + TurSNSiteSearchContext turSNSiteSearchContext) { + return turSNSearchProcess.search(turSNSiteSearchContext); } } diff --git a/turing-app/src/main/java/com/viglet/turing/persistence/repository/sn/ranking/TurSNRankingExpressionRepository.java b/turing-app/src/main/java/com/viglet/turing/persistence/repository/sn/ranking/TurSNRankingExpressionRepository.java index 3b0dd830e1..d789645cb6 100644 --- a/turing-app/src/main/java/com/viglet/turing/persistence/repository/sn/ranking/TurSNRankingExpressionRepository.java +++ b/turing-app/src/main/java/com/viglet/turing/persistence/repository/sn/ranking/TurSNRankingExpressionRepository.java @@ -21,7 +21,6 @@ package com.viglet.turing.persistence.repository.sn.ranking; import com.viglet.turing.persistence.model.sn.TurSNSite; -import com.viglet.turing.persistence.model.sn.ranking.TurSNRankingCondition; import com.viglet.turing.persistence.model.sn.ranking.TurSNRankingExpression; import org.jetbrains.annotations.NotNull; import org.springframework.cache.annotation.CacheEvict; diff --git a/turing-commons/src/main/java/com/viglet/turing/commons/se/TurSEFilterQueryParameters.java b/turing-commons/src/main/java/com/viglet/turing/commons/se/TurSEFilterQueryParameters.java index 293764d36c..3150f72c72 100644 --- a/turing-commons/src/main/java/com/viglet/turing/commons/se/TurSEFilterQueryParameters.java +++ b/turing-commons/src/main/java/com/viglet/turing/commons/se/TurSEFilterQueryParameters.java @@ -5,7 +5,6 @@ import lombok.Setter; import java.io.Serializable; -import java.util.ArrayList; import java.util.Collections; import java.util.List; diff --git a/turing-commons/src/main/java/com/viglet/turing/commons/se/TurSEParameters.java b/turing-commons/src/main/java/com/viglet/turing/commons/se/TurSEParameters.java index df764c1279..7906a981cf 100644 --- a/turing-commons/src/main/java/com/viglet/turing/commons/se/TurSEParameters.java +++ b/turing-commons/src/main/java/com/viglet/turing/commons/se/TurSEParameters.java @@ -33,7 +33,8 @@ public TurSEParameters(String query, TurSEFilterQueryParameters filterQueries, } public TurSEParameters(String query, TurSEFilterQueryParameters filterQueries, Integer currentPage, String sort, Integer rows, - String group, Integer autoCorrectionDisabled, TurSNSitePostParamsBean turSNSitePostParamsBean) { + String group, Integer autoCorrectionDisabled, + TurSNSitePostParamsBean turSNSitePostParamsBean) { super(); this.query = query; this.filterQueries = filterQueries; diff --git a/turing-commons/src/main/java/com/viglet/turing/commons/sn/bean/TurSNSiteSearchDocumentBean.java b/turing-commons/src/main/java/com/viglet/turing/commons/sn/bean/TurSNSiteSearchDocumentBean.java index cb4335e04d..4879a7062c 100644 --- a/turing-commons/src/main/java/com/viglet/turing/commons/sn/bean/TurSNSiteSearchDocumentBean.java +++ b/turing-commons/src/main/java/com/viglet/turing/commons/sn/bean/TurSNSiteSearchDocumentBean.java @@ -40,6 +40,6 @@ public class TurSNSiteSearchDocumentBean implements Serializable { private String source; private boolean elevate; private List metadata; - private Map fields; + private transient Map fields; }