Search in sources :

Example 86 with SortedNumericDocValuesField

use of org.apache.lucene.document.SortedNumericDocValuesField 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)

Example 87 with SortedNumericDocValuesField

use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.

the class FunctionTestSetup method addDoc.

private static void addDoc(RandomIndexWriter iw, int i) throws Exception {
    Document d = new Document();
    Field f;
    int scoreAndID = i + 1;
    FieldType customType = new FieldType(TextField.TYPE_STORED);
    customType.setTokenized(false);
    customType.setOmitNorms(true);
    // for debug purposes
    f = newField(ID_FIELD, id2String(scoreAndID), customType);
    d.add(f);
    d.add(new SortedDocValuesField(ID_FIELD, new BytesRef(id2String(scoreAndID))));
    FieldType customType2 = new FieldType(TextField.TYPE_NOT_STORED);
    customType2.setOmitNorms(true);
    // for regular search
    f = newField(TEXT_FIELD, "text of doc" + scoreAndID + textLine(i), customType2);
    d.add(f);
    // for function scoring
    f = new StoredField(INT_FIELD, scoreAndID);
    d.add(f);
    d.add(new NumericDocValuesField(INT_FIELD, scoreAndID));
    // for function scoring
    f = new StoredField(FLOAT_FIELD, scoreAndID);
    d.add(f);
    d.add(new NumericDocValuesField(FLOAT_FIELD, Float.floatToRawIntBits(scoreAndID)));
    f = new StoredField(INT_FIELD_MV_MIN, scoreAndID);
    d.add(f);
    f = new StoredField(INT_FIELD_MV_MIN, scoreAndID + 1);
    d.add(f);
    d.add(new SortedNumericDocValuesField(INT_FIELD_MV_MIN, scoreAndID));
    d.add(new SortedNumericDocValuesField(INT_FIELD_MV_MIN, scoreAndID + 1));
    f = new StoredField(INT_FIELD_MV_MAX, scoreAndID);
    d.add(f);
    f = new StoredField(INT_FIELD_MV_MAX, scoreAndID - 1);
    d.add(f);
    d.add(new SortedNumericDocValuesField(INT_FIELD_MV_MAX, scoreAndID));
    d.add(new SortedNumericDocValuesField(INT_FIELD_MV_MAX, scoreAndID - 1));
    f = new StoredField(FLOAT_FIELD_MV_MIN, scoreAndID);
    d.add(f);
    f = new StoredField(FLOAT_FIELD_MV_MIN, scoreAndID + 1);
    d.add(f);
    d.add(new SortedNumericDocValuesField(FLOAT_FIELD_MV_MIN, NumericUtils.floatToSortableInt(scoreAndID)));
    d.add(new SortedNumericDocValuesField(FLOAT_FIELD_MV_MIN, NumericUtils.floatToSortableInt(scoreAndID + 1)));
    f = new StoredField(FLOAT_FIELD_MV_MAX, scoreAndID);
    d.add(f);
    f = new StoredField(FLOAT_FIELD_MV_MAX, scoreAndID - 1);
    d.add(f);
    d.add(new SortedNumericDocValuesField(FLOAT_FIELD_MV_MAX, NumericUtils.floatToSortableInt(scoreAndID)));
    d.add(new SortedNumericDocValuesField(FLOAT_FIELD_MV_MAX, NumericUtils.floatToSortableInt(scoreAndID - 1)));
    iw.addDocument(d);
    log("added: " + d);
}
Also used : NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) StoredField(org.apache.lucene.document.StoredField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) StoredField(org.apache.lucene.document.StoredField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Document(org.apache.lucene.document.Document) BytesRef(org.apache.lucene.util.BytesRef) FieldType(org.apache.lucene.document.FieldType)

Example 88 with SortedNumericDocValuesField

use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.

the class BaseDocValuesFormatTestCase method testOneSortedNumber.

public void testOneSortedNumber() throws IOException {
    Directory directory = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), directory);
    Document doc = new Document();
    doc.add(new SortedNumericDocValuesField("dv", 5));
    writer.addDocument(doc);
    writer.close();
    // Now search the index:
    IndexReader reader = DirectoryReader.open(directory);
    assert reader.leaves().size() == 1;
    SortedNumericDocValues dv = reader.leaves().get(0).reader().getSortedNumericDocValues("dv");
    assertEquals(0, dv.nextDoc());
    assertEquals(1, dv.docValueCount());
    assertEquals(5, dv.nextValue());
    reader.close();
    directory.close();
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory)

Example 89 with SortedNumericDocValuesField

use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.

the class BaseDocValuesFormatTestCase method testTwoSortedNumberOneMissing.

public void testTwoSortedNumberOneMissing() throws IOException {
    Directory directory = newDirectory();
    IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(null));
    Document doc = new Document();
    doc.add(new SortedNumericDocValuesField("dv", 11));
    doc.add(new SortedNumericDocValuesField("dv", -5));
    writer.addDocument(doc);
    writer.addDocument(new Document());
    writer.close();
    // Now search the index:
    IndexReader reader = DirectoryReader.open(directory);
    assert reader.leaves().size() == 1;
    SortedNumericDocValues dv = reader.leaves().get(0).reader().getSortedNumericDocValues("dv");
    assertEquals(0, dv.nextDoc());
    assertEquals(2, dv.docValueCount());
    assertEquals(-5, dv.nextValue());
    assertEquals(11, dv.nextValue());
    assertEquals(NO_MORE_DOCS, dv.nextDoc());
    reader.close();
    directory.close();
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory)

Example 90 with SortedNumericDocValuesField

use of org.apache.lucene.document.SortedNumericDocValuesField in project lucene-solr by apache.

the class BaseDocValuesFormatTestCase method addRandomFields.

@Override
protected void addRandomFields(Document doc) {
    if (usually()) {
        doc.add(new NumericDocValuesField("ndv", random().nextInt(1 << 12)));
        doc.add(new BinaryDocValuesField("bdv", new BytesRef(TestUtil.randomSimpleString(random()))));
        doc.add(new SortedDocValuesField("sdv", new BytesRef(TestUtil.randomSimpleString(random(), 2))));
    }
    int numValues = random().nextInt(5);
    for (int i = 0; i < numValues; ++i) {
        doc.add(new SortedSetDocValuesField("ssdv", new BytesRef(TestUtil.randomSimpleString(random(), 2))));
    }
    numValues = random().nextInt(5);
    for (int i = 0; i < numValues; ++i) {
        doc.add(new SortedNumericDocValuesField("sndv", TestUtil.nextLong(random(), Long.MIN_VALUE, Long.MAX_VALUE)));
    }
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)104 Document (org.apache.lucene.document.Document)86 Directory (org.apache.lucene.store.Directory)72 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)44 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)40 IndexReader (org.apache.lucene.index.IndexReader)35 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)27 BytesRef (org.apache.lucene.util.BytesRef)21 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)19 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)19 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)18 StringField (org.apache.lucene.document.StringField)18 IndexSearcher (org.apache.lucene.search.IndexSearcher)18 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)16 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)16 Sort (org.apache.lucene.search.Sort)15 SortField (org.apache.lucene.search.SortField)14 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)13 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)12 StoredField (org.apache.lucene.document.StoredField)12