From 5a62b3058f7d4a5840875233e5ade323a7c804ca Mon Sep 17 00:00:00 2001 From: Viktor Lofgren Date: Tue, 16 Jan 2024 10:55:24 +0100 Subject: [PATCH] (query-api) Make the search set identifier a string value in the API This will free the core marginalia search engine to use arbitrary search set definitions, while the app can use its hardcoded defaults. --- .../client/model/query/SearchSpecification.java | 2 +- .../nu/marginalia/query/QueryProtobufCodec.java | 8 ++++---- .../nu/marginalia/query/model/QueryParams.java | 4 ++-- .../java/nu/marginalia/api/ApiSearchOperator.java | 2 +- .../search/SearchQueryParamFactory.java | 8 ++++---- .../control/app/svc/SearchToBanService.java | 2 +- .../marginalia/index/svc/IndexQueryService.java | 4 +--- .../index/svc/IndexSearchSetsService.java | 15 +++++++++------ .../IndexQueryServiceIntegrationSmokeTest.java | 4 ++-- .../svc/IndexQueryServiceIntegrationTest.java | 2 +- .../IndexQueryServiceIntegrationTestModule.java | 2 +- .../nu/marginalia/query/QueryBasicInterface.java | 8 ++++++-- .../nu/marginalia/query/svc/QueryFactoryTest.java | 2 +- 13 files changed, 34 insertions(+), 29 deletions(-) diff --git a/code/api/index-api/src/main/java/nu/marginalia/index/client/model/query/SearchSpecification.java b/code/api/index-api/src/main/java/nu/marginalia/index/client/model/query/SearchSpecification.java index 27d815e9..2ea743cf 100644 --- a/code/api/index-api/src/main/java/nu/marginalia/index/client/model/query/SearchSpecification.java +++ b/code/api/index-api/src/main/java/nu/marginalia/index/client/model/query/SearchSpecification.java @@ -15,7 +15,7 @@ public class SearchSpecification { /** If present and not empty, limit the search to these domain IDs */ public List domains; - public SearchSetIdentifier searchSetIdentifier; + public String searchSetIdentifier; public final String humanQuery; diff --git a/code/api/query-api/src/main/java/nu/marginalia/query/QueryProtobufCodec.java b/code/api/query-api/src/main/java/nu/marginalia/query/QueryProtobufCodec.java index 48e8a94a..36b16a55 100644 --- a/code/api/query-api/src/main/java/nu/marginalia/query/QueryProtobufCodec.java +++ b/code/api/query-api/src/main/java/nu/marginalia/query/QueryProtobufCodec.java @@ -31,7 +31,7 @@ public class QueryProtobufCodec { builder.addSubqueries(IndexProtobufCodec.convertSearchSubquery(subquery)); } - builder.setSearchSetIdentifier(query.specs.searchSetIdentifier.name()); + builder.setSearchSetIdentifier(query.specs.searchSetIdentifier); builder.setHumanQuery(request.getHumanQuery()); builder.setQuality(convertSpecLimit(query.specs.quality)); @@ -62,7 +62,7 @@ public class QueryProtobufCodec { convertSpecLimit(request.getDomainCount()), request.getDomainIdsList(), IndexProtobufCodec.convertQueryLimits(request.getQueryLimits()), - SearchSetIdentifier.valueOf(request.getSearchSetIdentifier())); + request.getSearchSetIdentifier()); } @@ -133,7 +133,7 @@ public class QueryProtobufCodec { return new SearchSpecification( subqueries, specs.getDomainsList(), - SearchSetIdentifier.valueOf(specs.getSearchSetIdentifier()), + specs.getSearchSetIdentifier(), specs.getHumanQuery(), IndexProtobufCodec.convertSpecLimit(specs.getQuality()), IndexProtobufCodec.convertSpecLimit(specs.getYear()), @@ -159,7 +159,7 @@ public class QueryProtobufCodec { .setYear(convertSpecLimit(params.year())) .setSize(convertSpecLimit(params.size())) .setRank(convertSpecLimit(params.rank())) - .setSearchSetIdentifier(params.identifier().name()); + .setSearchSetIdentifier(params.identifier()); if (params.nearDomain() != null) builder.setNearDomain(params.nearDomain()); diff --git a/code/api/query-api/src/main/java/nu/marginalia/query/model/QueryParams.java b/code/api/query-api/src/main/java/nu/marginalia/query/model/QueryParams.java index 6b88dbc6..9d83e117 100644 --- a/code/api/query-api/src/main/java/nu/marginalia/query/model/QueryParams.java +++ b/code/api/query-api/src/main/java/nu/marginalia/query/model/QueryParams.java @@ -23,10 +23,10 @@ public record QueryParams( SpecificationLimit domainCount, List domainIds, QueryLimits limits, - SearchSetIdentifier identifier + String identifier ) { - public QueryParams(String query, QueryLimits limits, SearchSetIdentifier identifier) { + public QueryParams(String query, QueryLimits limits, String identifier) { this(query, null, List.of(), List.of(), diff --git a/code/services-application/api-service/src/main/java/nu/marginalia/api/ApiSearchOperator.java b/code/services-application/api-service/src/main/java/nu/marginalia/api/ApiSearchOperator.java index f60a1750..f02afedd 100644 --- a/code/services-application/api-service/src/main/java/nu/marginalia/api/ApiSearchOperator.java +++ b/code/services-application/api-service/src/main/java/nu/marginalia/api/ApiSearchOperator.java @@ -55,7 +55,7 @@ public class ApiSearchOperator { Math.min(100, count), 150, 8192), - searchSet); + searchSet.name()); } private SearchSetIdentifier selectSearchSet(int index) { diff --git a/code/services-application/search-service/src/main/java/nu/marginalia/search/SearchQueryParamFactory.java b/code/services-application/search-service/src/main/java/nu/marginalia/search/SearchQueryParamFactory.java index 03acc479..edb1b62f 100644 --- a/code/services-application/search-service/src/main/java/nu/marginalia/search/SearchQueryParamFactory.java +++ b/code/services-application/search-service/src/main/java/nu/marginalia/search/SearchQueryParamFactory.java @@ -35,7 +35,7 @@ public class SearchQueryParamFactory { SpecificationLimit.none(), List.of(), new QueryLimits(1, 25, 200, 8192), - profile.searchSetIdentifier + profile.searchSetIdentifier.name() ); } @@ -54,7 +54,7 @@ public class SearchQueryParamFactory { SpecificationLimit.none(), List.of(), new QueryLimits(count, count, 100, 512), - SearchSetIdentifier.NONE + SearchSetIdentifier.NONE.name() ); } @@ -72,7 +72,7 @@ public class SearchQueryParamFactory { SpecificationLimit.none(), List.of(), new QueryLimits(100, 100, 100, 512), - SearchSetIdentifier.NONE + SearchSetIdentifier.NONE.name() ); } @@ -90,7 +90,7 @@ public class SearchQueryParamFactory { SpecificationLimit.none(), List.of(), new QueryLimits(100, 100, 100, 512), - SearchSetIdentifier.NONE + SearchSetIdentifier.NONE.name() ); } } diff --git a/code/services-core/control-service/src/main/java/nu/marginalia/control/app/svc/SearchToBanService.java b/code/services-core/control-service/src/main/java/nu/marginalia/control/app/svc/SearchToBanService.java index 12118989..6ff198da 100644 --- a/code/services-core/control-service/src/main/java/nu/marginalia/control/app/svc/SearchToBanService.java +++ b/code/services-core/control-service/src/main/java/nu/marginalia/control/app/svc/SearchToBanService.java @@ -79,7 +79,7 @@ public class SearchToBanService { private Object executeQuery(Context ctx, String query) { return queryClient.search(ctx, new QueryParams( query, new QueryLimits(2, 200, 250, 8192), - SearchSetIdentifier.NONE + "NONE" )); } } diff --git a/code/services-core/index-service/src/main/java/nu/marginalia/index/svc/IndexQueryService.java b/code/services-core/index-service/src/main/java/nu/marginalia/index/svc/IndexQueryService.java index 8560b38a..cc9d8e04 100644 --- a/code/services-core/index-service/src/main/java/nu/marginalia/index/svc/IndexQueryService.java +++ b/code/services-core/index-service/src/main/java/nu/marginalia/index/svc/IndexQueryService.java @@ -261,9 +261,7 @@ public class IndexQueryService extends IndexApiImplBase { return new SmallSearchSet(request.getDomainsList()); } - return searchSetsService.getSearchSetByName( - SearchSetIdentifier.valueOf(request.getSearchSetIdentifier()) - ); + return searchSetsService.getSearchSetByName(request.getSearchSetIdentifier()); } private SearchResultSet executeSearch(SearchParameters params) throws SQLException { diff --git a/code/services-core/index-service/src/main/java/nu/marginalia/index/svc/IndexSearchSetsService.java b/code/services-core/index-service/src/main/java/nu/marginalia/index/svc/IndexSearchSetsService.java index 47dcf5b2..d8f90d34 100644 --- a/code/services-core/index-service/src/main/java/nu/marginalia/index/svc/IndexSearchSetsService.java +++ b/code/services-core/index-service/src/main/java/nu/marginalia/index/svc/IndexSearchSetsService.java @@ -80,16 +80,19 @@ public class IndexSearchSetsService { return domainRankings; } - public SearchSet getSearchSetByName(SearchSetIdentifier searchSetIdentifier) { + public SearchSet getSearchSetByName(String searchSetIdentifier) { + if (null == searchSetIdentifier) { return anySet; } + return switch (searchSetIdentifier) { - case NONE -> anySet; - case POPULAR -> popularSet; - case ACADEMIA -> academiaSet; - case SMALLWEB -> smallWebSet; - case BLOGS -> blogsSet; + case "POPULAR" -> popularSet; + case "ACADEMIA" -> academiaSet; + case "SMALLWEB" -> smallWebSet; + case "BLOGS" -> blogsSet; + case "NONE", "" -> anySet; + default -> throw new IllegalArgumentException("Unknown search set"); }; } diff --git a/code/services-core/index-service/src/test/java/nu/marginalia/index/svc/IndexQueryServiceIntegrationSmokeTest.java b/code/services-core/index-service/src/test/java/nu/marginalia/index/svc/IndexQueryServiceIntegrationSmokeTest.java index f8c95cf2..c9a07e60 100644 --- a/code/services-core/index-service/src/test/java/nu/marginalia/index/svc/IndexQueryServiceIntegrationSmokeTest.java +++ b/code/services-core/index-service/src/test/java/nu/marginalia/index/svc/IndexQueryServiceIntegrationSmokeTest.java @@ -129,7 +129,7 @@ public class IndexQueryServiceIntegrationSmokeTest { .domainCount(SpecificationLimit.none()) .rankingParams(ResultRankingParameters.sensibleDefaults()) .domains(new ArrayList<>()) - .searchSetIdentifier(SearchSetIdentifier.NONE) + .searchSetIdentifier("NONE") .subqueries(List.of(new SearchSubquery( List.of("3", "5", "2"), List.of("4"), Collections.emptyList(), Collections.emptyList(), Collections.emptyList()))).build()); @@ -207,7 +207,7 @@ public class IndexQueryServiceIntegrationSmokeTest { .rank(SpecificationLimit.none()) .domainCount(SpecificationLimit.none()) .queryStrategy(QueryStrategy.SENTENCE) - .searchSetIdentifier(SearchSetIdentifier.NONE) + .searchSetIdentifier("NONE") .rankingParams(ResultRankingParameters.sensibleDefaults()) .subqueries(List.of(new SearchSubquery( List.of("4"), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), diff --git a/code/services-core/index-service/src/test/java/nu/marginalia/index/svc/IndexQueryServiceIntegrationTest.java b/code/services-core/index-service/src/test/java/nu/marginalia/index/svc/IndexQueryServiceIntegrationTest.java index ca5cafe0..55fea27f 100644 --- a/code/services-core/index-service/src/test/java/nu/marginalia/index/svc/IndexQueryServiceIntegrationTest.java +++ b/code/services-core/index-service/src/test/java/nu/marginalia/index/svc/IndexQueryServiceIntegrationTest.java @@ -431,7 +431,7 @@ public class IndexQueryServiceIntegrationTest { .domainCount(SpecificationLimit.none()) .rankingParams(ResultRankingParameters.sensibleDefaults()) .domains(new ArrayList<>()) - .searchSetIdentifier(SearchSetIdentifier.NONE) + .searchSetIdentifier("NONE") .subqueries(List.of()); return mutator.apply(builder).build(); diff --git a/code/services-core/index-service/src/test/java/nu/marginalia/index/svc/IndexQueryServiceIntegrationTestModule.java b/code/services-core/index-service/src/test/java/nu/marginalia/index/svc/IndexQueryServiceIntegrationTestModule.java index 746657d8..5089bd5f 100644 --- a/code/services-core/index-service/src/test/java/nu/marginalia/index/svc/IndexQueryServiceIntegrationTestModule.java +++ b/code/services-core/index-service/src/test/java/nu/marginalia/index/svc/IndexQueryServiceIntegrationTestModule.java @@ -69,7 +69,7 @@ public class IndexQueryServiceIntegrationTestModule extends AbstractModule { bind(ProcessHeartbeat.class).toInstance(new FakeProcessHeartbeat()); IndexSearchSetsService setsServiceMock = Mockito.mock(IndexSearchSetsService.class); - when(setsServiceMock.getSearchSetByName(SearchSetIdentifier.NONE)).thenReturn(new SearchSetAny()); + when(setsServiceMock.getSearchSetByName("NONE")).thenReturn(new SearchSetAny()); when(setsServiceMock.getDomainRankings()).thenReturn(new DomainRankings()); bind(IndexSearchSetsService.class).toInstance(setsServiceMock); diff --git a/code/services-core/query-service/src/main/java/nu/marginalia/query/QueryBasicInterface.java b/code/services-core/query-service/src/main/java/nu/marginalia/query/QueryBasicInterface.java index ddef20d6..363b493f 100644 --- a/code/services-core/query-service/src/main/java/nu/marginalia/query/QueryBasicInterface.java +++ b/code/services-core/query-service/src/main/java/nu/marginalia/query/QueryBasicInterface.java @@ -44,9 +44,13 @@ public class QueryBasicInterface { if (queryParam == null) { return renderer.render(new Object()); } + + int count = request.queryParams("count") == null ? 10 : Integer.parseInt(request.queryParams("count")); + String set = request.queryParams("set") == null ? "" : request.queryParams("set"); + var query = queryFactory.createQuery(new QueryParams(queryParam, new QueryLimits( - 1, 10, 250, 8192 - ), SearchSetIdentifier.NONE)); + 1, count, 250, 8192 + ), set)); var rsp = indexClient.query( Context.fromRequest(request), diff --git a/code/services-core/query-service/src/test/java/nu/marginalia/query/svc/QueryFactoryTest.java b/code/services-core/query-service/src/test/java/nu/marginalia/query/svc/QueryFactoryTest.java index c5f2eb42..c0874acc 100644 --- a/code/services-core/query-service/src/test/java/nu/marginalia/query/svc/QueryFactoryTest.java +++ b/code/services-core/query-service/src/test/java/nu/marginalia/query/svc/QueryFactoryTest.java @@ -49,7 +49,7 @@ public class QueryFactoryTest { SpecificationLimit.none(), null, new QueryLimits(100, 100, 100, 100), - SearchSetIdentifier.BLOGS)).specs; + "NONE")).specs; } @Test