Example usage for org.apache.lucene.index NumericDocValues NumericDocValues

List of usage examples for org.apache.lucene.index NumericDocValues NumericDocValues

Introduction

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

Prototype

protected NumericDocValues() 

Source Link

Document

Sole constructor.

Usage

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

License:Apache License

private NumericDocValues secureNumericDocValues(final NumericDocValues numericDocValues, final ReadType type) {
    if (numericDocValues == null) {
        return null;
    }/*ww  w. java  2  s  .  co  m*/
    return new NumericDocValues() {

        @Override
        public long get(int docID) {
            try {
                if (_accessControl.hasAccess(type, docID)) {
                    return numericDocValues.get(docID);
                }
                return 0L; // Default missing value.
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };
}

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

License:Apache License

private void getCountNumeric() throws IOException {
    final FieldType ft = schemaField.getType();
    final String fieldName = schemaField.getName();
    final NumericType numericType = ft.getNumericType();
    if (numericType == null) {
        throw new IllegalStateException();
    }/*  www.j a  va  2s  .com*/
    final List<AtomicReaderContext> leaves = searcher.getIndexReader().leaves();

    final Iterator<AtomicReaderContext> ctxIt = leaves.iterator();
    AtomicReaderContext ctx = null;
    NumericDocValues longs = null;
    Bits docsWithField = null;
    for (DocIterator docsIt = docs.iterator(); docsIt.hasNext();) {
        final int doc = docsIt.nextDoc();
        if (ctx == null || doc >= ctx.docBase + ctx.reader().maxDoc()) {
            do {
                ctx = ctxIt.next();
            } while (ctx == null || doc >= ctx.docBase + ctx.reader().maxDoc());
            assert doc >= ctx.docBase;
            switch (numericType) {
            case LONG:
                longs = DocValues.getNumeric(ctx.reader(), fieldName);
                break;
            case INT:
                longs = DocValues.getNumeric(ctx.reader(), fieldName);
                break;
            case FLOAT:
                final NumericDocValues floats = DocValues.getNumeric(ctx.reader(), fieldName);
                // TODO: this bit flipping should probably be moved to tie-break in the PQ comparator
                longs = new NumericDocValues() {
                    @Override
                    public long get(int docID) {
                        long bits = floats.get(docID);
                        if (bits < 0)
                            bits ^= 0x7fffffffffffffffL;
                        return bits;
                    }
                };
                break;
            case DOUBLE:
                final NumericDocValues doubles = DocValues.getNumeric(ctx.reader(), fieldName);
                // TODO: this bit flipping should probably be moved to tie-break in the PQ comparator
                longs = new NumericDocValues() {
                    @Override
                    public long get(int docID) {
                        long bits = doubles.get(docID);
                        if (bits < 0)
                            bits ^= 0x7fffffffffffffffL;
                        return bits;
                    }
                };
                break;
            default:
                throw new AssertionError();
            }
            docsWithField = DocValues.getDocsWithField(ctx.reader(), schemaField.getName());
        }
        long v = longs.get(doc - ctx.docBase);
        if (v != 0 || docsWithField.get(doc - ctx.docBase)) {
            accumIntervalWithValue(v);
        }
    }
}

From source file:org.codelibs.elasticsearch.index.fielddata.NumericDoubleValues.java

License:Apache License

/** Returns numeric docvalues view of raw double bits */
public NumericDocValues getRawDoubleValues() {
    return new NumericDocValues() {
        @Override//from  w  ww  .  j  av a2  s . co  m
        public long get(int docID) {
            return Double.doubleToRawLongBits(NumericDoubleValues.this.get(docID));
        }
    };
}

From source file:org.codelibs.elasticsearch.index.fielddata.NumericDoubleValues.java

License:Apache License

/** Returns numeric docvalues view of raw float bits */
public NumericDocValues getRawFloatValues() {
    return new NumericDocValues() {
        @Override/*from w  ww .  j  av a 2s.c  om*/
        public long get(int docID) {
            return Float.floatToRawIntBits((float) NumericDoubleValues.this.get(docID));
        }
    };
}

From source file:org.codelibs.elasticsearch.search.MultiValueMode.java

License:Apache License

/**
 * Return a {NumericDocValues} instance that can be used to sort documents
 * with this mode and the provided values. When a document has no value,
 * <code>missingValue</code> is returned.
 *
 * Allowed Modes: SUM, AVG, MEDIAN, MIN, MAX
 */// ww w .  j  a  v  a  2 s.  co  m
public NumericDocValues select(final SortedNumericDocValues values, final long missingValue) {
    final NumericDocValues singleton = DocValues.unwrapSingleton(values);
    if (singleton != null) {
        final Bits docsWithField = DocValues.unwrapSingletonBits(values);
        if (docsWithField == null || missingValue == 0) {
            return singleton;
        } else {
            return new NumericDocValues() {
                @Override
                public long get(int docID) {
                    final long value = singleton.get(docID);
                    if (value == 0 && docsWithField.get(docID) == false) {
                        return missingValue;
                    }
                    return value;
                }
            };
        }
    } else {
        return new NumericDocValues() {
            @Override
            public long get(int docID) {
                return pick(values, missingValue, docID);
            }
        };
    }
}

From source file:org.codelibs.elasticsearch.search.MultiValueMode.java

License:Apache License

/**
 * Return a {NumericDocValues} instance that can be used to sort root documents
 * with this mode, the provided values and filters for root/inner documents.
 *
 * For every root document, the values of its inner documents will be aggregated.
 * If none of the inner documents has a value, then <code>missingValue</code> is returned.
 *
 * Allowed Modes: SUM, AVG, MIN, MAX/*  w  w  w.  j  av a  2 s .c om*/
 *
 * NOTE: Calling the returned instance on docs that are not root docs is illegal
 *       The returned instance can only be evaluate the current and upcoming docs
 */
public NumericDocValues select(final SortedNumericDocValues values, final long missingValue,
        final BitSet rootDocs, final DocIdSetIterator innerDocs, int maxDoc) throws IOException {
    if (rootDocs == null || innerDocs == null) {
        return select(DocValues.emptySortedNumeric(maxDoc), missingValue);
    }

    return new NumericDocValues() {

        int lastSeenRootDoc = 0;
        long lastEmittedValue = missingValue;

        @Override
        public long get(int rootDoc) {
            assert rootDocs.get(rootDoc) : "can only sort root documents";
            assert rootDoc >= lastSeenRootDoc : "can only evaluate current and upcoming root docs";
            if (rootDoc == lastSeenRootDoc) {
                return lastEmittedValue;
            }
            try {
                final int prevRootDoc = rootDocs.prevSetBit(rootDoc - 1);
                final int firstNestedDoc;
                if (innerDocs.docID() > prevRootDoc) {
                    firstNestedDoc = innerDocs.docID();
                } else {
                    firstNestedDoc = innerDocs.advance(prevRootDoc + 1);
                }

                lastSeenRootDoc = rootDoc;
                lastEmittedValue = pick(values, missingValue, innerDocs, firstNestedDoc, rootDoc);
                return lastEmittedValue;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };
}

From source file:org.elasticsearch.index.fielddata.FieldDataTests.java

License:Apache License

public void testSortableLongBitsToDoubles() {
    final double value = randomDouble();
    final long valueBits = NumericUtils.doubleToSortableLong(value);

    NumericDocValues values = new NumericDocValues() {
        @Override//from www.  j  a va 2s.com
        public long get(int docID) {
            return valueBits;
        }
    };

    SortedNumericDoubleValues asMultiDoubles = FieldData
            .sortableLongBitsToDoubles(DocValues.singleton(values, null));
    NumericDoubleValues asDoubles = FieldData.unwrapSingleton(asMultiDoubles);
    assertNotNull(asDoubles);
    assertEquals(value, asDoubles.get(0), 0);

    NumericDocValues backToLongs = DocValues.unwrapSingleton(FieldData.toSortableLongBits(asMultiDoubles));
    assertSame(values, backToLongs);

    SortedNumericDocValues multiValues = new SortedNumericDocValues() {

        @Override
        public long valueAt(int index) {
            return valueBits;
        }

        @Override
        public void setDocument(int doc) {
        }

        @Override
        public int count() {
            return 1;
        }
    };

    asMultiDoubles = FieldData.sortableLongBitsToDoubles(multiValues);
    assertEquals(value, asMultiDoubles.valueAt(0), 0);
    assertSame(multiValues, FieldData.toSortableLongBits(asMultiDoubles));
}

From source file:org.elasticsearch.index.merge.policy.ElasticsearchMergePolicy.java

License:Apache License

/** Return an "upgraded" view of the reader. */
static AtomicReader filter(AtomicReader reader) throws IOException {
    final FieldInfos fieldInfos = reader.getFieldInfos();
    final FieldInfo versionInfo = fieldInfos.fieldInfo(VersionFieldMapper.NAME);
    if (versionInfo != null && versionInfo.hasDocValues()) {
        // the reader is a recent one, it has versions and they are stored
        // in a numeric doc values field
        return reader;
    }/*from w ww.j  a v a  2  s .  c o m*/
    // The segment is an old one, load all versions in memory and hide
    // them behind a numeric doc values field
    final Terms terms = reader.terms(UidFieldMapper.NAME);
    if (terms == null || !terms.hasPayloads()) {
        // The segment doesn't have an _uid field or doesn't have paylods
        // don't try to do anything clever. If any other segment has versions
        // all versions of this segment will be initialized to 0
        return reader;
    }
    final TermsEnum uids = terms.iterator(null);
    final GrowableWriter versions = new GrowableWriter(2, reader.maxDoc(), PackedInts.DEFAULT);
    DocsAndPositionsEnum dpe = null;
    for (BytesRef uid = uids.next(); uid != null; uid = uids.next()) {
        dpe = uids.docsAndPositions(reader.getLiveDocs(), dpe, DocsAndPositionsEnum.FLAG_PAYLOADS);
        assert dpe != null : "field has payloads";
        for (int doc = dpe.nextDoc(); doc != DocsEnum.NO_MORE_DOCS; doc = dpe.nextDoc()) {
            dpe.nextPosition();
            final BytesRef payload = dpe.getPayload();
            if (payload != null && payload.length == 8) {
                final long version = Numbers.bytesToLong(payload);
                versions.set(doc, version);
                break;
            }
        }
    }
    // Build new field infos, doc values, and return a filter reader
    final FieldInfo newVersionInfo;
    if (versionInfo == null) {
        // Find a free field number
        int fieldNumber = 0;
        for (FieldInfo fi : fieldInfos) {
            fieldNumber = Math.max(fieldNumber, fi.number + 1);
        }
        newVersionInfo = new FieldInfo(VersionFieldMapper.NAME, false, fieldNumber, false, true, false,
                IndexOptions.DOCS_ONLY, DocValuesType.NUMERIC, DocValuesType.NUMERIC, -1,
                Collections.<String, String>emptyMap());
    } else {
        newVersionInfo = new FieldInfo(VersionFieldMapper.NAME, versionInfo.isIndexed(), versionInfo.number,
                versionInfo.hasVectors(), versionInfo.omitsNorms(), versionInfo.hasPayloads(),
                versionInfo.getIndexOptions(), versionInfo.getDocValuesType(), versionInfo.getNormType(),
                versionInfo.getDocValuesGen(), versionInfo.attributes());
    }
    final ArrayList<FieldInfo> fieldInfoList = new ArrayList<>();
    for (FieldInfo info : fieldInfos) {
        if (info != versionInfo) {
            fieldInfoList.add(info);
        }
    }
    fieldInfoList.add(newVersionInfo);
    final FieldInfos newFieldInfos = new FieldInfos(fieldInfoList.toArray(new FieldInfo[fieldInfoList.size()]));
    final NumericDocValues versionValues = new NumericDocValues() {
        @Override
        public long get(int index) {
            return versions.get(index);
        }
    };
    return new FilterAtomicReader(reader) {
        @Override
        public FieldInfos getFieldInfos() {
            return newFieldInfos;
        }

        @Override
        public NumericDocValues getNumericDocValues(String field) throws IOException {
            if (VersionFieldMapper.NAME.equals(field)) {
                return versionValues;
            }
            return super.getNumericDocValues(field);
        }

        @Override
        public Bits getDocsWithField(String field) throws IOException {
            return new Bits.MatchAllBits(in.maxDoc());
        }
    };
}

From source file:org.elasticsearch.index.merge.policy.IndexUpgraderMergePolicy.java

License:Apache License

/** Return an "upgraded" view of the reader. */
static AtomicReader filter(AtomicReader reader) throws IOException {
    final FieldInfos fieldInfos = reader.getFieldInfos();
    final FieldInfo versionInfo = fieldInfos.fieldInfo(VersionFieldMapper.NAME);
    if (versionInfo != null && versionInfo.hasDocValues()) {
        // the reader is a recent one, it has versions and they are stored
        // in a numeric doc values field
        return reader;
    }//  w w w .java2 s  . co  m
    // The segment is an old one, load all versions in memory and hide
    // them behind a numeric doc values field
    final Terms terms = reader.terms(UidFieldMapper.NAME);
    if (terms == null || !terms.hasPayloads()) {
        // The segment doesn't have an _uid field or doesn't have paylods
        // don't try to do anything clever. If any other segment has versions
        // all versions of this segment will be initialized to 0
        return reader;
    }
    final TermsEnum uids = terms.iterator(null);
    final GrowableWriter versions = new GrowableWriter(2, reader.maxDoc(), PackedInts.DEFAULT);
    DocsAndPositionsEnum dpe = null;
    for (BytesRef uid = uids.next(); uid != null; uid = uids.next()) {
        dpe = uids.docsAndPositions(reader.getLiveDocs(), dpe, DocsAndPositionsEnum.FLAG_PAYLOADS);
        assert dpe != null : "field has payloads";
        for (int doc = dpe.nextDoc(); doc != DocsEnum.NO_MORE_DOCS; doc = dpe.nextDoc()) {
            dpe.nextPosition();
            final BytesRef payload = dpe.getPayload();
            if (payload != null && payload.length == 8) {
                final long version = Numbers.bytesToLong(payload);
                versions.set(doc, version);
                break;
            }
        }
    }
    // Build new field infos, doc values, and return a filter reader
    final FieldInfo newVersionInfo;
    if (versionInfo == null) {
        // Find a free field number
        int fieldNumber = 0;
        for (FieldInfo fi : fieldInfos) {
            fieldNumber = Math.max(fieldNumber, fi.number + 1);
        }
        newVersionInfo = new FieldInfo(VersionFieldMapper.NAME, false, fieldNumber, false, true, false,
                IndexOptions.DOCS_ONLY, DocValuesType.NUMERIC, DocValuesType.NUMERIC,
                Collections.<String, String>emptyMap());
    } else {
        newVersionInfo = new FieldInfo(VersionFieldMapper.NAME, versionInfo.isIndexed(), versionInfo.number,
                versionInfo.hasVectors(), versionInfo.omitsNorms(), versionInfo.hasPayloads(),
                versionInfo.getIndexOptions(), versionInfo.getDocValuesType(), versionInfo.getNormType(),
                versionInfo.attributes());
    }
    final ArrayList<FieldInfo> fieldInfoList = new ArrayList<FieldInfo>();
    for (FieldInfo info : fieldInfos) {
        if (info != versionInfo) {
            fieldInfoList.add(info);
        }
    }
    fieldInfoList.add(newVersionInfo);
    final FieldInfos newFieldInfos = new FieldInfos(fieldInfoList.toArray(new FieldInfo[fieldInfoList.size()]));
    final NumericDocValues versionValues = new NumericDocValues() {
        @Override
        public long get(int index) {
            return versions.get(index);
        }
    };
    return new FilterAtomicReader(reader) {
        @Override
        public FieldInfos getFieldInfos() {
            return newFieldInfos;
        }

        @Override
        public NumericDocValues getNumericDocValues(String field) throws IOException {
            if (VersionFieldMapper.NAME.equals(field)) {
                return versionValues;
            }
            return super.getNumericDocValues(field);
        }

        @Override
        public Bits getDocsWithField(String field) throws IOException {
            return new Bits.MatchAllBits(in.maxDoc());
        }
    };
}

From source file:org.elasticsearch.search.MultiValueModeTests.java

License:Apache License

public void testSingleValuedLongs() throws Exception {
    final int numDocs = scaledRandomIntBetween(1, 100);
    final long[] array = new long[numDocs];
    final FixedBitSet docsWithValue = randomBoolean() ? null : new FixedBitSet(numDocs);
    for (int i = 0; i < array.length; ++i) {
        if (randomBoolean()) {
            array[i] = randomLong();//from w  w  w  .  ja  va 2  s . c o m
            if (docsWithValue != null) {
                docsWithValue.set(i);
            }
        } else if (docsWithValue != null && randomBoolean()) {
            docsWithValue.set(i);
        }
    }
    final NumericDocValues singleValues = new NumericDocValues() {
        @Override
        public long get(int docID) {
            return array[docID];
        }
    };
    final SortedNumericDocValues multiValues = DocValues.singleton(singleValues, docsWithValue);
    verify(multiValues, numDocs);
    final FixedBitSet rootDocs = randomRootDocs(numDocs);
    final FixedBitSet innerDocs = randomInnerDocs(rootDocs);
    verify(multiValues, numDocs, rootDocs, innerDocs);
}