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

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

Introduction

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

Prototype

public abstract int nextDoc() throws IOException;

Source Link

Document

Advances to the next document in the set and returns the doc it is currently on, or #NO_MORE_DOCS if there are no more docs in the set.
NOTE: after the iterator has exhausted you should not call this method, as it may result in unpredicted behavior.

Usage

From source file:org.apache.solr.request.IntervalFacets.java

License:Apache License

private void accumIntervalsMulti(SortedSetDocValues ssdv, DocIdSetIterator disi, Bits bits) throws IOException {
    // First update the ordinals in the intervals for this segment
    for (FacetInterval interval : intervals) {
        interval.updateContext(ssdv);/* w w w .j  a v  a 2 s.  c  o  m*/
    }

    int doc;
    while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
        if (bits != null && bits.get(doc) == false) {
            continue;
        }
        ssdv.setDocument(doc);
        long currOrd;
        int currentInterval = 0;
        while ((currOrd = ssdv.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS) {
            boolean evaluateNextInterval = true;
            while (evaluateNextInterval && currentInterval < intervals.length) {
                IntervalCompareResult result = intervals[currentInterval].includes(currOrd);
                switch (result) {
                case INCLUDED:
                    /*
                     * Increment the current interval and move to the next one using
                     * the same value
                     */
                    intervals[currentInterval].incCount();
                    currentInterval++;
                    break;
                case LOWER_THAN_START:
                    /*
                     * None of the next intervals will match this value (all of them have 
                     * higher start value). Move to the next value for this document. 
                     */
                    evaluateNextInterval = false;
                    break;
                case GREATER_THAN_END:
                    /*
                     * Next interval may match this value
                     */
                    currentInterval++;
                    break;
                }
            }
        }
    }
}

From source file:org.apache.solr.request.IntervalFacets.java

License:Apache License

private void accumIntervalsSingle(SortedDocValues sdv, DocIdSetIterator disi, Bits bits) throws IOException {
    // First update the ordinals in the intervals to this segment
    for (FacetInterval interval : intervals) {
        interval.updateContext(sdv);/*  w  w w .jav a2 s .  c om*/
    }
    int doc;
    while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
        if (bits != null && bits.get(doc) == false) {
            continue;
        }
        int ord = sdv.getOrd(doc);
        if (ord >= 0) {
            accumInterval(ord);
        }
    }
}

From source file:org.apache.solr.response.SortingResponseWriter.java

License:Apache License

