Search in sources :

Example 1 with FunctionValues

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

the class DocValuesTest method testDocValues.

@Test
public void testDocValues() throws IOException {
    assertU(adoc("id", "1"));
    assertU(commit());
    try (SolrCore core = h.getCoreInc()) {
        final RefCounted<SolrIndexSearcher> searcherRef = core.openNewSearcher(true, true);
        final SolrIndexSearcher searcher = searcherRef.get();
        try {
            final LeafReader reader = searcher.getSlowAtomicReader();
            assertEquals(1, reader.numDocs());
            final FieldInfos infos = reader.getFieldInfos();
            assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("floatdv").getDocValuesType());
            assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("intdv").getDocValuesType());
            assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("doubledv").getDocValuesType());
            assertEquals(DocValuesType.NUMERIC, infos.fieldInfo("longdv").getDocValuesType());
            assertEquals(DocValuesType.SORTED, infos.fieldInfo("stringdv").getDocValuesType());
            assertEquals(DocValuesType.SORTED, infos.fieldInfo("booldv").getDocValuesType());
            NumericDocValues dvs = reader.getNumericDocValues("floatdv");
            assertEquals(0, dvs.nextDoc());
            assertEquals((long) Float.floatToIntBits(1), dvs.longValue());
            dvs = reader.getNumericDocValues("intdv");
            assertEquals(0, dvs.nextDoc());
            assertEquals(2L, dvs.longValue());
            dvs = reader.getNumericDocValues("doubledv");
            assertEquals(0, dvs.nextDoc());
            assertEquals(Double.doubleToLongBits(3), dvs.longValue());
            dvs = reader.getNumericDocValues("longdv");
            assertEquals(0, dvs.nextDoc());
            assertEquals(4L, dvs.longValue());
            SortedDocValues sdv = reader.getSortedDocValues("stringdv");
            assertEquals(0, sdv.nextDoc());
            assertEquals("solr", sdv.binaryValue().utf8ToString());
            sdv = reader.getSortedDocValues("booldv");
            assertEquals(0, sdv.nextDoc());
            assertEquals("T", sdv.binaryValue().utf8ToString());
            final IndexSchema schema = core.getLatestSchema();
            final SchemaField floatDv = schema.getField("floatdv");
            final SchemaField intDv = schema.getField("intdv");
            final SchemaField doubleDv = schema.getField("doubledv");
            final SchemaField longDv = schema.getField("longdv");
            final SchemaField boolDv = schema.getField("booldv");
            FunctionValues values = floatDv.getType().getValueSource(floatDv, null).getValues(null, searcher.getSlowAtomicReader().leaves().get(0));
            assertEquals(1f, values.floatVal(0), 0f);
            assertEquals(1f, values.objectVal(0));
            values = intDv.getType().getValueSource(intDv, null).getValues(null, searcher.getSlowAtomicReader().leaves().get(0));
            assertEquals(2, values.intVal(0));
            assertEquals(2, values.objectVal(0));
            values = doubleDv.getType().getValueSource(doubleDv, null).getValues(null, searcher.getSlowAtomicReader().leaves().get(0));
            assertEquals(3d, values.doubleVal(0), 0d);
            assertEquals(3d, values.objectVal(0));
            values = longDv.getType().getValueSource(longDv, null).getValues(null, searcher.getSlowAtomicReader().leaves().get(0));
            assertEquals(4L, values.longVal(0));
            assertEquals(4L, values.objectVal(0));
            values = boolDv.getType().getValueSource(boolDv, null).getValues(null, searcher.getSlowAtomicReader().leaves().get(0));
            assertEquals("true", values.strVal(0));
            assertEquals(true, values.objectVal(0));
            // check reversibility of created fields
            tstToObj(schema.getField("floatdv"), -1.5f);
            tstToObj(schema.getField("floatdvs"), -1.5f);
            tstToObj(schema.getField("doubledv"), -1.5d);
            tstToObj(schema.getField("doubledvs"), -1.5d);
            tstToObj(schema.getField("intdv"), -7);
            tstToObj(schema.getField("intdvs"), -7);
            tstToObj(schema.getField("longdv"), -11L);
            tstToObj(schema.getField("longdvs"), -11L);
            tstToObj(schema.getField("datedv"), new Date(1000));
            tstToObj(schema.getField("datedvs"), new Date(1000));
            tstToObj(schema.getField("stringdv"), "foo");
            tstToObj(schema.getField("stringdvs"), "foo");
            tstToObj(schema.getField("booldv"), true);
            tstToObj(schema.getField("booldvs"), true);
        } finally {
            searcherRef.decref();
        }
    }
}
Also used : FieldInfos(org.apache.lucene.index.FieldInfos) NumericDocValues(org.apache.lucene.index.NumericDocValues) LeafReader(org.apache.lucene.index.LeafReader) SolrCore(org.apache.solr.core.SolrCore) FunctionValues(org.apache.lucene.queries.function.FunctionValues) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SortedDocValues(org.apache.lucene.index.SortedDocValues) Date(java.util.Date) Test(org.junit.Test)

Example 2 with FunctionValues

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

