Skip to content

Commit

Permalink
Revert "Feature/lws 88 boosting (#1531)"
Browse files Browse the repository at this point in the history
This reverts commit 4c997a9.
  • Loading branch information
olovy committed Dec 11, 2024
1 parent 4c997a9 commit b35d538
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 753 deletions.
32 changes: 3 additions & 29 deletions rest/src/main/groovy/whelk/rest/api/SearchUtils2.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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 @@ -68,30 +67,21 @@ 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)) {
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);
partialCollectionView.put(QueryParams.ApiParams.DEBUG, Map.of(QueryParams.Debug.ES_QUERY, esQueryDsl));
}

return partialCollectionView;
}

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

queryDsl.put("query", getEsQuery(queryTree, queryParams.boostFields));
queryDsl.put("query", queryTree.toEs(queryUtil, disambiguate));
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 @@ -102,28 +92,12 @@ 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;
}

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

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

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

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

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

return responseMap
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
}
catch (Exception e) {
if (isBadRequest(e)) {
Expand Down
Loading

0 comments on commit b35d538

Please sign in to comment.