use of com.yahoo.search.grouping.result.GroupList in project vespa by vespa-engine.
the class UniqueGroupingSearcher method dedupe.
/**
* Until we can use the grouping pagination features in 5.1, we'll have to support offset
* by simply requesting and discarding hit #0 up to hit #offset.
*/
private static Result dedupe(Query query, Execution execution, String dedupField) {
Sorting sorting = query.getRanking().getSorting();
if (sorting != null && sorting.fieldOrders().size() > 1) {
query.trace("Can not use grouping for deduping with multi-level sorting.", 3);
// we'd ever want to actually use it (and a bit harder to implement as well).
return execution.search(query);
}
int hits = query.getHits();
int offset = query.getOffset();
int groupingHits = hits + offset;
GroupingRequest groupingRequest = GroupingRequest.newInstance(query);
groupingRequest.setRootOperation(buildGroupingExpression(dedupField, groupingHits, query.getPresentation().getSummary(), sorting));
query.setHits(0);
query.setOffset(0);
Result result = execution.search(query);
// query could have changed further down in the chain
query = result.getQuery();
query.setHits(hits);
query.setOffset(offset);
Group root = groupingRequest.getResultGroup(result);
if (null == root) {
String msg = "Result group not found for deduping grouping request, returning empty result.";
query.trace(msg, 3);
log.log(LogLevel.WARNING, msg);
throw new IllegalStateException("Failed to produce deduped result set.");
}
// hide our tracks
result.hits().remove(root.getId().toString());
GroupList resultGroups = root.getGroupList(dedupField);
if (resultGroups == null) {
query.trace("Deduping grouping request returned no hits, returning empty result.", 3);
return result;
}
// Make sure that .addAll() doesn't re-order the hits we copy from the grouping
// framework. The groups are already in the order they should be.
result.hits().setOrderer(NOP_ORDERER);
result.hits().addAll(getRequestedHits(resultGroups, offset, hits));
Long countField = (Long) root.getField(LABEL_COUNT);
long count = countField != null ? countField : 0;
result.setTotalHitCount(count);
return result;
}
use of com.yahoo.search.grouping.result.GroupList in project vespa by vespa-engine.
the class FieldCollapsingSearcherTestCase method testFieldCollapsingWithGrouping.
@Test
public void testFieldCollapsingWithGrouping() {
// Set up
FieldCollapsingSearcher collapse = new FieldCollapsingSearcher("other");
DocumentSourceSearcher docsource = new DocumentSourceSearcher();
Chain<Searcher> chain = new Chain<>(collapse, new AddAggregationStyleGroupingResultSearcher(), docsource);
// Caveat: Collapse is set to false, because that's what the
// collapser asks for
Query q = new Query("?query=test_collapse&collapsefield=amid");
// The searcher turns off collapsing further on in the chain
q.properties().set("collapse", "0");
Result r = new Result(q);
r.hits().add(createHit("http://acme.org/a.html", 10, 0));
r.hits().add(createHit("http://acme.org/b.html", 9, 0));
r.hits().add(createHit("http://acme.org/c.html", 9, 1));
r.hits().add(createHit("http://acme.org/d.html", 8, 1));
r.hits().add(createHit("http://acme.org/e.html", 8, 2));
r.hits().add(createHit("http://acme.org/f.html", 7, 2));
r.hits().add(createHit("http://acme.org/g.html", 7, 3));
r.hits().add(createHit("http://acme.org/h.html", 6, 3));
r.setTotalHitCount(8);
docsource.addResult(q, r);
// Test basic collapsing on mid
Query query = new Query("?query=test_collapse&collapsefield=amid");
Result result = new Execution(chain, Execution.Context.createContextStub()).search(query);
// Assert that the regular hits are collapsed
assertEquals(4 + 1, result.getHitCount());
assertEquals(1, docsource.getQueryCount());
assertHit("http://acme.org/a.html", 10, 0, result.hits().get(0));
assertHit("http://acme.org/c.html", 9, 1, result.hits().get(1));
assertHit("http://acme.org/e.html", 8, 2, result.hits().get(2));
assertHit("http://acme.org/g.html", 7, 3, result.hits().get(3));
// Assert that the aggregation group hierarchy is left intact
HitGroup root = getFirstGroupIn(result.hits());
assertNotNull(root);
// The id ends by a global counter currently
assertEquals("group:root:", root.getId().stringValue().substring(0, 11));
assertEquals(1, root.size());
HitGroup groupList = (GroupList) root.get("grouplist:g1");
assertNotNull(groupList);
assertEquals(1, groupList.size());
HitGroup group = (HitGroup) groupList.get("group:long:37");
assertNotNull(group);
}
use of com.yahoo.search.grouping.result.GroupList in project vespa by vespa-engine.
the class UniqueGroupingSearcherTestCase method testGroupingBasedDedupWithEmptyGroupingHitsList.
@Test
public void testGroupingBasedDedupWithEmptyGroupingHitsList() throws Exception {
Result result = search("?query=foo&unique=fingerprint", new MockResultProvider(0, true).addGroupList(new GroupList("fingerprint")));
assertEquals(0, result.hits().size());
assertEquals(0, result.getTotalHitCount());
}
use of com.yahoo.search.grouping.result.GroupList in project vespa by vespa-engine.
the class UniqueGroupingSearcherTestCase method testGroupingBasedDedupWithGroupingHits.
@Test
public void testGroupingBasedDedupWithGroupingHits() throws Exception {
GroupList fingerprint = new GroupList("fingerprint");
fingerprint.add(makeHitGroup("1"));
fingerprint.add(makeHitGroup("2"));
fingerprint.add(makeHitGroup("3"));
fingerprint.add(makeHitGroup("4"));
fingerprint.add(makeHitGroup("5"));
fingerprint.add(makeHitGroup("6"));
fingerprint.add(makeHitGroup("7"));
MockResultProvider mockResultProvider = new MockResultProvider(15, true);
mockResultProvider.addGroupList(fingerprint);
mockResultProvider.resultGroup.setField(UniqueGroupingSearcher.LABEL_COUNT, 42l);
Result result = search("?query=foo&unique=fingerprint&hits=5&offset=1", mockResultProvider);
assertEquals(5, result.hits().size());
assertEquals("2", result.hits().get(0).getId().toString());
assertEquals("3", result.hits().get(1).getId().toString());
assertEquals("4", result.hits().get(2).getId().toString());
assertEquals("5", result.hits().get(3).getId().toString());
assertEquals("6", result.hits().get(4).getId().toString());
assertEquals(42, result.getTotalHitCount());
}
use of com.yahoo.search.grouping.result.GroupList in project vespa by vespa-engine.
the class UniqueGroupingSearcherTestCase method testGroupingBasedDedupWithGroupingHitsAndSorting.
@Test
public void testGroupingBasedDedupWithGroupingHitsAndSorting() throws Exception {
GroupList fingerprint = new GroupList("fingerprint");
fingerprint.add(makeSortingHitGroup("1"));
fingerprint.add(makeSortingHitGroup("2"));
fingerprint.add(makeSortingHitGroup("3"));
fingerprint.add(makeSortingHitGroup("4"));
fingerprint.add(makeSortingHitGroup("5"));
fingerprint.add(makeSortingHitGroup("6"));
fingerprint.add(makeSortingHitGroup("7"));
MockResultProvider mockResultProvider = new MockResultProvider(100, true);
mockResultProvider.addGroupList(fingerprint);
mockResultProvider.resultGroup.setField(UniqueGroupingSearcher.LABEL_COUNT, 1337l);
Result result = search("?query=foo&unique=fingerprint&hits=5&offset=1&sorting=-expdate", mockResultProvider);
assertEquals(5, result.hits().size());
assertEquals("2", result.hits().get(0).getId().toString());
assertEquals("3", result.hits().get(1).getId().toString());
assertEquals("4", result.hits().get(2).getId().toString());
assertEquals("5", result.hits().get(3).getId().toString());
assertEquals("6", result.hits().get(4).getId().toString());
assertEquals(1337, result.getTotalHitCount());
}
Aggregations