Search in sources :

Example 1 with RandomAccessOrds

use of org.apache.lucene.index.RandomAccessOrds in project elasticsearch by elastic.

the class MultiValueModeTests method testMultiValuedOrds.

public void testMultiValuedOrds() 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] = j == 0 ? randomInt(1000) : values[j - 1] + 1 + randomInt(1000);
        }
        array[i] = values;
    }
    final RandomAccessOrds multiValues = new RandomAccessOrds() {

        int doc;

        @Override
        public long ordAt(int index) {
            return array[doc][index];
        }

        @Override
        public int cardinality() {
            return array[doc].length;
        }

        @Override
        public long nextOrd() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void setDocument(int docID) {
            this.doc = docID;
        }

        @Override
        public BytesRef lookupOrd(long ord) {
            throw new UnsupportedOperationException();
        }

        @Override
        public long getValueCount() {
            return 1 << 20;
        }
    };
    verify(multiValues, numDocs);
    final FixedBitSet rootDocs = randomRootDocs(numDocs);
    final FixedBitSet innerDocs = randomInnerDocs(rootDocs);
    verify(multiValues, numDocs, rootDocs, innerDocs);
}
Also used : RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds) FixedBitSet(org.apache.lucene.util.FixedBitSet)

Example 2 with RandomAccessOrds

use of org.apache.lucene.index.RandomAccessOrds in project elasticsearch by elastic.

the class MissingValuesTests method testMissingOrds.

public void testMissingOrds() {
    final int numDocs = TestUtil.nextInt(random(), 1, 100);
    final int numOrds = TestUtil.nextInt(random(), 1, 10);
    final Set<BytesRef> valueSet = new HashSet<>();
    while (valueSet.size() < numOrds) {
        valueSet.add(new BytesRef(RandomStrings.randomAsciiOfLength(random(), 5)));
    }
    final BytesRef[] values = valueSet.toArray(new BytesRef[numOrds]);
    Arrays.sort(values);
    final int[][] ords = new int[numDocs][];
    for (int i = 0; i < numDocs; ++i) {
        ords[i] = new int[random().nextInt(numOrds)];
        for (int j = 0; j < ords[i].length; ++j) {
            ords[i][j] = j;
        }
        for (int j = ords[i].length - 1; j >= 0; --j) {
            final int maxOrd = j == ords[i].length - 1 ? numOrds : ords[i][j + 1];
            ords[i][j] = TestUtil.nextInt(random(), ords[i][j], maxOrd - 1);
        }
    }
    RandomAccessOrds asRandomAccessOrds = new AbstractRandomAccessOrds() {

        int i = -1;

        @Override
        public void doSetDocument(int docID) {
            i = docID;
        }

        @Override
        public BytesRef lookupOrd(long ord) {
            return values[(int) ord];
        }

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

        @Override
        public long ordAt(int index) {
            return ords[i][index];
        }

        @Override
        public int cardinality() {
            return ords[i].length;
        }
    };
    final BytesRef existingMissing = RandomPicks.randomFrom(random(), values);
    final BytesRef missingMissing = new BytesRef(RandomStrings.randomAsciiOfLength(random(), 5));
    for (BytesRef missing : Arrays.asList(existingMissing, missingMissing)) {
        RandomAccessOrds withMissingReplaced = MissingValues.replaceMissing(asRandomAccessOrds, missing);
        if (valueSet.contains(missing)) {
            assertEquals(values.length, withMissingReplaced.getValueCount());
        } else {
            assertEquals(values.length + 1, withMissingReplaced.getValueCount());
        }
        for (int i = 0; i < numDocs; ++i) {
            withMissingReplaced.setDocument(i);
            if (ords[i].length > 0) {
                assertEquals(ords[i].length, withMissingReplaced.cardinality());
                for (int j = 0; j < ords[i].length; ++j) {
                    assertEquals(values[ords[i][j]], withMissingReplaced.lookupOrd(withMissingReplaced.ordAt(j)));
                }
            } else {
                assertEquals(1, withMissingReplaced.cardinality());
                assertEquals(missing, withMissingReplaced.lookupOrd(withMissingReplaced.ordAt(0)));
            }
        }
    }
}
Also used : RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds) AbstractRandomAccessOrds(org.elasticsearch.index.fielddata.AbstractRandomAccessOrds) AbstractRandomAccessOrds(org.elasticsearch.index.fielddata.AbstractRandomAccessOrds) GeoPoint(org.elasticsearch.common.geo.GeoPoint) BytesRef(org.apache.lucene.util.BytesRef) HashSet(java.util.HashSet)

