Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/lws 88 boosting #1531

Merged
merged 12 commits into from
Dec 11, 2024
32 changes: 29 additions & 3 deletions rest/src/main/groovy/whelk/rest/api/SearchUtils2.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.util.*;

import static whelk.search2.EsBoost.addConstantBoosts;
import static whelk.search2.Spell.buildSpellSuggestions;
import static whelk.util.Jackson.mapper;

Expand Down Expand Up @@ -67,21 +68,30 @@ Map<String, Object> doSearch(Map<String, String[]> queryParameters) throws Inval

Map<String, Object> partialCollectionView = getPartialCollectionView(queryRes, qTree, queryParams, appParams);

Map<String, Object> debugView = new HashMap<>();
if (queryParams.debug.contains(QueryParams.Debug.ES_QUERY)) {
partialCollectionView.put(QueryParams.ApiParams.DEBUG, Map.of(QueryParams.Debug.ES_QUERY, esQueryDsl));
debugView.put(QueryParams.Debug.ES_QUERY, esQueryDsl);
}
if (queryParams.debug.contains(QueryParams.Debug.ES_SCORE)) {
debugView.put(QueryParams.Debug.ES_SCORE, queryRes.scores);
}
if (!debugView.isEmpty()) {
partialCollectionView.put(QueryParams.ApiParams.DEBUG, debugView);
}

return partialCollectionView;
}

private Map<String, Object> getEsQueryDsl(QueryTree queryTree, QueryParams queryParams, AppParams.StatsRepr statsRepr) {
var queryDsl = new LinkedHashMap<String, Object>();
queryDsl.put("query", queryTree.toEs(queryUtil, disambiguate));

queryDsl.put("query", getEsQuery(queryTree, queryParams.boostFields));
queryDsl.put("size", queryParams.limit);
queryDsl.put("from", queryParams.offset);
queryDsl.put("sort", (queryParams.sortBy == Sort.DEFAULT_BY_RELEVANCY && queryTree.isWild()
? Sort.BY_DOC_ID
: queryParams.sortBy).getSortClauses(queryUtil::getSortField));

if (queryParams.spell.suggest && queryUtil.esMappings.isSpellCheckAvailable()) {
var spellQuery = Spell.getSpellQuery(queryTree);
if (spellQuery.isPresent()) {
Expand All @@ -92,12 +102,28 @@ private Map<String, Object> getEsQueryDsl(QueryTree queryTree, QueryParams query
}
}
}

queryDsl.put("aggs", Aggs.buildAggQuery(statsRepr, disambiguate, queryTree.getOutsetType(), queryUtil::getNestedPath));
queryDsl.put("track_total_hits", true);

if (queryParams.debug.contains(QueryParams.Debug.ES_SCORE)) {
queryDsl.put("explain", true);
// Scores won't be calculated when also using sort unless explicitly asked for
queryDsl.put("track_scores", true);
}

return queryDsl;
}

public Map<String, Object> getPartialCollectionView(QueryResult queryResult,
private Map<String, Object> getEsQuery(QueryTree queryTree, List<String> boostFields) {
if (!boostFields.isEmpty()) {
return queryTree.toEs(queryUtil, disambiguate, boostFields);
}
boostFields = queryUtil.esBoost.getBoostFields(queryTree.collectTypes());
return addConstantBoosts(queryTree.toEs(queryUtil, disambiguate, boostFields));
}

private Map<String, Object> getPartialCollectionView(QueryResult queryResult,
QueryTree qt,
QueryParams queryParams,
AppParams appParams) {
Expand Down
23 changes: 4 additions & 19 deletions whelk-core/src/main/groovy/whelk/component/ElasticSearch.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -580,18 +580,13 @@ class ElasticSearch {
}

Map query(Map jsonDsl) {
return performQuery(
jsonDsl,
getQueryUrl(),
{ def d = it."_source"; d."_id" = it."_id"; return d }
)
return performQuery(jsonDsl, getQueryUrl())
}

Map queryIds(Map jsonDsl) {
return performQuery(
jsonDsl,
getQueryUrl(['took','hits.total','hits.hits._id']),
{ it."_id" }
getQueryUrl(['took','hits.total','hits.hits._id'])
)
}

Expand Down Expand Up @@ -629,7 +624,7 @@ class ElasticSearch {
return super.hashCode()
}

private Map performQuery(Map jsonDsl, String queryUrl, Closure<Map> hitCollector) {
private Map performQuery(Map jsonDsl, String queryUrl) {
try {
def start = System.currentTimeMillis()
String responseBody = client.performRequest('POST',
Expand All @@ -643,17 +638,7 @@ class ElasticSearch {
log.info("ES query took ${duration} (${responseMap.took} server-side)")
}

def results = [:]

results.startIndex = jsonDsl.from
results.totalHits = responseMap.hits.total.value
results.items = responseMap.hits.hits.collect(hitCollector)
results.aggregations = responseMap.aggregations
// Spell checking
if (responseMap.suggest?.simple_phrase) {
results.spell = responseMap.suggest.simple_phrase[0].options
}
return results
return responseMap
}
catch (Exception e) {
if (isBadRequest(e)) {
Expand Down
Loading
Loading