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

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

Introduction

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

Prototype

public static int floatToSortableInt(float value) 

Source Link

Document

Converts a float value to a sortable signed int.

Usage

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

License:Apache License

private static Function<String, BytesRef> getDecoder(String type) {
    switch (type.toLowerCase(Locale.ROOT)) {
    case "base64":
        return s -> new BytesRef(Base64.getUrlDecoder().decode(s.getBytes(Charset.defaultCharset())));
    case "utf8":
        return BytesRef::new;
    case "int":
        return s -> {
            BytesRefBuilder builder = new BytesRefBuilder();
            LegacyNumericUtils.intToPrefixCoded(Integer.parseInt(s), 0, builder);
            return builder.get();
        };//  w  ww.  ja  va  2  s  .c  o  m
    case "long":
        return s -> {
            BytesRefBuilder builder = new BytesRefBuilder();
            LegacyNumericUtils.longToPrefixCoded(Long.parseLong(s), 0, builder);
            return builder.get();
        };
    case "float":
        return s -> {
            BytesRefBuilder builder = new BytesRefBuilder();
            LegacyNumericUtils.intToPrefixCoded(NumericUtils.floatToSortableInt(Float.parseFloat(s)), 0,
                    builder);
            return builder.get();
        };
    case "double":
        return s -> {
            BytesRefBuilder builder = new BytesRefBuilder();
            LegacyNumericUtils.longToPrefixCoded(NumericUtils.doubleToSortableLong(Double.parseDouble(s)), 0,
                    builder);
            return builder.get();
        };
    default:
        throw new IllegalArgumentException("Unknown decoder type: " + type);
    }
}

From source file:com.querydsl.lucene4.LuceneSerializer.java

License:Apache License

private BytesRef convertNumber(Number number) {
    if (Integer.class.isInstance(number) || Byte.class.isInstance(number) || Short.class.isInstance(number)) {
        BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT);
        NumericUtils.intToPrefixCoded(number.intValue(), 0, bytes);
        return bytes;
    } else if (Double.class.isInstance(number) || BigDecimal.class.isInstance(number)) {
        BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_LONG);
        long l = NumericUtils.doubleToSortableLong(number.doubleValue());
        NumericUtils.longToPrefixCoded(l, 0, bytes);
        return bytes;
    } else if (Long.class.isInstance(number) || BigInteger.class.isInstance(number)) {
        BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_LONG);
        NumericUtils.longToPrefixCoded(number.longValue(), 0, bytes);
        return bytes;
    } else if (Float.class.isInstance(number)) {
        BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_INT);
        int i = NumericUtils.floatToSortableInt(number.floatValue());
        NumericUtils.intToPrefixCoded(i, 0, bytes);
        return bytes;
    } else {// ww  w .  j  a v a2 s  .c  o  m
        throw new IllegalArgumentException("Unsupported numeric type " + number.getClass().getName());
    }
}

From source file:com.querydsl.lucene5.LuceneSerializer.java

License:Apache License

private BytesRef convertNumber(Number number) {
    if (Integer.class.isInstance(number) || Byte.class.isInstance(number) || Short.class.isInstance(number)) {
        BytesRefBuilder ref = new BytesRefBuilder();
        NumericUtils.intToPrefixCoded(number.intValue(), 0, ref);
        return ref.get();
    } else if (Double.class.isInstance(number) || BigDecimal.class.isInstance(number)) {
        BytesRefBuilder ref = new BytesRefBuilder();
        long l = NumericUtils.doubleToSortableLong(number.doubleValue());
        NumericUtils.longToPrefixCoded(l, 0, ref);
        return ref.get();
    } else if (Long.class.isInstance(number) || BigInteger.class.isInstance(number)) {
        BytesRefBuilder ref = new BytesRefBuilder();
        NumericUtils.longToPrefixCoded(number.longValue(), 0, ref);
        return ref.get();
    } else if (Float.class.isInstance(number)) {
        BytesRefBuilder ref = new BytesRefBuilder();
        int i = NumericUtils.floatToSortableInt(number.floatValue());
        NumericUtils.intToPrefixCoded(i, 0, ref);
        return ref.get();
    } else {/*from ww w  .  j  av a2s.  c o m*/
        throw new IllegalArgumentException("Unsupported numeric type " + number.getClass().getName());
    }
}