Example 3 with RandomAccessOrds

use of org.apache.lucene.index.RandomAccessOrds in project elasticsearch by elastic.

the class MultiOrdinalsTests method testOrdinals.

public void testOrdinals() throws Exception {
    int maxDoc = 7;
    long maxOrds = 32;
    OrdinalsBuilder builder = new OrdinalsBuilder(maxDoc);
    // 0
    builder.nextOrdinal();
    builder.addDoc(1).addDoc(4).addDoc(5).addDoc(6);
    // 1
    builder.nextOrdinal();
    builder.addDoc(0).addDoc(5).addDoc(6);
    // 3
    builder.nextOrdinal();
    builder.addDoc(2).addDoc(4).addDoc(5).addDoc(6);
    // 3
    builder.nextOrdinal();
    builder.addDoc(0).addDoc(4).addDoc(5).addDoc(6);
    // 4
    builder.nextOrdinal();
    builder.addDoc(4).addDoc(5).addDoc(6);
    // 5
    builder.nextOrdinal();
    builder.addDoc(4).addDoc(5).addDoc(6);
    while (builder.getValueCount() < maxOrds) {
        builder.nextOrdinal();
        builder.addDoc(5).addDoc(6);
    }
    long[][] ordinalPlan = new long[][] { { 1, 3 }, { 0 }, { 2 }, {}, { 0, 2, 3, 4, 5 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 } };
    Ordinals ordinals = creationMultiOrdinals(builder);
    RandomAccessOrds docs = ordinals.ordinals();
    assertEquals(docs, ordinalPlan);
}
Also used : RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds)

Example 4 with RandomAccessOrds

use of org.apache.lucene.index.RandomAccessOrds in project elasticsearch by elastic.

the class AbstractStringFieldDataTestCase method testGlobalOrdinals.