public void write(Writer writer, SolrQueryRequest req, SolrQueryResponse res) throws IOException {
    Exception e1 = res.getException();
    if (e1 != null) {
        e1.printStackTrace(new PrintWriter(writer));
        return;/*from   w w  w .  ja va2  s .com*/
    }
    SolrRequestInfo info = SolrRequestInfo.getRequestInfo();
    SortSpec sortSpec = info.getResponseBuilder().getSortSpec();

    if (sortSpec == null) {
        throw new IOException(new SyntaxError("No sort criteria was provided."));
    }

    SolrIndexSearcher searcher = req.getSearcher();
    Sort sort = searcher.weightSort(sortSpec.getSort());

    if (sort == null) {
        throw new IOException(new SyntaxError("No sort criteria was provided."));
    }

    if (sort.needsScores()) {
        throw new IOException(new SyntaxError("Scoring is not currently supported with xsort."));
    }

    FixedBitSet[] sets = (FixedBitSet[]) req.getContext().get("export");
    Integer th = (Integer) req.getContext().get("totalHits");

    if (sets == null) {
        throw new IOException(new SyntaxError("xport RankQuery is required for xsort: rq={!xport}"));
    }

    int totalHits = th.intValue();
    SolrParams params = req.getParams();
    String fl = params.get("fl");

    if (fl == null) {
        throw new IOException(new SyntaxError("export field list (fl) must be specified."));
    }

    String[] fields = fl.split(",");

    for (int i = 0; i < fields.length; i++) {
        if (fl.trim().equals("score")) {
            throw new IOException(new SyntaxError("Scoring is not currently supported with xsort."));
        }
    }

    FieldWriter[] fieldWriters = getFieldWriters(fields, req.getSearcher());
    writer.write(
            "{\"responseHeader\": {\"status\": 0}, \"response\":{\"numFound\":" + totalHits + ", \"docs\":[");

    //Write the data.
    List<AtomicReaderContext> leaves = req.getSearcher().getTopReaderContext().leaves();
    SortDoc sortDoc = getSortDoc(req.getSearcher(), sort.getSort());
    int count = 0;
    int queueSize = 30000;
    SortQueue queue = new SortQueue(queueSize, sortDoc);
    SortDoc[] outDocs = new SortDoc[queueSize];

    boolean commaNeeded = false;
    while (count < totalHits) {
        //long begin = System.nanoTime();
        queue.reset();
        SortDoc top = queue.top();
        for (int i = 0; i < leaves.size(); i++) {
            sortDoc.setNextReader(leaves.get(i));
            DocIdSetIterator it = sets[i].iterator();
            int docId = -1;
            while ((docId = it.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
                sortDoc.setValues(docId);
                if (top.lessThan(sortDoc)) {
                    top.setValues(sortDoc);
                    top = queue.updateTop();
                }
            }
        }

        int outDocsIndex = -1;

        for (int i = 0; i < queueSize; i++) {
            SortDoc s = queue.pop();
            if (s.docId > -1) {
                outDocs[++outDocsIndex] = s;
            }
        }

        //long end = System.nanoTime();

        count += (outDocsIndex + 1);

        try {
            for (int i = outDocsIndex; i >= 0; --i) {
                SortDoc s = outDocs[i];
                if (commaNeeded) {
                    writer.write(',');
                }
                writer.write('{');
                writeDoc(s, leaves, fieldWriters, sets, writer);
                writer.write('}');
                commaNeeded = true;
                s.reset();
            }
        } catch (Throwable e) {
            Throwable ex = e;
            while (ex != null) {
                String m = ex.getMessage();
                if (m != null && m.contains("Broken pipe")) {
                    logger.info("Early client disconnect during export");
                    return;
                }
                ex = ex.getCause();
            }

            if (e instanceof IOException) {
                throw ((IOException) e);
            } else {
                throw new IOException(e);
            }
        }
    }

    //System.out.println("Sort Time 2:"+Long.toString(total/1000000));
    writer.write("]}}");
    writer.flush();
}

From source file:org.apache.solr.search.DocSetBuilder.java

License:Apache License

public void add(DocIdSetIterator iter, int base) throws IOException {
    grow((int) Math.min(Integer.MAX_VALUE, iter.cost()));

    if (bitSet != null) {
        add(bitSet, iter, base);// ww w  .ja va2s .com
    } else {
        while (true) {
            for (int i = pos; i < buffer.length; ++i) {
                final int doc = iter.nextDoc();
                if (doc == DocIdSetIterator.NO_MORE_DOCS) {
                    pos = i; // update pos
                    return;
                }
                buffer[i] = doc + base; // using the loop counter may help with removal of bounds checking
            }

            pos = buffer.length; // update pos
            if (pos + 1 >= threshold) {
                break;
            }

            growBuffer(pos + 1);
        }

        upgradeToBitSet();
        add(bitSet, iter, base);
    }
}

From source file:org.apache.solr.search.DocSetBuilder.java

License:Apache License

public static void add(FixedBitSet bitSet, DocIdSetIterator iter, int base) throws IOException {
    for (int doc = iter.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = iter.nextDoc()) {
        bitSet.set(doc + base);/*  w  w  w  . j a v  a 2  s.  c  o  m*/
    }
}

From source file:org.apache.solr.search.facet.FacetFieldProcessorByArrayDV.java

License:Apache License

private void collectPerSeg(SortedDocValues singleDv, DocIdSetIterator disi, LongValues toGlobal)
        throws IOException {
    int segMax = singleDv.getValueCount() + 1;
    final int[] counts = getCountArr(segMax);

    /** alternate trial implementations
     // ord//from   w  w  w .ja v a2 s .  c  om
     // FieldUtil.visitOrds(singleDv, disi,  (doc,ord)->{counts[ord+1]++;} );
            
    FieldUtil.OrdValues ordValues = FieldUtil.getOrdValues(singleDv, disi);
    while (ordValues.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
      counts[ ordValues.getOrd() + 1]++;
    }
     **/

    // calculate segment-local counts
    int doc;
    if (singleDv instanceof FieldCacheImpl.SortedDocValuesImpl.Iter) {
        FieldCacheImpl.SortedDocValuesImpl.Iter fc = (FieldCacheImpl.SortedDocValuesImpl.Iter) singleDv;
        while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
            counts[fc.getOrd(doc) + 1]++;
        }
    } else {
        while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
            if (singleDv.advanceExact(doc)) {
                counts[singleDv.ordValue() + 1]++;
            }
        }
    }

    // convert segment-local counts to global counts
    for (int i = 1; i < segMax; i++) {
        int segCount = counts[i];
        if (segCount > 0) {
            int slot = toGlobal == null ? (i - 1) : (int) toGlobal.get(i - 1);
            countAcc.incrementCount(slot, segCount);
        }
    }
}

