From 5c6e9355490f5f9acc662fea1f3570372847963b Mon Sep 17 00:00:00 2001 From: Philipp Gritsch <philipp.gritsch@uibk.ac.at> Date: Wed, 13 Dec 2023 14:37:35 +0000 Subject: [PATCH] Revert "#454 also added scriptscorequery + normalize metadata factors + add intrinsic score" This reverts commit 67d0ff0e76ec577121f121e6d2b3f0e365511ef7 --- pom.xml | 2 +- .../repository/search/MetaDataRepository.java | 56 ++++++++++--------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/pom.xml b/pom.xml index 79b18f8c6..8c8e43180 100644 --- a/pom.xml +++ b/pom.xml @@ -158,7 +158,7 @@ <dependency> <groupId>org.codeability.sharing</groupId> <artifactId>SharingPluginPlatformAPI</artifactId> - <version>0.4.10</version> + <version>0.4.10-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springdoc</groupId> diff --git a/src/main/java/at/ac/uibk/gitsearch/repository/search/MetaDataRepository.java b/src/main/java/at/ac/uibk/gitsearch/repository/search/MetaDataRepository.java index 21f30e4d4..91f827246 100644 --- a/src/main/java/at/ac/uibk/gitsearch/repository/search/MetaDataRepository.java +++ b/src/main/java/at/ac/uibk/gitsearch/repository/search/MetaDataRepository.java @@ -7,6 +7,10 @@ import at.ac.uibk.gitsearch.repository.jpa.StatisticsRepository; import at.ac.uibk.gitsearch.service.dto.AutoCompleteEntry; import at.ac.uibk.gitsearch.service.dto.StatisticsDTO; import co.elastic.clients.elasticsearch.ElasticsearchClient; +import co.elastic.clients.elasticsearch._types.ScriptSortType; +import co.elastic.clients.elasticsearch._types.SortOptions; +import co.elastic.clients.elasticsearch._types.SortOptionsBuilders; +import co.elastic.clients.elasticsearch._types.SortOrder; import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery; import co.elastic.clients.elasticsearch._types.query_dsl.ExistsQuery; import co.elastic.clients.elasticsearch._types.query_dsl.MatchPhrasePrefixQuery; @@ -14,7 +18,6 @@ import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery; import co.elastic.clients.elasticsearch._types.query_dsl.MultiMatchQuery; import co.elastic.clients.elasticsearch._types.query_dsl.PrefixQuery; import co.elastic.clients.elasticsearch._types.query_dsl.RangeQuery; -import co.elastic.clients.elasticsearch._types.query_dsl.ScriptScoreQuery; import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery; import co.elastic.clients.elasticsearch._types.query_dsl.TextQueryType; import co.elastic.clients.elasticsearch.core.search.Hit; @@ -53,7 +56,6 @@ import java.util.function.Consumer; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; -import java.util.stream.DoubleStream; import java.util.stream.Stream; import java.util.stream.StreamSupport; import javax.annotation.PostConstruct; @@ -558,17 +560,20 @@ public class MetaDataRepository { @SuppressWarnings({ "PMD.ExcessiveMethodLength", "PMD.NcssCount" }) public SearchResultsDTO pageDetailsWithJavaApi(SearchInputDTO searchInputDTO, Optional<User> user, boolean ignoreUserAuthorization) throws IOException { + BoolQuery.Builder queryBuilder = createQueryBuilder(searchInputDTO, user, ignoreUserAuthorization); + final int pageSize = searchInputDTO.getPageSize(); final int from = (searchInputDTO.getPage()) * pageSize; try { - final BoolQuery.Builder boolQueryBuilder = createBoolQueryBuilder(searchInputDTO, user, ignoreUserAuthorization); - final ScriptScoreQuery.Builder scoreQueryBuilder = createScoreQueryBuilder(searchInputDTO, boolQueryBuilder.build()); + final BoolQuery query = queryBuilder.build(); + // LOGGER.info("ElasticSearch Query using Java Client API:\n{}", query) co.elastic.clients.elasticsearch.core.SearchResponse<SearchResultDTO> searchResponse = elasticsearchAPIClient.search( search -> search .index(SearchRepositoryConstants.INDEX_METADATA) - .query(q -> q.scriptScore(scoreQueryBuilder.build())) + .query(q -> q.bool(query)) + .sort(getSortStrategy(searchInputDTO.getOrdering())) .from(from) .size(pageSize), SearchResultDTO.class @@ -588,8 +593,7 @@ public class MetaDataRepository { } } - private ScriptScoreQuery.Builder createScoreQueryBuilder(SearchInputDTO searchInputDTO, final BoolQuery query) { - final SearchOrdering searchOrdering = searchInputDTO.getOrdering(); + private List<SortOptions> getSortStrategy(SearchOrdering searchOrdering) { String sortingScriptTemplate = "double boost = 0;" + "if (doc.containsKey('${badgeRewarded}') && doc['${badgeRewarded}'].size() > 0) {" + @@ -601,7 +605,7 @@ public class MetaDataRepository { "if (doc.containsKey('${views}') && doc['${views}'].size() > 0) {" + " boost = boost + doc['${views}'].value * params.factorViews;" + "}" + - "return boost + _score;"; + "return boost;"; Map<String, String> templateValueMapping = Map.of( "badgeRewarded", @@ -612,25 +616,23 @@ public class MetaDataRepository { SearchRepositoryConstants.SEARCHSTATISTICS_VIEWS ); - double maxFactor = - DoubleStream - .of(searchOrdering.getFactorBadge(), searchOrdering.getFactorDownloads(), searchOrdering.getFactorViews()) - .max() - .getAsDouble() * - 0.2; - - return new ScriptScoreQuery.Builder() - .script(sb -> - sb.inline(isb -> - isb - .lang("painless") - .source(StringSubstitutor.replace(sortingScriptTemplate, templateValueMapping)) - .params("factorBadge", JsonData.of(searchOrdering.getFactorBadge() / maxFactor)) - .params("factorDownloads", JsonData.of(searchOrdering.getFactorDownloads() / maxFactor)) - .params("factorViews", JsonData.of(searchOrdering.getFactorViews() / maxFactor)) + var scriptSort = SortOptionsBuilders.script(b -> + b + .type(ScriptSortType.Number) + .order(SortOrder.Desc) + .script(sb -> + sb.inline(isb -> + isb + .lang("painless") + .source(StringSubstitutor.replace(sortingScriptTemplate, templateValueMapping)) + .params("factorBadge", JsonData.of(searchOrdering.getFactorBadge())) + .params("factorDownloads", JsonData.of(searchOrdering.getFactorDownloads())) + .params("factorViews", JsonData.of(searchOrdering.getFactorViews())) + ) ) - ) - .query(q -> q.bool(query)); + ); + + return List.of(SortOptionsBuilders.score(s -> s.order(SortOrder.Desc)), scriptSort); } /** @@ -642,7 +644,7 @@ public class MetaDataRepository { * @return */ @SuppressWarnings("PMD.ExcessiveMethodLength") - private BoolQuery.Builder createBoolQueryBuilder(SearchInputDTO searchInputDTO, Optional<User> user, boolean ignoreUserAuthorization) { + private BoolQuery.Builder createQueryBuilder(SearchInputDTO searchInputDTO, Optional<User> user, boolean ignoreUserAuthorization) { BoolQuery.Builder queryBuilder = new BoolQuery.Builder(); forEachTerm( -- GitLab