public void testGlobalOrdinals() throws Exception {
    fillExtendedMvSet();
    refreshReader();
    IndexOrdinalsFieldData ifd = getForField("string", "value", hasDocValues());
    IndexOrdinalsFieldData globalOrdinals = ifd.loadGlobal(topLevelReader);
    assertThat(topLevelReader.leaves().size(), equalTo(3));
    // First segment
    assertThat(globalOrdinals, instanceOf(GlobalOrdinalsIndexFieldData.class));
    LeafReaderContext leaf = topLevelReader.leaves().get(0);
    AtomicOrdinalsFieldData afd = globalOrdinals.load(leaf);
    RandomAccessOrds values = afd.getOrdinalsValues();
    assertIteratorConsistentWithRandomAccess(values, leaf.reader().maxDoc());
    values.setDocument(0);
    assertThat(values.cardinality(), equalTo(2));
    long ord = values.nextOrd();
    assertThat(ord, equalTo(3L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("02"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(5L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("04"));
    values.setDocument(1);
    assertThat(values.cardinality(), equalTo(0));
    values.setDocument(2);
    assertThat(values.cardinality(), equalTo(1));
    ord = values.nextOrd();
    assertThat(ord, equalTo(4L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("03"));
    // Second segment
    leaf = topLevelReader.leaves().get(1);
    afd = globalOrdinals.load(leaf);
    values = afd.getOrdinalsValues();
    assertIteratorConsistentWithRandomAccess(values, leaf.reader().maxDoc());
    values.setDocument(0);
    assertThat(values.cardinality(), equalTo(3));
    ord = values.nextOrd();
    assertThat(ord, equalTo(5L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("04"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(6L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("05"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(7L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("06"));
    values.setDocument(1);
    assertThat(values.cardinality(), equalTo(3));
    ord = values.nextOrd();
    assertThat(ord, equalTo(7L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("06"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(8L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("07"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(9L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("08"));
    values.setDocument(2);
    assertThat(values.cardinality(), equalTo(0));
    values.setDocument(3);
    assertThat(values.cardinality(), equalTo(3));
    ord = values.nextOrd();
    assertThat(ord, equalTo(9L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("08"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(10L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("09"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(11L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("10"));
    // Third segment
    leaf = topLevelReader.leaves().get(2);
    afd = globalOrdinals.load(leaf);
    values = afd.getOrdinalsValues();
    assertIteratorConsistentWithRandomAccess(values, leaf.reader().maxDoc());
    values.setDocument(0);
    values.setDocument(0);
    assertThat(values.cardinality(), equalTo(3));
    ord = values.nextOrd();
    assertThat(ord, equalTo(0L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("!08"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(1L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("!09"));
    ord = values.nextOrd();
    assertThat(ord, equalTo(2L));
    assertThat(values.lookupOrd(ord).utf8ToString(), equalTo("!10"));
}
Also used : GlobalOrdinalsIndexFieldData(org.elasticsearch.index.fielddata.ordinals.GlobalOrdinalsIndexFieldData) RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds) LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Example 5 with RandomAccessOrds

use of org.apache.lucene.index.RandomAccessOrds in project elasticsearch by elastic.

the class SingleOrdinalsTests method testSvValues.

public void testSvValues() throws IOException {
    int numDocs = 1000000;
    int numOrdinals = numDocs / 4;
    Map<Integer, Long> controlDocToOrdinal = new HashMap<>();
    OrdinalsBuilder builder = new OrdinalsBuilder(numDocs);
    long ordinal = builder.currentOrdinal();
    for (int doc = 0; doc < numDocs; doc++) {
        if (doc % numOrdinals == 0) {
            ordinal = builder.nextOrdinal();
        }
        controlDocToOrdinal.put(doc, ordinal);
        builder.addDoc(doc);
    }
    Ordinals ords = builder.build();
    assertThat(ords, instanceOf(SinglePackedOrdinals.class));
    RandomAccessOrds docs = ords.ordinals();
    final SortedDocValues singleOrds = DocValues.unwrapSingleton(docs);
    assertNotNull(singleOrds);
    for (Map.Entry<Integer, Long> entry : controlDocToOrdinal.entrySet()) {
        assertThat(entry.getValue(), equalTo((long) singleOrds.getOrd(entry.getKey())));
    }
}
Also used : HashMap(java.util.HashMap) RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds) SortedDocValues(org.apache.lucene.index.SortedDocValues) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

RandomAccessOrds (org.apache.lucene.index.RandomAccessOrds)12 SortedDocValues (org.apache.lucene.index.SortedDocValues)4 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)3 BytesRef (org.apache.lucene.util.BytesRef)3 HashSet (java.util.HashSet)2 Random (java.util.Random)2 FixedBitSet (org.apache.lucene.util.FixedBitSet)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 Document (org.apache.lucene.document.Document)1 StringField (org.apache.lucene.document.StringField)1 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)1 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)1 Scorer (org.apache.lucene.search.Scorer)1 BitSet (org.apache.lucene.util.BitSet)1 LongBitSet (org.apache.lucene.util.LongBitSet)1 GeoPoint (org.elasticsearch.common.geo.GeoPoint)1 AbstractRandomAccessOrds (org.elasticsearch.index.fielddata.AbstractRandomAccessOrds)1