Example usage for org.apache.lucene.util NumericUtils sortableIntToFloat

List of usage examples for org.apache.lucene.util NumericUtils sortableIntToFloat

Introduction

In this page you can find the example usage for org.apache.lucene.util NumericUtils sortableIntToFloat.

Prototype

public static float sortableIntToFloat(int encoded) 

Source Link

Document

Converts a sortable int back to a float.

Usage

From source file:com.github.flaxsearch.util.BytesRefUtils.java

License:Apache License

private static Function<BytesRef, String> getEncoder(String type) {
    switch (type.toLowerCase(Locale.ROOT)) {
    case "base64":
        return b -> new String(Base64.getUrlEncoder().encode(b.bytes), Charset.defaultCharset());
    case "utf8":
        return BytesRef::utf8ToString;
    case "int":
        return b -> Integer.toString(LegacyNumericUtils.prefixCodedToInt(b));
    case "long":
        return b -> Long.toString(LegacyNumericUtils.prefixCodedToLong(b));
    case "float":
        return b -> Float.toString(NumericUtils.sortableIntToFloat(LegacyNumericUtils.prefixCodedToInt(b)));
    case "double":
        return b -> Double.toString(NumericUtils.sortableLongToDouble(LegacyNumericUtils.prefixCodedToLong(b)));
    default:/*  w w w .  j a  v a  2 s. co m*/
        throw new IllegalArgumentException("Unknown encoder type: " + type);
    }
}

From source file:org.alfresco.solr.transformer.DocValueDocTransformer.java

License:Open Source License

