Skip to content

Commit

Permalink
Feature/search str for debug (#1546)
Browse files Browse the repository at this point in the history
Include _str values when in ES score debug mode
  • Loading branch information
kwahlin authored Jan 21, 2025
1 parent 5a3305e commit b881ddb
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions whelk-core/src/main/groovy/whelk/search2/QueryResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static whelk.JsonLd.SEARCH_KEY;
import static whelk.search2.QueryUtil.castToStringObjectMap;
import static whelk.util.DocumentUtil.getAtPath;
import static whelk.util.DocumentUtil.traverse;
Expand Down Expand Up @@ -81,7 +82,13 @@ private static Map<String, Object> normalizeResponse(Map<?, ?> esResponse) {
return norm;
}

private record EsItem(Map<String, Object> map) {
private class EsItem {
private final Map<String, Object> map;

EsItem(Map<String, Object> map) {
this.map = map;
}

private Map<String, Object> toLd(Function<Map<String, Object>, Map<String, Object>> applyLens) {
LdItem ldItem = new LdItem(applyLens.apply(map));

Expand All @@ -90,7 +97,10 @@ private Map<String, Object> toLd(Function<Map<String, Object>, Map<String, Objec
// reverseLinks must be re-added because they might get filtered out in applyLens().
getReverseLinks().ifPresent(ldItem::addReverseLinks);

getScoreExplanation().ifPresent(ldItem::addScore);
if (debug.contains(QueryParams.Debug.ES_SCORE)) {
ldItem.addSearchStrings(this);
getScoreExplanation().ifPresent(ldItem::addScore);
}

return ldItem.map;
}
Expand Down Expand Up @@ -135,6 +145,18 @@ private void addReverseLinks(Map<String, Object> reverseLinks) {
map.put("reverseLinks", reverseLinks);
}

@SuppressWarnings({"rawtypes", "unchecked"})
private void addSearchStrings(EsItem esItem) {
DocumentUtil.traverse(esItem.map, (value, path) -> {
if (!path.isEmpty() && path.getLast().equals(SEARCH_KEY)) {
if (getAtPath(map, path.subList(0, path.size() - 1)) instanceof Map m) {
m.put(SEARCH_KEY, value);
}
}
return new DocumentUtil.Nop();
});
}

private void addScore(Map<String, Object> scoreExplanation) {
var scorePerField = getScorePerField(scoreExplanation);
var totalScore = scorePerField.values().stream().reduce((double) 0, Double::sum);
Expand Down

0 comments on commit b881ddb

Please sign in to comment.