RankedDocumentModel extends ProcessingModel { @Override public ScoredDocument[] execute(Node queryTree, Parameters queryParams) throws Exception { ScoringContext context = new ScoringContext(); int requested = queryParams.get("requested", 1000); boolean annotate = queryParams.get("annotate", false); FixedSizeMinHeap<ScoredDocument> queue = new FixedSizeMinHeap<>(ScoredDocument.class, requested, new ScoredDocument.ScoredDocumentComparator()); ScoreIterator iterator = (ScoreIterator) retrieval.createIterator(queryParams, queryTree); while (!iterator.isDone()) { long document = iterator.currentCandidate(); context.document = document; iterator.syncTo(document); if (iterator.hasMatch(context)) { double score = iterator.score(context); if (queue.size() < requested || queue.peek().score < score) { ScoredDocument scoredDocument = new ScoredDocument(document, score); if (annotate) { scoredDocument.annotation = iterator.getAnnotatedNode(context); } queue.offer(scoredDocument); } } iterator.movePast(document); } return toReversedArray(queue); } RankedDocumentModel(LocalRetrieval lr); @Override ScoredDocument[] execute(Node queryTree, Parameters queryParams);  }
@Test public void testEntireCollection() throws Exception { Parameters globals = Parameters.create(); LocalRetrieval ret = new LocalRetrieval(index.getAbsolutePath(), globals); Parameters queryParams = Parameters.create(); queryParams.set("requested", 10); Node query = StructuredQuery.parse("#combine( test text 0 1 2 3 4 )"); query = ret.transformQuery(query, queryParams); RankedDocumentModel model = new RankedDocumentModel(ret); ScoredDocument[] results = model.execute(query, queryParams); assertEquals(results.length, 10); for (int i = 0; i < 10; i++) { assertEquals(results[i].document, i); assertEquals(results[i].rank, i + 1); if (i > 0) { assert (CmpUtil.compare(results[i].score, results[i - 1].score) <= 0); } } query = StructuredQuery.parse("#combine( test text 99 )"); query = ret.transformQuery(query, queryParams); results = model.execute(query, queryParams); assertEquals(results.length, 10); for (int i = 0; i < 10; i++) { assertEquals(results[i].document, i + 90); assertEquals(results[i].rank, i + 1); if (i > 0) { assert (CmpUtil.compare(results[i].score, results[i - 1].score) <= 0); } } }
@Test public void testWhiteList() throws Exception { Parameters globals = Parameters.create(); LocalRetrieval ret = new LocalRetrieval(index.getAbsolutePath(), globals); Parameters queryParams = Parameters.create(); queryParams.set("requested", 10); Node query = StructuredQuery.parse("#combine( test text 0 1 2 3 4 )"); query = ret.transformQuery(query, queryParams); WorkingSetDocumentModel model = new WorkingSetDocumentModel(ret); queryParams.set("working", Arrays.asList(new Long[]{0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 11L, 12L, 13L, 14L})); ScoredDocument[] results = model.execute(query, queryParams); assertEquals(results.length, 10); for (int i = 0; i < 9; i++) { assertEquals(results[i].document, i); assertEquals(results[i].rank, i + 1); if (i > 0) { assert (CmpUtil.compare(results[i].score, results[i - 1].score) <= 0); } } assertEquals(results[9].document, 11); assertEquals(results[9].rank, 10); assert (CmpUtil.compare(results[9].score, results[8].score) <= 0); query = StructuredQuery.parse("#combine( test text 90 )"); query = ret.transformQuery(query, queryParams); queryParams.set("working", Arrays.asList(new Long[]{0L, 1L, 2L, 3L, 4L, 5L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L})); results = model.execute(query, queryParams); assertEquals(results.length, 10); for (int i = 0; i < 10; i++) { assertEquals(results[i].document, i + 90); assertEquals(results[i].rank, i + 1); if (i > 0) { assert (CmpUtil.compare(results[i].score, results[i - 1].score) <= 0); } } queryParams.set("working", Arrays.asList(new String[]{"d-0", "d-1", "d-2", "d-3", "d-4", "d-5", "d-90", "d-91", "d-92", "d-93", "d-94", "d-95", "d-96", "d-97", "d-98", "d-99"})); results = model.execute(query, queryParams); assertEquals(results.length, 10); for (int i = 0; i < 10; i++) { assertEquals(results[i].document, i + 90); assertEquals(results[i].rank, i + 1); if (i > 0) { assert (CmpUtil.compare(results[i].score, results[i - 1].score) <= 0); } } }