Example usage for org.apache.lucene.search DocIdSetIterator NO_MORE_DOCS

List of usage examples for org.apache.lucene.search DocIdSetIterator NO_MORE_DOCS

Introduction

In this page you can find the example usage for org.apache.lucene.search DocIdSetIterator NO_MORE_DOCS.

Prototype

int NO_MORE_DOCS

To view the source code for org.apache.lucene.search DocIdSetIterator NO_MORE_DOCS.

Click Source Link

Document

When returned by #nextDoc() , #advance(int) and #docID() it means there are no more docs in the iterator.

Usage

From source file:com.sindicetech.siren.search.node.TestLuceneProxyNodeScorer.java

License:Open Source License

@Test
public void testAdvanceInfiniteLoop() throws Exception {
    this.addDocuments("{ \"baba\" : \"bibi ccc\" , \"ccc\" : \"bbb ccc\" }",
            "{ \"baba bibi baba bibi\" : \"aaa bbb ddd\" }", "{ \"baba bibi\" : \"aaa bbb ddd\" }");

    final Scorer scorer1 = this.getScorer(nbq(must("baba", "bibi")).getLuceneProxyQuery());

    assertTrue(scorer1.advance(0) != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals(1, scorer1.docID());/*w w w .  jav a2 s .co m*/
    assertEquals(2, scorer1.freq(), 0);
    final float score1 = scorer1.score();
    assertTrue(scorer1.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals(2, scorer1.docID());
    assertEquals(2, scorer1.freq(), 0);
    final float score2 = scorer1.score();
    assertTrue(score1 > score2);
    assertTrue(scorer1.nextDoc() == DocIdSetIterator.NO_MORE_DOCS);
}

From source file:com.teaspoonconsulting.solracls.SolrACLQueryComponent.java

License:Apache License

private ConstantScoreQuery buildFilterForPrincipals(final String[] principals) {
    Filter f = new Filter() {
        public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs) {
            long start = System.currentTimeMillis();

            AtomicReader rdr = context.reader();
            OpenBitSet bits = new OpenBitSet(rdr.maxDoc());

            for (String principal : principals) {
                try {
                    DocsEnum td = rdr.termDocsEnum(new Term(principalsField, principal));

                    if (td == null) {
                        continue;
                    }/*w w w .  j av a  2 s .  c o  m*/

                    while (td.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
                        if (acceptDocs == null || acceptDocs.get(td.docID())) {
                            bits.set(td.docID());
                        }
                    }

                } catch (IOException e) {
                    return null;
                }
            }

            LOGGER.info("Building {}-bit filter for segment [{}]" + rdr + "] took {} milliseconds",
                    new Object[] { rdr.maxDoc(), rdr, (System.currentTimeMillis() - start) });

            return bits;
        }
    };

    return new ConstantScoreQuery(new CachingWrapperFilter(f));
}

From source file:com.xiaomi.linden.lucene.query.flexiblequery.TermDocsEnum.java

License:Apache License

public boolean next() throws IOException {
    if (postings == null) {
        doc = DocIdSetIterator.NO_MORE_DOCS;
        return false;
    }/*  ww w.j a v  a 2s .  c o m*/
    doc = postings.nextDoc();
    if (doc == DocIdSetIterator.NO_MORE_DOCS) {
        return false;
    }
    freq = postings.freq();
    if (freq > initPositionSize) {
        int newSize = 2 * freq;
        positions = new int[newSize];
        matchedPositions = new int[newSize];
        initPositionSize = newSize;
    }
    for (int i = 0; i < freq; i++) {
        positions[i] = postings.nextPosition();
    }
    position = positions[0];
    return true;
}

From source file:de.blizzy.documentr.search.PageIndex.java

License:Open Source License

public Set<String> getAllTags(Authentication authentication) throws IOException, TimeoutException {
    IndexReader reader = null;//w  w w .ja va  2  s .  c  o  m
    IndexSearcher searcher = null;
    try {
        searcher = searcherManager.acquire();
        Bits visibleDocs = getVisibleDocIds(searcher, authentication);
        Set<String> tags = Sets.newHashSet();
        if (visibleDocs.length() > 0) {
            reader = searcher.getIndexReader();
            Terms terms = MultiFields.getTerms(reader, TAG);
            if (terms != null) {
                TermsEnum termsEnum = terms.iterator(null);
                BytesRef ref;
                while ((ref = termsEnum.next()) != null) {
                    DocsEnum docsEnum = termsEnum.docs(visibleDocs, null, 0);
                    if (docsEnum.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
                        tags.add(ref.utf8ToString());
                    }
                }
            }
        }
        return tags;
    } finally {
        if (searcher != null) {
            searcherManager.release(searcher);
        }
    }
}

From source file:de.blizzy.documentr.search.TagFinder.java

License:Open Source License

public Set<String> getAllTags(Authentication authentication) throws IOException, TimeoutException {
    IndexReader reader = null;//w ww  . j a v a  2  s  .  com
    IndexSearcher searcher = null;
    try {
        searcher = searcherManager.acquire();

        // no point in running the task asynchronously here
        GetVisibleDocIdsTask visibleDocIdsTask = new GetVisibleDocIdsTask(searcher, authentication, userStore,
                permissionEvaluator, taskExecutor);
        Bits visibleDocIds = visibleDocIdsTask.call();

        Set<String> tags = Sets.newHashSet();
        if (visibleDocIds.length() > 0) {
            reader = searcher.getIndexReader();
            Terms terms = MultiFields.getTerms(reader, PageIndex.TAG);
            if (terms != null) {
                TermsEnum termsEnum = terms.iterator(null);
                BytesRef ref;
                while ((ref = termsEnum.next()) != null) {
                    DocsEnum docsEnum = termsEnum.docs(visibleDocIds, null, 0);
                    if (docsEnum.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
                        tags.add(ref.utf8ToString());
                    }
                }
            }
        }
        return tags;
    } finally {
        if (searcher != null) {
            searcherManager.release(searcher);
        }
    }
}

From source file:de.jetsli.lumeo.util.TermFilter.java

License:Apache License

@Override
public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs) throws IOException {
    AtomicReader reader = context.reader();
    FixedBitSet result = new FixedBitSet(reader.maxDoc());
    DocsEnum de = reader.termDocsEnum(acceptDocs, fieldName, bytes, false);
    if (de == null)
        return result;

    int id;/*from   w ww .j ava  2s.  co  m*/
    while ((id = de.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
        result.set(id);
    }
    return result;
}

From source file:de.mirkosertic.desktopsearch.SearchPhraseSuggester.java

License:Open Source License

public List<Suggestion> suggestSearchPhrase(String aFieldName, String aPhrase) throws IOException {

    LOGGER.info("Trying to find suggestions for phrase " + aPhrase);

    long theStartTime = System.currentTimeMillis();
    try {/*from ww  w .  ja  v a 2 s .c  o  m*/
        List<String> theTokens = toTokens(aFieldName, aPhrase);

        List<SpanQuery> theSpanQueries = theTokens.stream().map(s -> {
            if (QueryUtils.isWildCard(s)) {
                WildcardQuery theWildcardQuery = new WildcardQuery(new Term(aFieldName, s));
                SpanMultiTermQueryWrapper theWrapper = new SpanMultiTermQueryWrapper(theWildcardQuery);
                try {
                    return theWrapper.getRewriteMethod().rewrite(indexReader, theWildcardQuery);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return new SpanTermQuery(new Term(aFieldName, s));
        }).collect(Collectors.toList());

        SpanQuery theSpanQuery = new SpanNearQuery(theSpanQueries.toArray(new SpanQuery[theSpanQueries.size()]),
                configuration.getSuggestionSlop(), configuration.isSuggestionInOrder());

        LOGGER.info("created span query " + theSpanQuery);

        LeafReader theAtomicReader = SlowCompositeReaderWrapper.wrap(indexReader);

        Map<Term, TermContext> theTermContexts = new HashMap<>();
        Map<String, Long> theSpanFrequencies = new HashMap<>();

        // These are all the matching spans over all documents
        Spans theMatchingSpans = theSpanQuery.getSpans(theAtomicReader.getContext(),
                new Bits.MatchAllBits(indexReader.numDocs()), theTermContexts);

        while (theMatchingSpans.next()) {

            // This maps the position of a term and the term string itself
            // the positions must be in order, so we have to use a treemap.
            Map<Integer, String> theEntries = new TreeMap<>();

            Terms theAllTermsFromDocument = indexReader.getTermVector(theMatchingSpans.doc(),
                    IndexFields.CONTENT_NOT_STEMMED);
            int theSpanStart = theMatchingSpans.start() - configuration.getSuggestionWindowBefore();
            int theSpanEnd = theMatchingSpans.end() + configuration.getSuggestionWindowAfter();
            TermsEnum theTermsEnum = theAllTermsFromDocument.iterator(null);
            BytesRef theTerm;
            while ((theTerm = theTermsEnum.next()) != null) {
                DocsAndPositionsEnum thePositionEnum = theTermsEnum.docsAndPositions(null, null);
                if (thePositionEnum.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
                    int i = 0;
                    int position;
                    while (i < thePositionEnum.freq() && (position = thePositionEnum.nextPosition()) != -1) {
                        if (position >= theSpanStart && position <= theSpanEnd) {
                            theEntries.put(position, theTerm.utf8ToString());
                        }
                        i++;
                    }
                }
            }

            StringBuilder theResultString = new StringBuilder();
            theEntries.entrySet().forEach(e -> {
                if (theResultString.length() > 0) {
                    theResultString.append(" ");
                }
                theResultString.append(e.getValue());
            });

            String theTotalSpan = theResultString.toString().trim();

            Long theFrequency = theSpanFrequencies.get(theTotalSpan);
            if (theFrequency == null) {
                theSpanFrequencies.put(theTotalSpan, 1L);
            } else {
                theSpanFrequencies.put(theTotalSpan, theFrequency + 1);
            }
        }

        return theSpanFrequencies.entrySet().stream().filter(t -> t.getValue() > 1)
                .sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue()))
                .limit(configuration.getNumberOfSuggestions())
                .map(T -> new Suggestion(highlight(T.getKey(), theTokens), T.getKey()))
                .collect(Collectors.toList());
    } finally {
        long theDuration = System.currentTimeMillis() - theStartTime;
        LOGGER.info("Took " + theDuration + "ms");
    }
}

From source file:de.unihildesheim.iw.lucene.document.FeedbackQueryTest.java

License:Open Source License

private static int getNumDocsFromSet(final DocIdSet set) throws IOException {
    final DocIdSetIterator disi = set.iterator();
    int count = 0;
    if (disi != null) {
        while (disi.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
            count++;// w w  w  . j  a va 2  s . c o m
        }
    }
    return count;
}

From source file:de.unihildesheim.iw.lucene.search.EmptyFieldFilter.java

License:Open Source License

@Override
public DocIdSet getDocIdSet(@NotNull final LeafReaderContext context, @Nullable final Bits acceptDocs)
        throws IOException {
    FixedBitSet checkBits;//from  w w  w .j  a v  a  2s  . co m
    final LeafReader reader = context.reader();
    final int maxDoc = reader.maxDoc();

    BitSet finalBits = new SparseFixedBitSet(maxDoc);
    if (acceptDocs == null) {
        checkBits = BitsUtils.bits2FixedBitSet(reader.getLiveDocs());
        if (checkBits == null) {
            // all live
            checkBits = new FixedBitSet(maxDoc);
            checkBits.set(0, checkBits.length());
        }
    } else {
        checkBits = BitsUtils.bits2FixedBitSet(acceptDocs);
    }

    @Nullable
    final Terms terms = reader.terms(this.field);
    if (terms != null) {
        final int termsDocCount = terms.getDocCount();

        if (termsDocCount != 0) {
            if (termsDocCount == maxDoc) {
                // all matching
                finalBits = checkBits;
            } else {
                @Nullable
                final Terms t = reader.terms(this.field);
                if (t != null) {
                    PostingsEnum pe = null;
                    final TermsEnum te = t.iterator(null);
                    int docId;
                    while (te.next() != null) {
                        pe = te.postings(checkBits, pe, (int) PostingsEnum.NONE);
                        while ((docId = pe.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
                            if (checkBits.getAndClear(docId)) {
                                finalBits.set(docId);
                            }
                        }
                    }
                }
            }
        }
    }
    return new BitDocIdSet(finalBits);
}

From source file:de.unihildesheim.iw.lucene.search.IPCFieldFilter.java

License:Open Source License

@Override
public DocIdSet getDocIdSet(@NotNull final LeafReaderContext context, @Nullable final Bits acceptDocs)
        throws IOException {
    final LeafReader reader = context.reader();
    final int maxDoc = reader.maxDoc();
    final BitSet finalBits = new SparseFixedBitSet(maxDoc);

    if (acceptDocs == null) {
        // check all
        for (int i = 0; i < maxDoc; i++) {
            if (this.filterFunc.isAccepted(reader, i, this.ipcParser)) {
                finalBits.set(i);/*from  w w  w.j a va 2  s .  c  o m*/
            }
        }
    } else {
        final BitSet checkBits = BitsUtils.bits2BitSet(acceptDocs);
        final DocIdSetIterator disi = new BitDocIdSet(checkBits).iterator();
        int docId;
        while ((docId = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
            if (this.filterFunc.isAccepted(reader, docId, this.ipcParser)) {
                finalBits.set(docId);
            }
        }
    }

    return new BitDocIdSet(finalBits);
}