Search in sources :

Example 6 with BytesRefFieldSource

use of org.apache.lucene.queries.function.valuesource.BytesRefFieldSource in project lucene-solr by apache.

the class TestDocValuesFieldSources method test.

@SuppressWarnings("fallthrough")
public void test(DocValuesType type) throws IOException {
    Directory d = newDirectory();
    IndexWriterConfig iwConfig = newIndexWriterConfig(new MockAnalyzer(random()));
    final int nDocs = atLeast(50);
    final Field id = new NumericDocValuesField("id", 0);
    final Field f;
    switch(type) {
        case BINARY:
            f = new BinaryDocValuesField("dv", new BytesRef());
            break;
        case SORTED:
            f = new SortedDocValuesField("dv", new BytesRef());
            break;
        case NUMERIC:
            f = new NumericDocValuesField("dv", 0);
            break;
        case SORTED_NUMERIC:
            f = new SortedNumericDocValuesField("dv", 0);
            break;
        default:
            throw new AssertionError();
    }
    Document document = new Document();
    document.add(id);
    document.add(f);
    final Object[] vals = new Object[nDocs];
    RandomIndexWriter iw = new RandomIndexWriter(random(), d, iwConfig);
    for (int i = 0; i < nDocs; ++i) {
        id.setLongValue(i);
        switch(type) {
            case SORTED:
            case BINARY:
                do {
                    vals[i] = TestUtil.randomSimpleString(random(), 20);
                } while (((String) vals[i]).isEmpty());
                f.setBytesValue(new BytesRef((String) vals[i]));
                break;
            case NUMERIC:
            case SORTED_NUMERIC:
                // keep it an int
                final int bitsPerValue = RandomNumbers.randomIntBetween(random(), 1, 31);
                vals[i] = (long) random().nextInt((int) PackedInts.maxValue(bitsPerValue));
                f.setLongValue((Long) vals[i]);
                break;
        }
        iw.addDocument(document);
        if (random().nextBoolean() && i % 10 == 9) {
            iw.commit();
        }
    }
    iw.close();
    DirectoryReader rd = DirectoryReader.open(d);
    for (LeafReaderContext leave : rd.leaves()) {
        final FunctionValues ids = new LongFieldSource("id").getValues(null, leave);
        final ValueSource vs;
        switch(type) {
            case BINARY:
            case SORTED:
                vs = new BytesRefFieldSource("dv");
                break;
            case NUMERIC:
                vs = new LongFieldSource("dv");
                break;
            case SORTED_NUMERIC:
                // Since we are not indexing multiple values, MIN and MAX should work the same way
                vs = random().nextBoolean() ? new MultiValuedLongFieldSource("dv", Type.MIN) : new MultiValuedLongFieldSource("dv", Type.MAX);
                break;
            default:
                throw new AssertionError();
        }
        final FunctionValues values = vs.getValues(null, leave);
        BytesRefBuilder bytes = new BytesRefBuilder();
        for (int i = 0; i < leave.reader().maxDoc(); ++i) {
            assertTrue(values.exists(i));
            if (vs instanceof BytesRefFieldSource) {
                assertTrue(values.objectVal(i) instanceof String);
            } else if (vs instanceof LongFieldSource) {
                assertTrue(values.objectVal(i) instanceof Long);
                assertTrue(values.bytesVal(i, bytes));
            } else {
                throw new AssertionError();
            }
            Object expected = vals[ids.intVal(i)];
            switch(type) {
                case SORTED:
                    // no exception
                    values.ordVal(i);
                    assertTrue(values.numOrd() >= 1);
                // fall-through
                case BINARY:
                    assertEquals(expected, values.objectVal(i));
                    assertEquals(expected, values.strVal(i));
                    assertEquals(expected, values.objectVal(i));
                    assertEquals(expected, values.strVal(i));
                    assertTrue(values.bytesVal(i, bytes));
                    assertEquals(new BytesRef((String) expected), bytes.get());
                    break;
                case NUMERIC:
                case SORTED_NUMERIC:
                    assertEquals(((Number) expected).longValue(), values.longVal(i));
                    break;
            }
        }
    }
    rd.close();
    d.close();
}
Also used : BytesRefFieldSource(org.apache.lucene.queries.function.valuesource.BytesRefFieldSource) Document(org.apache.lucene.document.Document) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Field(org.apache.lucene.document.Field) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory) MultiValuedLongFieldSource(org.apache.lucene.queries.function.valuesource.MultiValuedLongFieldSource) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DirectoryReader(org.apache.lucene.index.DirectoryReader) LongFieldSource(org.apache.lucene.queries.function.valuesource.LongFieldSource) MultiValuedLongFieldSource(org.apache.lucene.queries.function.valuesource.MultiValuedLongFieldSource) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

BytesRefFieldSource (org.apache.lucene.queries.function.valuesource.BytesRefFieldSource)6 ValueSource (org.apache.lucene.queries.function.ValueSource)2 LongFieldSource (org.apache.lucene.queries.function.valuesource.LongFieldSource)2 BytesRef (org.apache.lucene.util.BytesRef)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)1 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)1 Document (org.apache.lucene.document.Document)1 Field (org.apache.lucene.document.Field)1 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)1 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)1 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)1 ConstValueSource (org.apache.lucene.queries.function.valuesource.ConstValueSource)1 DocFreqValueSource (org.apache.lucene.queries.function.valuesource.DocFreqValueSource)1 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)1