the class IfFunction method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues ifVals = ifSource.getValues(context, readerContext);
    final FunctionValues trueVals = trueSource.getValues(context, readerContext);
    final FunctionValues falseVals = falseSource.getValues(context, readerContext);
    return new FunctionValues() {

        @Override
        public byte byteVal(int doc) throws IOException {
            return ifVals.boolVal(doc) ? trueVals.byteVal(doc) : falseVals.byteVal(doc);
        }

        @Override
        public short shortVal(int doc) throws IOException {
            return ifVals.boolVal(doc) ? trueVals.shortVal(doc) : falseVals.shortVal(doc);
        }

        @Override
        public float floatVal(int doc) throws IOException {
            return ifVals.boolVal(doc) ? trueVals.floatVal(doc) : falseVals.floatVal(doc);
        }

        @Override
        public int intVal(int doc) throws IOException {
            return ifVals.boolVal(doc) ? trueVals.intVal(doc) : falseVals.intVal(doc);
        }

        @Override
        public long longVal(int doc) throws IOException {
            return ifVals.boolVal(doc) ? trueVals.longVal(doc) : falseVals.longVal(doc);
        }

        @Override
        public double doubleVal(int doc) throws IOException {
            return ifVals.boolVal(doc) ? trueVals.doubleVal(doc) : falseVals.doubleVal(doc);
        }

        @Override
        public String strVal(int doc) throws IOException {
            return ifVals.boolVal(doc) ? trueVals.strVal(doc) : falseVals.strVal(doc);
        }

        @Override
        public boolean boolVal(int doc) throws IOException {
            return ifVals.boolVal(doc) ? trueVals.boolVal(doc) : falseVals.boolVal(doc);
        }

        @Override
        public boolean bytesVal(int doc, BytesRefBuilder target) throws IOException {
            return ifVals.boolVal(doc) ? trueVals.bytesVal(doc, target) : falseVals.bytesVal(doc, target);
        }

        @Override
        public Object objectVal(int doc) throws IOException {
            return ifVals.boolVal(doc) ? trueVals.objectVal(doc) : falseVals.objectVal(doc);
        }

        @Override
        public boolean exists(int doc) throws IOException {
            return ifVals.boolVal(doc) ? trueVals.exists(doc) : falseVals.exists(doc);
        }

        @Override
        public ValueFiller getValueFiller() {
            // for now, use float.
            return super.getValueFiller();
        }

        @Override
        public String toString(int doc) throws IOException {
            return "if(" + ifVals.toString(doc) + ',' + trueVals.toString(doc) + ',' + falseVals.toString(doc) + ')';
        }
    };
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) FunctionValues(org.apache.lucene.queries.function.FunctionValues)

Example 3 with FunctionValues

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

the class MultiBoolFunction method getValues.

@Override
public BoolDocValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues[] vals = new FunctionValues[sources.size()];
    int i = 0;
    for (ValueSource source : sources) {
        vals[i++] = source.getValues(context, readerContext);
    }
    return new BoolDocValues(this) {

        @Override
        public boolean boolVal(int doc) throws IOException {
            return func(doc, vals);
        }

        @Override
        public String toString(int doc) throws IOException {
            StringBuilder sb = new StringBuilder(name());
            sb.append('(');
            boolean first = true;
            for (FunctionValues dv : vals) {
                if (first) {
                    first = false;
                } else {
                    sb.append(',');
                }
                sb.append(dv.toString(doc));
            }
            return sb.toString();
        }
    };
}
Also used : ValueSource(org.apache.lucene.queries.function.ValueSource) BoolDocValues(org.apache.lucene.queries.function.docvalues.BoolDocValues) FunctionValues(org.apache.lucene.queries.function.FunctionValues)

Example 4 with FunctionValues

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

the class MultiFunction method valsArr.

public static FunctionValues[] valsArr(List<ValueSource> sources, Map fcontext, LeafReaderContext readerContext) throws IOException {
    final FunctionValues[] valsArr = new FunctionValues[sources.size()];
    int i = 0;
    for (ValueSource source : sources) {
        valsArr[i++] = source.getValues(fcontext, readerContext);
    }
    return valsArr;
}
Also used : ValueSource(org.apache.lucene.queries.function.ValueSource) FunctionValues(org.apache.lucene.queries.function.FunctionValues)

Example 5 with FunctionValues

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

the class ComparisonBoolFunction method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues lhsVal = this.lhs.getValues(context, readerContext);
    final FunctionValues rhsVal = this.rhs.getValues(context, readerContext);
    final String compLabel = this.name();
    return new BoolDocValues(this) {

        @Override
        public boolean boolVal(int doc) throws IOException {
            return compare(doc, lhsVal, rhsVal);
        }

        @Override
        public String toString(int doc) throws IOException {
            return compLabel + "(" + lhsVal.toString(doc) + "," + rhsVal.toString(doc) + ")";
        }

        @Override
        public boolean exists(int doc) throws IOException {
            return lhsVal.exists(doc) && rhsVal.exists(doc);
        }
    };
}
Also used : BoolDocValues(org.apache.lucene.queries.function.docvalues.BoolDocValues) FunctionValues(org.apache.lucene.queries.function.FunctionValues)

Aggregations

FunctionValues (org.apache.lucene.queries.function.FunctionValues)45 ValueSource (org.apache.lucene.queries.function.ValueSource)10 DoubleDocValues (org.apache.lucene.queries.function.docvalues.DoubleDocValues)9 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)7 Map (java.util.Map)6 LeafReader (org.apache.lucene.index.LeafReader)6 NumericDocValues (org.apache.lucene.index.NumericDocValues)6 IOException (java.io.IOException)5 FloatDocValues (org.apache.lucene.queries.function.docvalues.FloatDocValues)5 Explanation (org.apache.lucene.search.Explanation)4 ArrayList (java.util.ArrayList)3 Terms (org.apache.lucene.index.Terms)3 BoolDocValues (org.apache.lucene.queries.function.docvalues.BoolDocValues)3 BytesRef (org.apache.lucene.util.BytesRef)3 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)3 Point (org.locationtech.spatial4j.shape.Point)3 Rectangle (org.locationtech.spatial4j.shape.Rectangle)3 Date (java.util.Date)2 TermsEnum (org.apache.lucene.index.TermsEnum)2 SimpleFloatFunction (org.apache.lucene.queries.function.valuesource.SimpleFloatFunction)2