Example usage for org.apache.lucene.index SortedSetDocValues nextOrd

List of usage examples for org.apache.lucene.index SortedSetDocValues nextOrd

Introduction

In this page you can find the example usage for org.apache.lucene.index SortedSetDocValues nextOrd.

Prototype

public abstract long nextOrd() throws IOException;

Source Link

Document

Returns the next ordinal for the current document.

Usage

From source file:dk.statsbiblioteket.netark.dvenabler.DVReaderTest.java

License:Apache License

private static List<String> getSortedSetDocValues(AtomicReaderContext atomContext, int docID, String field)
        throws IOException {
    SortedSetDocValues dvs = atomContext.reader().getSortedSetDocValues(field);
    if (dvs == null) {
        throw new IllegalStateException("No SortedSetDocValues for field '" + field + "'");
    }//from   ww w.j  ava2 s  .  c  om
    dvs.setDocument(docID);
    List<String> values = new ArrayList<String>();
    BytesRef result = new BytesRef();
    long ord;
    while ((ord = dvs.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS) {
        dvs.lookupOrd(ord, result);
        values.add(result.utf8ToString());
    }
    return values;
}

From source file:lucene.security.index.SecureAtomicReader.java

License:Apache License

@Override
public SortedSetDocValues getSortedSetDocValues(String field) throws IOException {
    final SortedSetDocValues sortedSetDocValues = in.getSortedSetDocValues(field);
    if (sortedSetDocValues == null) {
        return null;
    }/* ww  w  .  j av  a2 s. com*/
    return new SortedSetDocValues() {

        private boolean _access;

        @Override
        public void setDocument(int docID) {
            try {
                if (_access = _accessControl.hasAccess(ReadType.SORTED_SET_DOC_VALUE, docID)) {
                    sortedSetDocValues.setDocument(docID);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public long nextOrd() {
            if (_access) {
                return sortedSetDocValues.nextOrd();
            }
            return NO_MORE_ORDS;
        }

        @Override
        public void lookupOrd(long ord, BytesRef result) {
            if (_access) {
                sortedSetDocValues.lookupOrd(ord, result);
            } else {
                result.bytes = BinaryDocValues.MISSING;
                result.length = 0;
                result.offset = 0;
            }
        }

        @Override
        public long getValueCount() {
            return sortedSetDocValues.getValueCount();
        }
    };
}

From source file:lucene.security.index.SecureAtomicReaderTestBase.java

License:Apache License

@Test
public void testSortedSetDocValues() throws IOException {
    SecureAtomicReader secureReader = getSecureReader();
    SortedSetDocValues sortedSetDocValues = secureReader.getSortedSetDocValues("sortedset");
    {/*www.ja  va  2  s . co  m*/
        BytesRef result = new BytesRef();
        int docID = 0;
        sortedSetDocValues.setDocument(docID);
        long ord = -1;
        assertTrue((ord = sortedSetDocValues.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS);
        sortedSetDocValues.lookupOrd(ord, result);
        assertEquals(new BytesRef(Integer.toString(docID)), result);

        assertTrue((ord = sortedSetDocValues.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS);
        sortedSetDocValues.lookupOrd(ord, result);
        assertEquals(new BytesRef("0" + Integer.toString(docID)), result);

        assertTrue((ord = sortedSetDocValues.nextOrd()) == SortedSetDocValues.NO_MORE_ORDS);
    }

    {
        int docID = 1;
        sortedSetDocValues.setDocument(docID);
        assertTrue(sortedSetDocValues.nextOrd() == SortedSetDocValues.NO_MORE_ORDS);
    }

    {
        BytesRef result = new BytesRef();
        int docID = 2;
        sortedSetDocValues.setDocument(docID);
        long ord = -1;
        assertTrue((ord = sortedSetDocValues.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS);
        sortedSetDocValues.lookupOrd(ord, result);
        assertEquals(new BytesRef("0" + Integer.toString(docID)), result);

        assertTrue((ord = sortedSetDocValues.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS);
        sortedSetDocValues.lookupOrd(ord, result);
        assertEquals(new BytesRef(Integer.toString(docID)), result);

        assertTrue((ord = sortedSetDocValues.nextOrd()) == SortedSetDocValues.NO_MORE_ORDS);
    }

    {
        int docID = 3;
        sortedSetDocValues.setDocument(docID);
        assertTrue(sortedSetDocValues.nextOrd() == SortedSetDocValues.NO_MORE_ORDS);
    }
}

From source file:org.alfresco.solr.transformer.DocValueDocTransformer.java

License:Open Source License

@Override
public void transform(SolrDocument doc, int docid, float score) throws IOException {
    for (String fieldName : context.getSearcher().getFieldNames()) {
        SchemaField schemaField = context.getSearcher().getSchema().getFieldOrNull(fieldName);
        if (schemaField != null) {
            if (schemaField.hasDocValues()) {
                SortedDocValues sortedDocValues = context.getSearcher().getSlowAtomicReader()
                        .getSortedDocValues(fieldName);
                if (sortedDocValues != null) {
                    int ordinal = sortedDocValues.getOrd(docid);
                    if (ordinal > -1) {
                        doc.removeFields(fieldName);
                        String alfrescoFieldName = AlfrescoSolrDataModel.getInstance()
                                .getAlfrescoPropertyFromSchemaField(fieldName);
                        doc.removeFields(alfrescoFieldName);
                        doc.addField(alfrescoFieldName, schemaField.getType().toObject(schemaField,
                                sortedDocValues.lookupOrd(ordinal)));
                    }// w w w .java  2 s .c  om
                }

                SortedSetDocValues sortedSetDocValues = context.getSearcher().getSlowAtomicReader()
                        .getSortedSetDocValues(fieldName);
                if (sortedSetDocValues != null) {
                    ArrayList<Object> newValues = new ArrayList<Object>();
                    sortedSetDocValues.setDocument(docid);
                    long ordinal;
                    while ((ordinal = sortedSetDocValues.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS) {
                        newValues.add(schemaField.getType().toObject(schemaField,
                                sortedSetDocValues.lookupOrd(ordinal)));
                    }
                    doc.removeFields(fieldName);
                    String alfrescoFieldName = AlfrescoSolrDataModel.getInstance()
                            .getAlfrescoPropertyFromSchemaField(fieldName);
                    doc.removeFields(alfrescoFieldName);
                    doc.addField(alfrescoFieldName, newValues);
                }

                BinaryDocValues binaryDocValues = context.getSearcher().getSlowAtomicReader()
                        .getBinaryDocValues(fieldName);
                if (binaryDocValues != null) {
                    doc.removeFields(fieldName);
                    String alfrescoFieldName = AlfrescoSolrDataModel.getInstance()
                            .getAlfrescoPropertyFromSchemaField(fieldName);
                    doc.removeFields(alfrescoFieldName);
                    doc.addField(alfrescoFieldName,
                            schemaField.getType().toObject(schemaField, binaryDocValues.get(docid)));
                }

                if (schemaField.getType().getNumericType() != null) {
                    NumericDocValues numericDocValues = context.getSearcher().getSlowAtomicReader()
                            .getNumericDocValues(fieldName);
                    if (numericDocValues != null) {
                        doc.removeFields(fieldName);
                        String alfrescoFieldName = AlfrescoSolrDataModel.getInstance()
                                .getAlfrescoPropertyFromSchemaField(fieldName);
                        doc.removeFields(alfrescoFieldName);
                        switch (schemaField.getType().getNumericType()) {
                        case DOUBLE:
                            doc.addField(alfrescoFieldName,
                                    Double.longBitsToDouble(numericDocValues.get(docid)));
                            break;
                        case FLOAT:
                            doc.addField(alfrescoFieldName,
                                    Float.intBitsToFloat((int) numericDocValues.get(docid)));
                            break;
                        case INT:
                            doc.addField(alfrescoFieldName, (int) numericDocValues.get(docid));
                            break;
                        case LONG:
                            doc.addField(alfrescoFieldName, numericDocValues.get(docid));
                            break;
                        }
                    }

                    SortedNumericDocValues sortedNumericDocValues = context.getSearcher().getSlowAtomicReader()
                            .getSortedNumericDocValues(fieldName);
                    if (sortedNumericDocValues != null) {
                        sortedNumericDocValues.setDocument(docid);
                        doc.removeFields(fieldName);
                        String alfrescoFieldName = AlfrescoSolrDataModel.getInstance()
                                .getAlfrescoPropertyFromSchemaField(fieldName);
                        doc.removeFields(alfrescoFieldName);
                        ArrayList<Object> newValues = new ArrayList<Object>(sortedNumericDocValues.count());
                        if (sortedNumericDocValues.count() > 0) {

                            for (int i = 0; i < sortedNumericDocValues.count(); i++) {
                                switch (schemaField.getType().getNumericType()) {
                                case DOUBLE:
                                    newValues.add(NumericUtils
                                            .sortableLongToDouble(sortedNumericDocValues.valueAt(i)));
                                    break;
                                case FLOAT:
                                    newValues.add(NumericUtils
                                            .sortableIntToFloat((int) sortedNumericDocValues.valueAt(i)));
                                    break;
                                case INT:
                                    newValues.add((int) sortedNumericDocValues.valueAt(i));
                                    break;
                                case LONG:
                                    newValues.add(sortedNumericDocValues.valueAt(i));
                                    break;

                                }
                            }
                        }
                        doc.addField(alfrescoFieldName, newValues);

                    }
                }
            }
        }
    }

}

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

License:Apache License

/** accumulates per-segment multi-valued facet counts, mapping to global ordinal space */
static void accumMulti(int counts[], int startTermIndex, SortedSetDocValues si, DocIdSetIterator disi,
        int subIndex, OrdinalMap map) throws IOException {
    int doc;//  www  .j  a  v  a  2s  . co  m
    while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
        si.setDocument(doc);
        // strange do-while to collect the missing count (first ord is NO_MORE_ORDS)
        int term = (int) si.nextOrd();
        if (term < 0) {
            if (startTermIndex == -1) {
                counts[0]++; // missing count
            }
            continue;
        }

        do {
            if (map != null) {
                term = (int) map.getGlobalOrd(subIndex, term);
            }
            int arrIdx = term - startTermIndex;
            if (arrIdx >= 0 && arrIdx < counts.length)
                counts[arrIdx]++;
        } while ((term = (int) si.nextOrd()) >= 0);
    }
}

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

License:Apache License

/** accumulates per-segment multi-valued stats */

static int accumMulti(int counts[], int docBase, FieldFacetStats[] facetStats, SortedSetDocValues si,
        DocIdSetIterator disi, int subIndex, OrdinalMap map) throws IOException {
    final LongValues ordMap = map == null ? null : map.getGlobalOrds(subIndex);
    int missingDocCount = 0;
    int doc;//from   w  w  w  . j a  v a 2 s  . c o  m
    while ((doc = disi.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
        si.setDocument(doc);
        long ord;
        boolean emptyTerm = true;
        while ((ord = si.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS) {
            emptyTerm = false;
            int term = (int) ord;
            if (map != null) {
                term = (int) ordMap.get(term);
            }
            counts[term]++;
            for (FieldFacetStats f : facetStats) {
                f.facetTermNum(docBase + doc, term);
            }
        }
        if (emptyTerm) {
            for (FieldFacetStats f : facetStats) {
                f.facetMissingNum(docBase + doc);
            }

            missingDocCount++;
        }
    }

    return missingDocCount;
}

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 ww .j  av  a2 s .co  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.schema.DocValuesMultiTest.java

License:Apache License

public void testDocValues() throws IOException {
    assertU(adoc("id", "1", "floatdv", "4.5", "intdv", "-1", "intdv", "3", "stringdv", "value1", "stringdv",
            "value2"));
    commit();//w ww.j ava2  s  .  co m
    SolrCore core = h.getCoreInc();
    try {
        final RefCounted<SolrIndexSearcher> searcherRef = core.openNewSearcher(true, true);
        final SolrIndexSearcher searcher = searcherRef.get();
        try {
            final AtomicReader reader = searcher.getAtomicReader();
            assertEquals(1, reader.numDocs());
            final FieldInfos infos = reader.getFieldInfos();
            assertEquals(DocValuesType.SORTED_SET, infos.fieldInfo("stringdv").getDocValuesType());
            assertEquals(DocValuesType.SORTED_SET, infos.fieldInfo("floatdv").getDocValuesType());
            assertEquals(DocValuesType.SORTED_SET, infos.fieldInfo("intdv").getDocValuesType());

            SortedSetDocValues dv = reader.getSortedSetDocValues("stringdv");
            dv.setDocument(0);
            assertEquals(0, dv.nextOrd());
            assertEquals(1, dv.nextOrd());
            assertEquals(SortedSetDocValues.NO_MORE_ORDS, dv.nextOrd());
        } finally {
            searcherRef.decref();
        }
    } finally {
        core.close();
    }
}

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  .  java 2  s  .  co  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(SortedSetDocValues multiDv, DocIdSetIterator disi, LongValues toGlobal)
        throws IOException {
    int doc;//from w ww .j  a v a 2  s  . c om
    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);
            }
        }
    }
}