Skip to content

Commit

Permalink
(search2): Make stats optional in API response (#1566)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwahlin committed Feb 27, 2025
1 parent f9e4927 commit 7d2fd4c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 7 additions & 2 deletions rest/src/main/groovy/whelk/rest/api/SearchUtils2.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ private Map<String, Object> getEsQueryDsl(QueryTree queryTree, QueryParams query
}
}

queryDsl.put("aggs", buildAggQuery(statsRepr, whelk.getJsonld(), queryTree.collectRulingTypes(whelk.getJsonld()), queryUtil::getNestedPath));
if (!queryParams.skipStats) {
queryDsl.put("aggs", buildAggQuery(statsRepr, whelk.getJsonld(), queryTree.collectRulingTypes(whelk.getJsonld()), queryUtil::getNestedPath));
}

queryDsl.put("track_total_hits", true);

if (queryParams.debug.contains(QueryParams.Debug.ES_SCORE)) {
Expand Down Expand Up @@ -130,7 +133,9 @@ private Map<String, Object> getPartialCollectionView(QueryResult queryResult,
view.put("search", Map.of("mapping", List.of(qt.toSearchMapping(queryParams.getNonQueryParams(0)))));
view.putAll(Pagination.makeLinks(queryResult.numHits, queryUtil.maxItems(), freeText, fullQuery, queryParams));
view.put("items", queryResult.collectItems(queryUtil.getApplyLensFunc(queryParams)));
view.put("stats", new Stats(whelk.getJsonld(), queryUtil, qt, queryResult, queryParams, appParams).build());
if (!queryParams.skipStats) {
view.put("stats", new Stats(whelk.getJsonld(), queryUtil, qt, queryResult, queryParams, appParams).build());
}
if (!queryResult.spell.isEmpty()) {
view.put("_spell", buildSpellSuggestions(queryResult, qt, queryParams.getNonQueryParams(0)));
}
Expand Down
7 changes: 7 additions & 0 deletions whelk-core/src/main/groovy/whelk/search2/QueryParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static class ApiParams {
public static final String DEBUG = "_debug";
public static final String APP_CONFIG = "_appConfig";
public static final String BOOST = "_boost";
public static final String STATS = "_stats";
}

public static class Debug {
Expand All @@ -50,6 +51,8 @@ public static class Debug {
public final String q;
public final String i;

public final boolean skipStats;

public QueryParams(Map<String, String[]> apiParameters) throws InvalidQueryException {
this.sortBy = Sort.fromString(getOptionalSingleNonEmpty(ApiParams.SORT, apiParameters).orElse(""));
this.object = getOptionalSingleNonEmpty(ApiParams.OBJECT, apiParameters).orElse(null);
Expand All @@ -63,6 +66,7 @@ public QueryParams(Map<String, String[]> apiParameters) throws InvalidQueryExcep
this.boostFields = getMultiple(ApiParams.BOOST, apiParameters);
this.q = getOptionalSingle(ApiParams.QUERY, apiParameters).orElse("");
this.i = getOptionalSingle(ApiParams.SIMPLE_FREETEXT, apiParameters).orElse("");
this.skipStats = getOptionalSingle(ApiParams.STATS, apiParameters).map("false"::equalsIgnoreCase).isPresent();
}

public Map<String, String> getNonQueryParams() {
Expand Down Expand Up @@ -98,6 +102,9 @@ public Map<String, String> getNonQueryParams(int offset) {
if (!boostFields.isEmpty()) {
params.put(ApiParams.BOOST, String.join(",", boostFields));
}
if (skipStats) {
params.put(ApiParams.STATS, "false");
}
return params;
}

Expand Down

0 comments on commit 7d2fd4c

Please sign in to comment.