use of org.apache.lucene.index.NumericDocValues in project lucene-solr by apache.
the class SortedNumericSelector method wrap.
/**
* Wraps a multi-valued SortedNumericDocValues as a single-valued view, using the specified selector
* and numericType.
*/
public static NumericDocValues wrap(SortedNumericDocValues sortedNumeric, Type selector, SortField.Type numericType) {
if (numericType != SortField.Type.INT && numericType != SortField.Type.LONG && numericType != SortField.Type.FLOAT && numericType != SortField.Type.DOUBLE) {
throw new IllegalArgumentException("numericType must be a numeric type");
}
final NumericDocValues view;
NumericDocValues singleton = DocValues.unwrapSingleton(sortedNumeric);
if (singleton != null) {
// it's actually single-valued in practice, but indexed as multi-valued,
// so just sort on the underlying single-valued dv directly.
// regardless of selector type, this optimization is safe!
view = singleton;
} else {
switch(selector) {
case MIN:
view = new MinValue(sortedNumeric);
break;
case MAX:
view = new MaxValue(sortedNumeric);
break;
default:
throw new AssertionError();
}
}
// undo the numericutils sortability
switch(numericType) {
case FLOAT:
return new FilterNumericDocValues(view) {
@Override
public long longValue() throws IOException {
return NumericUtils.sortableFloatBits((int) in.longValue());
}
};
case DOUBLE:
return new FilterNumericDocValues(view) {
@Override
public long longValue() throws IOException {
return NumericUtils.sortableDoubleBits(in.longValue());
}
};
default:
return view;
}
}
use of org.apache.lucene.index.NumericDocValues in project elasticsearch by elastic.
the class VersionFetchSubPhase method hitExecute.
@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
if (context.version() == false || (context.storedFieldsContext() != null && context.storedFieldsContext().fetchFields() == false)) {
return;
}
long version = Versions.NOT_FOUND;
try {
NumericDocValues versions = hitContext.reader().getNumericDocValues(VersionFieldMapper.NAME);
if (versions != null) {
version = versions.get(hitContext.docId());
}
} catch (IOException e) {
throw new ElasticsearchException("Could not retrieve version", e);
}
hitContext.hit().version(version < 0 ? -1 : version);
}
use of org.apache.lucene.index.NumericDocValues in project elasticsearch by elastic.
the class FieldDataTests method testDoublesToSortableLongBits.
public void testDoublesToSortableLongBits() {
final double value = randomDouble();
final long valueBits = NumericUtils.doubleToSortableLong(value);
NumericDoubleValues values = new NumericDoubleValues() {
@Override
public double get(int docID) {
return value;
}
};
SortedNumericDocValues asMultiLongs = FieldData.toSortableLongBits(FieldData.singleton(values, null));
NumericDocValues asLongs = DocValues.unwrapSingleton(asMultiLongs);
assertNotNull(asLongs);
assertEquals(valueBits, asLongs.get(0));
SortedNumericDoubleValues multiValues = new SortedNumericDoubleValues() {
@Override
public double valueAt(int index) {
return value;
}
@Override
public void setDocument(int doc) {
}
@Override
public int count() {
return 1;
}
};
asMultiLongs = FieldData.toSortableLongBits(multiValues);
assertEquals(valueBits, asMultiLongs.valueAt(0));
assertSame(multiValues, FieldData.sortableLongBitsToDoubles(asMultiLongs));
}
use of org.apache.lucene.index.NumericDocValues in project elasticsearch by elastic.
the class MultiValueModeTests method testSingleValuedLongs.
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();
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);
}
use of org.apache.lucene.index.NumericDocValues in project elasticsearch by elastic.
the class MultiValueModeTests method verify.
private void verify(SortedNumericDocValues values, int maxDoc) {
for (long missingValue : new long[] { 0, randomLong() }) {
for (MultiValueMode mode : MultiValueMode.values()) {
final NumericDocValues selected = mode.select(values, missingValue);
for (int i = 0; i < maxDoc; ++i) {
final long actual = selected.get(i);
long expected = 0;
values.setDocument(i);
int numValues = values.count();
if (numValues == 0) {
expected = missingValue;
} else {
if (mode == MultiValueMode.MAX) {
expected = Long.MIN_VALUE;
} else if (mode == MultiValueMode.MIN) {
expected = Long.MAX_VALUE;
}
for (int j = 0; j < numValues; ++j) {
if (mode == MultiValueMode.SUM || mode == MultiValueMode.AVG) {
expected += values.valueAt(j);
} else if (mode == MultiValueMode.MIN) {
expected = Math.min(expected, values.valueAt(j));
} else if (mode == MultiValueMode.MAX) {
expected = Math.max(expected, values.valueAt(j));
}
}
if (mode == MultiValueMode.AVG) {
expected = numValues > 1 ? Math.round((double) expected / (double) numValues) : expected;
} else if (mode == MultiValueMode.MEDIAN) {
int value = numValues / 2;
if (numValues % 2 == 0) {
expected = Math.round((values.valueAt(value - 1) + values.valueAt(value)) / 2.0);
} else {
expected = values.valueAt(value);
}
}
}
assertEquals(mode.toString() + " docId=" + i, expected, actual);
}
}
}
}
Aggregations