From source file:org.apache.solr.search.facet.FacetFieldProcessorByArrayDV.java

License:Apache License

private void collectPerSeg(SortedSetDocValues multiDv, DocIdSetIterator disi, LongValues toGlobal)
        throws IOException {
    int segMax = (int) multiDv.getValueCount();
    final int[] counts = getCountArr(segMax);

    int doc;/*from   w ww  .j  a  v  a  2s  .c o m*/
    while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
        if (multiDv.advanceExact(doc)) {
            for (;;) {
                int segOrd = (int) multiDv.nextOrd();
                if (segOrd < 0)
                    break;
                counts[segOrd]++;
            }
        }
    }

    for (int i = 0; i < segMax; i++) {
        int segCount = counts[i];
        if (segCount > 0) {
            int slot = toGlobal == null ? (i) : (int) toGlobal.get(i);
            countAcc.incrementCount(slot, segCount);
        }
    }
}

From source file:org.apache.solr.search.facet.FacetFieldProcessorByArrayDV.java

License:Apache License

private void collectDocs(SortedDocValues singleDv, DocIdSetIterator disi, LongValues toGlobal)
        throws IOException {
    int doc;//from  w w  w .  ja va  2  s .c  o  m
    while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
        if (singleDv.advanceExact(doc)) {
            int segOrd = singleDv.ordValue();
            collect(doc, segOrd, toGlobal);
        }
    }
}

From source file:org.apache.solr.search.facet.FacetFieldProcessorByArrayDV.java

License:Apache License

private void collectCounts(SortedDocValues singleDv, DocIdSetIterator disi, LongValues toGlobal)
        throws IOException {
    int doc;/* w  w w. java  2 s.  c om*/
    if (singleDv instanceof FieldCacheImpl.SortedDocValuesImpl.Iter) {

        FieldCacheImpl.SortedDocValuesImpl.Iter fc = (FieldCacheImpl.SortedDocValuesImpl.Iter) singleDv;
        while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
            int segOrd = fc.getOrd(doc);
            if (segOrd < 0)
                continue;
            int ord = (int) toGlobal.get(segOrd);
            countAcc.incrementCount(ord, 1);
        }

    } else {

        while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
            if (singleDv.advanceExact(doc)) {
                int segOrd = singleDv.ordValue();
                int ord = (int) toGlobal.get(segOrd);
                countAcc.incrementCount(ord, 1);
            }
        }

    }
}

From source file:org.apache.solr.search.facet.FacetFieldProcessorByArrayDV.java

License:Apache License

private void collectDocs(SortedSetDocValues multiDv, DocIdSetIterator disi, LongValues toGlobal)
        throws IOException {
    int doc;//  w  w w .j  a  va 2 s. c  o  m
    while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
        if (multiDv.advanceExact(doc)) {
            for (;;) {
                int segOrd = (int) multiDv.nextOrd();
                if (segOrd < 0)
                    break;
                collect(doc, segOrd, toGlobal);
            }
        }
    }
}