Java Code Examples for org.apache.lucene.index.LeafReader#postings()
The following examples show how to use
org.apache.lucene.index.LeafReader#postings() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LuceneIndex.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 6 votes |
private static Document getDocument(LeafReader reader, Term term) throws IOException { PostingsEnum docs = reader.postings(term); if (docs != null) { int docId = docs.nextDoc(); // PostingsEnum may contain deleted documents, we have to cope for it while (docId != PostingsEnum.NO_MORE_DOCS) { // if document is deleted, skip and continue Bits liveDocs = reader.getLiveDocs(); if (liveDocs != null && !liveDocs.get(docId)) { docId = docs.nextDoc(); continue; } if (docs.nextDoc() != PostingsEnum.NO_MORE_DOCS) { throw new IllegalStateException("Multiple Documents for term " + term.text()); } return readDocument(reader, docId, null); } } return null; }
Example 2
Source File: LuceneIndex.java From rdf4j with BSD 3-Clause "New" or "Revised" License | 5 votes |
private static void addDocuments(LeafReader reader, Term term, Collection<Document> documents) throws IOException { PostingsEnum docs = reader.postings(term); if (docs != null) { int docId; while ((docId = docs.nextDoc()) != PostingsEnum.NO_MORE_DOCS) { Bits liveDocs = reader.getLiveDocs(); // Maybe some of the docs have been deleted! Check that too.. if (liveDocs != null && !liveDocs.get(docId)) { continue; } Document document = readDocument(reader, docId, null); documents.add(document); } } }
Example 3
Source File: TestPositionIncrement.java From lucene-solr with Apache License 2.0 | 4 votes |
public void testPayloadsPos0() throws Exception { Directory dir = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), dir, new MockPayloadAnalyzer()); Document doc = new Document(); doc.add(new TextField("content", new StringReader( "a a b c d e a f g h i j a b k k"))); writer.addDocument(doc); final IndexReader readerFromWriter = writer.getReader(); LeafReader r = getOnlyLeafReader(readerFromWriter); PostingsEnum tp = r.postings(new Term("content", "a"), PostingsEnum.ALL); int count = 0; assertTrue(tp.nextDoc() != DocIdSetIterator.NO_MORE_DOCS); // "a" occurs 4 times assertEquals(4, tp.freq()); assertEquals(0, tp.nextPosition()); assertEquals(1, tp.nextPosition()); assertEquals(3, tp.nextPosition()); assertEquals(6, tp.nextPosition()); // only one doc has "a" assertEquals(DocIdSetIterator.NO_MORE_DOCS, tp.nextDoc()); IndexSearcher is = newSearcher(getOnlyLeafReader(readerFromWriter)); SpanTermQuery stq1 = new SpanTermQuery(new Term("content", "a")); SpanTermQuery stq2 = new SpanTermQuery(new Term("content", "k")); SpanQuery[] sqs = { stq1, stq2 }; SpanNearQuery snq = new SpanNearQuery(sqs, 30, false); count = 0; boolean sawZero = false; if (VERBOSE) { System.out.println("\ngetPayloadSpans test"); } PayloadSpanCollector collector = new PayloadSpanCollector(); Spans pspans = snq.createWeight(is, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(is.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS); while (pspans.nextDoc() != Spans.NO_MORE_DOCS) { while (pspans.nextStartPosition() != Spans.NO_MORE_POSITIONS) { if (VERBOSE) { System.out.println("doc " + pspans.docID() + ": span " + pspans.startPosition() + " to " + pspans.endPosition()); } collector.reset(); pspans.collect(collector); sawZero |= pspans.startPosition() == 0; for (BytesRef payload : collector.payloads) { count++; if (VERBOSE) { System.out.println(" payload: " + Term.toString(payload)); } } } } assertTrue(sawZero); assertEquals(8, count); // System.out.println("\ngetSpans test"); Spans spans = snq.createWeight(is, ScoreMode.COMPLETE_NO_SCORES, 1f).getSpans(is.getIndexReader().leaves().get(0), SpanWeight.Postings.POSITIONS); count = 0; sawZero = false; while (spans.nextDoc() != Spans.NO_MORE_DOCS) { while (spans.nextStartPosition() != Spans.NO_MORE_POSITIONS) { count++; sawZero |= spans.startPosition() == 0; // System.out.println(spans.doc() + " - " + spans.start() + " - " + // spans.end()); } } assertEquals(4, count); assertTrue(sawZero); writer.close(); is.getIndexReader().close(); dir.close(); }