@Override
public void transform(SolrDocument doc, int docid, float score) throws IOException {
    for (String fieldName : context.getSearcher().getFieldNames()) {
        SchemaField schemaField = context.getSearcher().getSchema().getFieldOrNull(fieldName);
        if (schemaField != null) {
            if (schemaField.hasDocValues()) {
                SortedDocValues sortedDocValues = context.getSearcher().getSlowAtomicReader()
                        .getSortedDocValues(fieldName);
                if (sortedDocValues != null) {
                    int ordinal = sortedDocValues.getOrd(docid);
                    if (ordinal > -1) {
                        doc.removeFields(fieldName);
                        String alfrescoFieldName = AlfrescoSolrDataModel.getInstance()
                                .getAlfrescoPropertyFromSchemaField(fieldName);
                        doc.removeFields(alfrescoFieldName);
                        doc.addField(alfrescoFieldName, schemaField.getType().toObject(schemaField,
                                sortedDocValues.lookupOrd(ordinal)));
                    }/* w w w .j  a v a  2 s.co m*/
                }

                SortedSetDocValues sortedSetDocValues = context.getSearcher().getSlowAtomicReader()
                        .getSortedSetDocValues(fieldName);
                if (sortedSetDocValues != null) {
                    ArrayList<Object> newValues = new ArrayList<Object>();
                    sortedSetDocValues.setDocument(docid);
                    long ordinal;
                    while ((ordinal = sortedSetDocValues.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS) {
                        newValues.add(schemaField.getType().toObject(schemaField,
                                sortedSetDocValues.lookupOrd(ordinal)));
                    }
                    doc.removeFields(fieldName);
                    String alfrescoFieldName = AlfrescoSolrDataModel.getInstance()
                            .getAlfrescoPropertyFromSchemaField(fieldName);
                    doc.removeFields(alfrescoFieldName);
                    doc.addField(alfrescoFieldName, newValues);
                }

                BinaryDocValues binaryDocValues = context.getSearcher().getSlowAtomicReader()
                        .getBinaryDocValues(fieldName);
                if (binaryDocValues != null) {
                    doc.removeFields(fieldName);
                    String alfrescoFieldName = AlfrescoSolrDataModel.getInstance()
                            .getAlfrescoPropertyFromSchemaField(fieldName);
                    doc.removeFields(alfrescoFieldName);
                    doc.addField(alfrescoFieldName,
                            schemaField.getType().toObject(schemaField, binaryDocValues.get(docid)));
                }

                if (schemaField.getType().getNumericType() != null) {
                    NumericDocValues numericDocValues = context.getSearcher().getSlowAtomicReader()
                            .getNumericDocValues(fieldName);
                    if (numericDocValues != null) {
                        doc.removeFields(fieldName);
                        String alfrescoFieldName = AlfrescoSolrDataModel.getInstance()
                                .getAlfrescoPropertyFromSchemaField(fieldName);
                        doc.removeFields(alfrescoFieldName);
                        switch (schemaField.getType().getNumericType()) {
                        case DOUBLE:
                            doc.addField(alfrescoFieldName,
                                    Double.longBitsToDouble(numericDocValues.get(docid)));
                            break;
                        case FLOAT:
                            doc.addField(alfrescoFieldName,
                                    Float.intBitsToFloat((int) numericDocValues.get(docid)));
                            break;
                        case INT:
                            doc.addField(alfrescoFieldName, (int) numericDocValues.get(docid));
                            break;
                        case LONG:
                            doc.addField(alfrescoFieldName, numericDocValues.get(docid));
                            break;
                        }
                    }

                    SortedNumericDocValues sortedNumericDocValues = context.getSearcher().getSlowAtomicReader()
                            .getSortedNumericDocValues(fieldName);
                    if (sortedNumericDocValues != null) {
                        sortedNumericDocValues.setDocument(docid);
                        doc.removeFields(fieldName);
                        String alfrescoFieldName = AlfrescoSolrDataModel.getInstance()
                                .getAlfrescoPropertyFromSchemaField(fieldName);
                        doc.removeFields(alfrescoFieldName);
                        ArrayList<Object> newValues = new ArrayList<Object>(sortedNumericDocValues.count());
                        if (sortedNumericDocValues.count() > 0) {

                            for (int i = 0; i < sortedNumericDocValues.count(); i++) {
                                switch (schemaField.getType().getNumericType()) {
                                case DOUBLE:
                                    newValues.add(NumericUtils
                                            .sortableLongToDouble(sortedNumericDocValues.valueAt(i)));
                                    break;
                                case FLOAT:
                                    newValues.add(NumericUtils
                                            .sortableIntToFloat((int) sortedNumericDocValues.valueAt(i)));
                                    break;
                                case INT:
                                    newValues.add((int) sortedNumericDocValues.valueAt(i));
                                    break;
                                case LONG:
                                    newValues.add(sortedNumericDocValues.valueAt(i));
                                    break;

                                }
                            }
                        }
                        doc.addField(alfrescoFieldName, newValues);

                    }
                }
            }
        }
    }

}

From source file:org.apache.blur.analysis.type.FloatFieldTypeDefinition.java

License:Apache License

@Override
public String readTerm(BytesRef byteRef) {
    if (NumericUtils.getPrefixCodedIntShift(byteRef) == 0)
        return NumericUtils.sortableIntToFloat(NumericUtils.prefixCodedToInt(byteRef)) + "";
    return null;//from   w  w  w .j  a  v a 2  s  . c o  m
}

From source file:org.apache.solr.analytics.function.field.FloatField.java

License:Apache License

@Override
public void collect(int doc) throws IOException {
    exists = docValues.advanceExact(doc);
    if (exists) {
        value = NumericUtils.sortableIntToFloat((int) docValues.longValue());
    }/*www.  ja v a 2  s.  c om*/
}

From source file:org.apache.solr.analytics.function.field.FloatMultiPointField.java

License:Apache License

@Override
public void collect(int doc) throws IOException {
    if (docValues.advanceExact(doc)) {
        count = docValues.docValueCount();
        resizeEmptyValues(count);/*from   w ww. j a v a 2s  .co m*/
        for (int i = 0; i < count; ++i) {
            values[i] = NumericUtils.sortableIntToFloat((int) docValues.nextValue());
        }
    } else {
        count = 0;
    }
}

From source file:org.apache.solr.analytics.function.field.FloatMultiTrieField.java

License:Apache License

@Override
public void collect(int doc) throws IOException {
    count = 0;//from w  ww .  j a  v a 2s. c  om
    if (docValues.advanceExact(doc)) {
        int term;
        while ((term = (int) docValues.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS) {
            if (count == values.length) {
                resizeValues();
            }
            values[count++] = NumericUtils
                    .sortableIntToFloat(LegacyNumericUtils.prefixCodedToInt(docValues.lookupOrd(term)));
        }
    }
}

From source file:org.apache.solr.handler.component.SortedNumericStatsValues.java

License:Apache License

private Number toCorrectType(long value) {
    switch (numberType) {
    case INTEGER:
    case LONG://from  www  .j a  v a 2 s.c o  m
        return value;
    case FLOAT:
        return NumericUtils.sortableIntToFloat((int) value);
    case DOUBLE:
        return NumericUtils.sortableLongToDouble(value);
    default:
        throw new AssertionError("Unsupported number type");
    }
}

From source file:org.apache.solr.legacy.TestLegacyNumericUtils.java

License:Apache License

public void testFloats() throws Exception {
    float[] vals = new float[] { Float.NEGATIVE_INFINITY, -2.3E25f, -1.0E15f, -1.0f, -1.0E-1f, -1.0E-2f, -0.0f,
            +0.0f, 1.0E-2f, 1.0E-1f, 1.0f, 1.0E15f, 2.3E25f, Float.POSITIVE_INFINITY, Float.NaN };
    int[] intVals = new int[vals.length];

    // check forward and back conversion
    for (int i = 0; i < vals.length; i++) {
        intVals[i] = NumericUtils.floatToSortableInt(vals[i]);
        assertTrue("forward and back conversion should generate same double",
                Float.compare(vals[i], NumericUtils.sortableIntToFloat(intVals[i])) == 0);
    }//from w  ww.j  a  v  a2  s  .c  om

    // check sort order (prefixVals should be ascending)
    for (int i = 1; i < intVals.length; i++) {
        assertTrue("check sort order", intVals[i - 1] < intVals[i]);
    }
}

From source file:org.apache.solr.legacy.TestLegacyTerms.java

License:Apache License

public void testFloatFieldMinMax() throws Exception {
    Directory dir = newDirectory();/*  w ww.jav  a 2 s. c o  m*/
    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
    int numDocs = atLeast(100);
    float minValue = Float.POSITIVE_INFINITY;
    float maxValue = Float.NEGATIVE_INFINITY;
    for (int i = 0; i < numDocs; i++) {
        Document doc = new Document();
        float num = random().nextFloat();
        minValue = Math.min(num, minValue);
        maxValue = Math.max(num, maxValue);
        doc.add(new LegacyFloatField("field", num, Field.Store.NO));
        w.addDocument(doc);
    }

    IndexReader r = w.getReader();
    Terms terms = MultiFields.getTerms(r, "field");
    assertEquals(minValue, NumericUtils.sortableIntToFloat(LegacyNumericUtils.getMinInt(terms)), 0.0f);
    assertEquals(maxValue, NumericUtils.sortableIntToFloat(LegacyNumericUtils.getMaxInt(terms)), 0.0f);

    r.close();
    w.close();
    dir.close();
}

From source file:org.apache.solr.legacy.TestNumericRangeQuery32.java

License:Apache License

/** we fake a float test using int2float conversion of LegacyNumericUtils */
private void testFloatRange(int precisionStep) throws Exception {
    final String field = "ascfield" + precisionStep;
    final int lower = -1000, upper = +2000;

    Query tq = LegacyNumericRangeQuery.newFloatRange(field, precisionStep,
            NumericUtils.sortableIntToFloat(lower), NumericUtils.sortableIntToFloat(upper), true, true);
    TopDocs tTopDocs = searcher.search(tq, 1);
    assertEquals("Returned count of range query must be equal to inclusive range length", upper - lower + 1,
            tTopDocs.totalHits);//  ww w  . j av  a  2  s  .c o m
}