RankedNonZeroLengthDocumentModel 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); LengthsIterator documentLengths = retrieval.getDocumentLengthsIterator(); while (!iterator.isDone()) { long document = iterator.currentCandidate(); context.document = document; documentLengths.syncTo(document); int length = documentLengths.length(context); iterator.syncTo(document); if (iterator.hasMatch(context)) { double score = iterator.score(context); if (length > 0 && (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); } RankedNonZeroLengthDocumentModel(LocalRetrieval lr); @Override ScoredDocument[] execute(Node queryTree, Parameters queryParams);  }
@Test public void test() 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(#inside( #text:@\\1917\\() #field:dummy() ))"); query = ret.transformQuery(query, queryParams); RankedDocumentModel oldModel = new RankedDocumentModel(ret); ScoredDocument[] results = oldModel.execute(query, queryParams); assertEquals(results.length, 10); RankedNonZeroLengthDocumentModel newModel = new RankedNonZeroLengthDocumentModel(ret); results = newModel.execute(query, queryParams); assertEquals(results.length, 5); Set<String> docNames = new HashSet<String>(5); for (int i = 0; i < 5; i++) { String name = ret.getDocumentName((long)results[i].document); docNames.add(name); Document doc = ret.getDocument(name, Document.DocumentComponents.All ); assertTrue(doc.text.length() > 0); } assertTrue(docNames.containsAll(Arrays.asList("doc-1", "doc-3", "doc-5", "doc-7", "doc-9") )); }