Skip to content

Commit

Permalink
Merge pull request #32901 from vespa-engine/bratseth/field-collapsing
Browse files Browse the repository at this point in the history
Cleanup: Non-functional changes only
  • Loading branch information
arnej27959 authored Nov 20, 2024
2 parents f76c38e + 7392f40 commit e15be81
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Map;

/**
* A searcher which does parameterized collapsing.
* A searcher which removes hits which has an already seen value of a given field.
*
* @author Steinar Knutsen
*/
Expand All @@ -28,8 +28,9 @@
public class FieldCollapsingSearcher extends Searcher {

private static final CompoundName collapse = CompoundName.from("collapse");
private static final CompoundName collapsefield = CompoundName.from("collapsefield");
private static final CompoundName collapsesize = CompoundName.from("collapsesize");
// TODO: Use collapse.field and collapse.size and make these aliases
private static final CompoundName collapseField = CompoundName.from("collapsefield");
private static final CompoundName collapseSize = CompoundName.from("collapsesize");
private static final CompoundName collapseSummaryName = CompoundName.from("collapse.summary");

/** Separator used for the fieldnames in collapsefield */
Expand All @@ -40,15 +41,13 @@ public class FieldCollapsingSearcher extends Searcher {

/**
* The max number of hits that will be preserved per unique
* value of the collapsing parameter,
* if no field-specific value is configured.
* value of the collapsing parameter, if no field-specific value is configured.
*/
private int defaultCollapseSize;

/**
* The factor by which to scale up the requested number of hits
* from the next searcher in the chain, because collapsing will
* likely delete many hits.
* from the next searcher in the chain, because collapsing will likely delete many hits.
*/
private double extraFactor;

Expand All @@ -60,10 +59,8 @@ public FieldCollapsingSearcher() {
@Inject
@SuppressWarnings("unused")
public FieldCollapsingSearcher(QrSearchersConfig config) {
QrSearchersConfig.Com.Yahoo.Prelude.Searcher.FieldCollapsingSearcher
s = config.com().yahoo().prelude().searcher().FieldCollapsingSearcher();

init(s.collapsesize(), s.extrafactor());
var searcherConfig = config.com().yahoo().prelude().searcher().FieldCollapsingSearcher();
init(searcherConfig.collapsesize(), searcherConfig.extrafactor());
}

/**
Expand Down Expand Up @@ -91,14 +88,11 @@ private void init(int collapseSize, double extraFactor) {
*/
@Override
public Result search(com.yahoo.search.Query query, Execution execution) {
String collapseFieldParam = query.properties().getString(collapsefield);

String collapseFieldParam = query.properties().getString(collapseField);
if (collapseFieldParam == null) return execution.search(query);

String[] collapseFields = collapseFieldParam.split(separator);

int globalCollapseSize = query.properties().getInteger(collapsesize, defaultCollapseSize);

int globalCollapseSize = query.properties().getInteger(collapseSize, defaultCollapseSize);
query.properties().set(collapse, "0");

int hitsToRequest = query.getHits() != 0 ? (int) Math.ceil((query.getOffset() + query.getHits() + 1) * extraFactor) : 0;
Expand All @@ -118,9 +112,7 @@ public Result search(com.yahoo.search.Query query, Execution execution) {
resultSource = search(query.clone(), execution, nextOffset, hitsToRequest);
fill(resultSource, summaryClass, execution);

collapse(result, knownCollapses, resultSource,
collapseFields, query.properties(), globalCollapseSize
);
collapse(result, knownCollapses, resultSource, collapseFields, query.properties(), globalCollapseSize);

hitsAfterCollapse = result.getHitCount();
if (resultSource.getTotalHitCount() < (hitsToRequest + nextOffset)) {
Expand All @@ -140,7 +132,7 @@ public Result search(com.yahoo.search.Query query, Execution execution) {

// Set correct meta information
result.mergeWith(resultSource);
// Keep only (offset,.. offset+hits) hits
// Keep only (offset ... offset+hits) hits
result.hits().trim(query.getOffset(), query.getHits());
// Mark query as query with collapsing
query.properties().set(collapse, "1");
Expand All @@ -160,22 +152,17 @@ private Result search(Query query, Execution execution, int offset, int hits) {
*/
private void collapse(Result result, Map<String, Integer> knownCollapses, Result resultSource,
String[] collapseFields, Properties queryProperties, int globalCollapseSize) {

for (Hit unknownHit : resultSource.hits()) {
if (!(unknownHit instanceof FastHit hit)) {
result.hits().add(unknownHit);
continue;
}

boolean addHit = true;

for (String collapseField : collapseFields) {

Object peek = hit.getField(collapseField);
String collapseId = peek != null ? peek.toString() : null;
if (collapseId == null) {
continue;
}
if (collapseId == null) continue;

// prepending the fieldname is necessary to distinguish between values in the different collapsefields
// @ cannot occur in fieldnames
Expand All @@ -199,19 +186,14 @@ private void collapse(Result result, Map<String, Integer> knownCollapses, Result
}
}

if (addHit) {
if (addHit)
result.hits().add(hit);
}
}
}

private int getCollapseSize(Properties properties, String fieldName, int globalCollapseSize) {
Integer fieldCollapseSize = properties.getInteger(collapsesize.append(fieldName));

if (fieldCollapseSize != null) {
return fieldCollapseSize;
}

return globalCollapseSize;
Integer fieldCollapseSize = properties.getInteger(collapseSize.append(fieldName));
return fieldCollapseSize != null ? fieldCollapseSize : globalCollapseSize;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private static Map<String, Map<String, DocumentScript>> createScriptsMap(Docume

ScriptExpression script = new ScriptExpression(expressions);
script.select(fieldPathOptimizer, fieldPathOptimizer);
fieldScripts.put(FULL, new DocumentScript(ilscript.doctype(), ilscript.docfield(),script));
fieldScripts.put(FULL, new DocumentScript(ilscript.doctype(), ilscript.docfield(), script));
documentFieldScripts.put(ilscript.doctype(), Collections.unmodifiableMap(fieldScripts));
}
return Collections.unmodifiableMap(documentFieldScripts);
Expand Down

0 comments on commit e15be81

Please sign in to comment.