blob: 30dee0e540e003347949ed621047c1bca95d7d80 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.grouping.result;
import com.yahoo.component.chain.dependencies.Before;
import com.yahoo.processing.request.CompoundName;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.grouping.vespa.GroupingExecutor;
import com.yahoo.search.result.Hit;
import com.yahoo.search.result.HitGroup;
import com.yahoo.search.searchchain.Execution;
import java.util.Iterator;
/**
* Flattens a grouping result into a flat list of hits on the top level in the returned result.
* Useful when using grouping to create hits with diversity and similar.
*
* @author bratseth
*/
@Before(GroupingExecutor.COMPONENT_NAME)
public class FlatteningSearcher extends Searcher {
private final CompoundName flatten = CompoundName.fromComponents("grouping", "flatten");
@Override
public Result search(Query query, Execution execution) {
if ( ! query.properties().getBoolean(flatten, true)) return execution.search(query);
if ( ! query.properties().getBoolean("flatten", true)) return execution.search(query);
query.trace("Flattening groups", 2);
int originalHits = query.getHits();
query.setHits(0);
Result result = execution.search(query);
query.setHits(originalHits);
flatten(result.hits(), 0, result);
return result;
}
private void flatten(HitGroup hits, int level, Result result) {
int hitsLeft = hits.size(); // Iterate only through the initial size
for (Iterator<Hit> i = hits.iterator(); i.hasNext() && hitsLeft-- > 0;) {
Hit hit = i.next();
// If we count the number of unique groups, use that as total hit count.
if (level == 0 && (hit instanceof RootGroup)) {
if (hit.fields().get("count()") != null)
result.setTotalHitCount((long)hit.fields().get("count()"));
}
if (hit instanceof HitGroup) {
flatten((HitGroup)hit, level++, result);
i.remove();
} else {
result.hits().add(hit);
}
}
}
}
|