From source file:com.qwazr.search.field.SortedFloatDocValuesType.java

License:Apache License

@Override
final public void fillValue(final Object value, final FieldConsumer consumer) {
    if (value instanceof Number)
        consumer.accept(new SortedNumericDocValuesField(fieldName,
                NumericUtils.floatToSortableInt(((Number) value).floatValue())));
    else/*ww  w  .jav a  2  s  . com*/
        consumer.accept(new SortedNumericDocValuesField(fieldName,
                NumericUtils.floatToSortableInt(Float.parseFloat(value.toString()))));
}

From source file:com.qwazr.search.index.BytesRefUtils.java

License:Apache License

final static public BytesRef from(final Float value) {
    if (value == null)
        return new BytesRef(BytesRef.EMPTY_BYTES);
    final BytesRefBuilder builder = new BytesRefBuilder();
    NumericUtils.intToPrefixCoded(NumericUtils.floatToSortableInt(value), 0, builder);
    return builder.toBytesRef();
}

From source file:io.crate.expression.reference.doc.FloatColumnReferenceTest.java

License:Apache License

@Override
protected void insertValues(IndexWriter writer) throws Exception {
    for (float f = -0.5f; f < 10.0f; f++) {
        Document doc = new Document();
        doc.add(new SortedNumericDocValuesField(column, NumericUtils.floatToSortableInt(f)));
        writer.addDocument(doc);/*from w ww .  j a va  2 s .  c o  m*/
    }
}

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

License:Apache License

/**
 * Initializes the token stream with the supplied <code>float</code> value.
 * @param value the value, for which this TokenStream should enumerate tokens.
 * @return this instance, because of this you can use it the following way:
 * <code>new Field(name, new LegacyNumericTokenStream(precisionStep).setFloatValue(value))</code>
 *//* w  w w.java2s. com*/
public LegacyNumericTokenStream setFloatValue(final float value) {
    numericAtt.init(NumericUtils.floatToSortableInt(value), valSize = 32, precisionStep, -precisionStep);
    return this;
}

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);
    }//  w  w  w . jav 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.TestLegacyNumericUtils.java

License:Apache License

public void testSortableFloatNaN() {
    final int plusInf = NumericUtils.floatToSortableInt(Float.POSITIVE_INFINITY);
    for (float nan : FLOAT_NANs) {
        assertTrue(Float.isNaN(nan));
        final int sortable = NumericUtils.floatToSortableInt(nan);
        assertTrue("Float not sorted correctly: " + nan + ", int repr: " + sortable + ", positive inf.: "
                + plusInf, sortable > plusInf);
    }//from ww  w .  j ava2s.  co m
}

From source file:org.apache.solr.request.NumericFacets.java

License:Apache License

