use of org.apache.lucene.index.SortedNumericDocValues in project elasticsearch by elastic.
the class BooleanFieldMapperTests method testDefaults.
public void testDefaults() throws IOException {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "boolean").endObject().endObject().endObject().endObject().string();
DocumentMapper defaultMapper = parser.parse("type", new CompressedXContent(mapping));
ParsedDocument doc = defaultMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("field", true).endObject().bytes());
try (Directory dir = new RAMDirectory();
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())))) {
w.addDocuments(doc.docs());
try (DirectoryReader reader = DirectoryReader.open(w)) {
final LeafReader leaf = reader.leaves().get(0).reader();
// boolean fields are indexed and have doc values by default
assertEquals(new BytesRef("T"), leaf.terms("field").iterator().next());
SortedNumericDocValues values = leaf.getSortedNumericDocValues("field");
assertNotNull(values);
values.setDocument(0);
assertEquals(1, values.count());
assertEquals(1, values.valueAt(0));
}
}
}
use of org.apache.lucene.index.SortedNumericDocValues in project elasticsearch by elastic.
the class MultiValueModeTests method verify.
private void verify(SortedNumericDocValues values, int maxDoc, FixedBitSet rootDocs, FixedBitSet innerDocs) throws IOException {
for (long missingValue : new long[] { 0, randomLong() }) {
for (MultiValueMode mode : new MultiValueMode[] { MultiValueMode.MIN, MultiValueMode.MAX, MultiValueMode.SUM, MultiValueMode.AVG }) {
final NumericDocValues selected = mode.select(values, missingValue, rootDocs, new BitSetIterator(innerDocs, 0L), maxDoc);
int prevRoot = -1;
for (int root = rootDocs.nextSetBit(0); root != -1; root = root + 1 < maxDoc ? rootDocs.nextSetBit(root + 1) : -1) {
final long actual = selected.get(root);
long expected = 0;
if (mode == MultiValueMode.MAX) {
expected = Long.MIN_VALUE;
} else if (mode == MultiValueMode.MIN) {
expected = Long.MAX_VALUE;
}
int numValues = 0;
for (int child = innerDocs.nextSetBit(prevRoot + 1); child != -1 && child < root; child = innerDocs.nextSetBit(child + 1)) {
values.setDocument(child);
for (int j = 0; j < values.count(); ++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));
}
++numValues;
}
}
if (numValues == 0) {
expected = missingValue;
} else if (mode == MultiValueMode.AVG) {
expected = numValues > 1 ? Math.round((double) expected / (double) numValues) : expected;
}
assertEquals(mode.toString() + " docId=" + root, expected, actual);
prevRoot = root;
}
}
}
}
use of org.apache.lucene.index.SortedNumericDocValues in project elasticsearch by elastic.
the class MultiValueModeTests method testMultiValuedLongs.
public void testMultiValuedLongs() throws Exception {
final int numDocs = scaledRandomIntBetween(1, 100);
final long[][] array = new long[numDocs][];
for (int i = 0; i < numDocs; ++i) {
final long[] values = new long[randomInt(4)];
for (int j = 0; j < values.length; ++j) {
values[j] = randomLong();
}
Arrays.sort(values);
array[i] = values;
}
final SortedNumericDocValues multiValues = new SortedNumericDocValues() {
int doc;
@Override
public long valueAt(int index) {
return array[doc][index];
}
@Override
public void setDocument(int doc) {
this.doc = doc;
}
@Override
public int count() {
return array[doc].length;
}
};
verify(multiValues, numDocs);
final FixedBitSet rootDocs = randomRootDocs(numDocs);
final FixedBitSet innerDocs = randomInnerDocs(rootDocs);
verify(multiValues, numDocs, rootDocs, innerDocs);
}
use of org.apache.lucene.index.SortedNumericDocValues in project elasticsearch by elastic.
the class ValuesSourceConfigTests method testEmptyLong.
public void testEmptyLong() throws Exception {
IndexService indexService = createIndex("index", Settings.EMPTY, "type", "long", "type=long");
client().prepareIndex("index", "type", "1").setSource().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L);
ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve(context, null, "long", null, null, null, null);
ValuesSource.Numeric valuesSource = config.toValuesSource(context);
LeafReaderContext ctx = searcher.reader().leaves().get(0);
SortedNumericDocValues values = valuesSource.longValues(ctx);
values.setDocument(0);
assertEquals(0, values.count());
config = ValuesSourceConfig.resolve(context, null, "long", null, 42, null, null);
valuesSource = config.toValuesSource(context);
values = valuesSource.longValues(ctx);
values.setDocument(0);
assertEquals(1, values.count());
assertEquals(42, values.valueAt(0));
}
}
use of org.apache.lucene.index.SortedNumericDocValues in project elasticsearch by elastic.
the class ValuesSourceConfigTests method testUnmappedLong.
public void testUnmappedLong() throws Exception {
IndexService indexService = createIndex("index", Settings.EMPTY, "type");
client().prepareIndex("index", "type", "1").setSource().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L);
ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve(context, ValueType.NUMBER, "long", null, null, null, null);
ValuesSource.Numeric valuesSource = config.toValuesSource(context);
assertNull(valuesSource);
config = ValuesSourceConfig.resolve(context, ValueType.NUMBER, "long", null, 42, null, null);
valuesSource = config.toValuesSource(context);
LeafReaderContext ctx = searcher.reader().leaves().get(0);
SortedNumericDocValues values = valuesSource.longValues(ctx);
values.setDocument(0);
assertEquals(1, values.count());
assertEquals(42, values.valueAt(0));
}
}
Aggregations