public static NamedList<Integer> getCounts(SolrIndexSearcher searcher, DocSet docs, String fieldName,
        int offset, int limit, int mincount, boolean missing, String sort) throws IOException {
    final boolean zeros = mincount <= 0;
    mincount = Math.max(mincount, 1);
    final SchemaField sf = searcher.getSchema().getField(fieldName);
    final FieldType ft = sf.getType();
    final NumericType numericType = ft.getNumericType();
    if (numericType == null) {
        throw new IllegalStateException();
    }//from  ww  w .  j  av a 2 s  .c o  m
    final List<AtomicReaderContext> leaves = searcher.getIndexReader().leaves();

    // 1. accumulate
    final HashTable hashTable = new HashTable();
    final Iterator<AtomicReaderContext> ctxIt = leaves.iterator();
    AtomicReaderContext ctx = null;
    FieldCache.Longs longs = null;
    Bits docsWithField = null;
    int missingCount = 0;
    for (DocIterator docsIt = docs.iterator(); docsIt.hasNext();) {
        final int doc = docsIt.nextDoc();
        if (ctx == null || doc >= ctx.docBase + ctx.reader().maxDoc()) {
            do {
                ctx = ctxIt.next();
            } while (ctx == null || doc >= ctx.docBase + ctx.reader().maxDoc());
            assert doc >= ctx.docBase;
            switch (numericType) {
            case LONG:
                longs = FieldCache.DEFAULT.getLongs(ctx.reader(), fieldName, true);
                break;
            case INT:
                final FieldCache.Ints ints = FieldCache.DEFAULT.getInts(ctx.reader(), fieldName, true);
                longs = new FieldCache.Longs() {
                    @Override
                    public long get(int docID) {
                        return ints.get(docID);
                    }
                };
                break;
            case FLOAT:
                final FieldCache.Floats floats = FieldCache.DEFAULT.getFloats(ctx.reader(), fieldName, true);
                longs = new FieldCache.Longs() {
                    @Override
                    public long get(int docID) {
                        return NumericUtils.floatToSortableInt(floats.get(docID));
                    }
                };
                break;
            case DOUBLE:
                final FieldCache.Doubles doubles = FieldCache.DEFAULT.getDoubles(ctx.reader(), fieldName, true);
                longs = new FieldCache.Longs() {
                    @Override
                    public long get(int docID) {
                        return NumericUtils.doubleToSortableLong(doubles.get(docID));
                    }
                };
                break;
            default:
                throw new AssertionError();
            }
            docsWithField = FieldCache.DEFAULT.getDocsWithField(ctx.reader(), fieldName);
        }
        long v = longs.get(doc - ctx.docBase);
        if (v != 0 || docsWithField.get(doc - ctx.docBase)) {
            hashTable.add(doc, v, 1);
        } else {
            ++missingCount;
        }
    }

    // 2. select top-k facet values
    final int pqSize = limit < 0 ? hashTable.size : Math.min(offset + limit, hashTable.size);
    final PriorityQueue<Entry> pq;
    if (FacetParams.FACET_SORT_COUNT.equals(sort) || FacetParams.FACET_SORT_COUNT_LEGACY.equals(sort)) {
        pq = new PriorityQueue<Entry>(pqSize) {
            @Override
            protected boolean lessThan(Entry a, Entry b) {
                if (a.count < b.count || (a.count == b.count && a.bits > b.bits)) {
                    return true;
                } else {
                    return false;
                }
            }
        };
    } else {
        pq = new PriorityQueue<Entry>(pqSize) {
            @Override
            protected boolean lessThan(Entry a, Entry b) {
                return a.bits > b.bits;
            }
        };
    }
    Entry e = null;
    for (int i = 0; i < hashTable.bits.length; ++i) {
        if (hashTable.counts[i] >= mincount) {
            if (e == null) {
                e = new Entry();
            }
            e.bits = hashTable.bits[i];
            e.count = hashTable.counts[i];
            e.docID = hashTable.docIDs[i];
            e = pq.insertWithOverflow(e);
        }
    }

    // 4. build the NamedList
    final ValueSource vs = ft.getValueSource(sf, null);
    final NamedList<Integer> result = new NamedList<Integer>();

    // This stuff is complicated because if facet.mincount=0, the counts needs
    // to be merged with terms from the terms dict
    if (!zeros || FacetParams.FACET_SORT_COUNT.equals(sort)
            || FacetParams.FACET_SORT_COUNT_LEGACY.equals(sort)) {
        // Only keep items we're interested in
        final Deque<Entry> counts = new ArrayDeque<Entry>();
        while (pq.size() > offset) {
            counts.addFirst(pq.pop());
        }

        // Entries from the PQ first, then using the terms dictionary
        for (Entry entry : counts) {
            final int readerIdx = ReaderUtil.subIndex(entry.docID, leaves);
            final FunctionValues values = vs.getValues(Collections.emptyMap(), leaves.get(readerIdx));
            result.add(values.strVal(entry.docID - leaves.get(readerIdx).docBase), entry.count);
        }

        if (zeros && (limit < 0 || result.size() < limit)) { // need to merge with the term dict
            if (!sf.indexed()) {
                throw new IllegalStateException("Cannot use " + FacetParams.FACET_MINCOUNT + "=0 on field "
                        + sf.getName() + " which is not indexed");
            }
            // Add zeros until there are limit results
            final Set<String> alreadySeen = new HashSet<String>();
            while (pq.size() > 0) {
                Entry entry = pq.pop();
                final int readerIdx = ReaderUtil.subIndex(entry.docID, leaves);
                final FunctionValues values = vs.getValues(Collections.emptyMap(), leaves.get(readerIdx));
                alreadySeen.add(values.strVal(entry.docID - leaves.get(readerIdx).docBase));
            }
            for (int i = 0; i < result.size(); ++i) {
                alreadySeen.add(result.getName(i));
            }
            final Terms terms = searcher.getAtomicReader().terms(fieldName);
            if (terms != null) {
                final String prefixStr = TrieField.getMainValuePrefix(ft);
                final BytesRef prefix;
                if (prefixStr != null) {
                    prefix = new BytesRef(prefixStr);
                } else {
                    prefix = new BytesRef();
                }
                final TermsEnum termsEnum = terms.iterator(null);
                BytesRef term;
                switch (termsEnum.seekCeil(prefix)) {
                case FOUND:
                case NOT_FOUND:
                    term = termsEnum.term();
                    break;
                case END:
                    term = null;
                    break;
                default:
                    throw new AssertionError();
                }
                final CharsRef spare = new CharsRef();
                for (int skipped = hashTable.size; skipped < offset && term != null
                        && StringHelper.startsWith(term, prefix);) {
                    ft.indexedToReadable(term, spare);
                    final String termStr = spare.toString();
                    if (!alreadySeen.contains(termStr)) {
                        ++skipped;
                    }
                    term = termsEnum.next();
                }
                for (; term != null && StringHelper.startsWith(term, prefix)
                        && (limit < 0 || result.size() < limit); term = termsEnum.next()) {
                    ft.indexedToReadable(term, spare);
                    final String termStr = spare.toString();
                    if (!alreadySeen.contains(termStr)) {
                        result.add(termStr, 0);
                    }
                }
            }
        }
    } else {
        // sort=index, mincount=0 and we have less than limit items
        // => Merge the PQ and the terms dictionary on the fly
        if (!sf.indexed()) {
            throw new IllegalStateException("Cannot use " + FacetParams.FACET_SORT + "="
                    + FacetParams.FACET_SORT_INDEX + " on a field which is not indexed");
        }
        final Map<String, Integer> counts = new HashMap<String, Integer>();
        while (pq.size() > 0) {
            final Entry entry = pq.pop();
            final int readerIdx = ReaderUtil.subIndex(entry.docID, leaves);
            final FunctionValues values = vs.getValues(Collections.emptyMap(), leaves.get(readerIdx));
            counts.put(values.strVal(entry.docID - leaves.get(readerIdx).docBase), entry.count);
        }
        final Terms terms = searcher.getAtomicReader().terms(fieldName);
        if (terms != null) {
            final String prefixStr = TrieField.getMainValuePrefix(ft);
            final BytesRef prefix;
            if (prefixStr != null) {
                prefix = new BytesRef(prefixStr);
            } else {
                prefix = new BytesRef();
            }
            final TermsEnum termsEnum = terms.iterator(null);
            BytesRef term;
            switch (termsEnum.seekCeil(prefix)) {
            case FOUND:
            case NOT_FOUND:
                term = termsEnum.term();
                break;
            case END:
                term = null;
                break;
            default:
                throw new AssertionError();
            }
            final CharsRef spare = new CharsRef();
            for (int i = 0; i < offset && term != null && StringHelper.startsWith(term, prefix); ++i) {
                term = termsEnum.next();
            }
            for (; term != null && StringHelper.startsWith(term, prefix)
                    && (limit < 0 || result.size() < limit); term = termsEnum.next()) {
                ft.indexedToReadable(term, spare);
                final String termStr = spare.toString();
                Integer count = counts.get(termStr);
                if (count == null) {
                    count = 0;
                }
                result.add(termStr, count);
            }
        }
    }

    if (missing) {
        result.add(null, missingCount);
    }
    return result;
}