Example usage for org.apache.lucene.document HalfFloatPoint sortableShortToHalfFloat

List of usage examples for org.apache.lucene.document HalfFloatPoint sortableShortToHalfFloat

Introduction

In this page you can find the example usage for org.apache.lucene.document HalfFloatPoint sortableShortToHalfFloat.

Prototype

public static float sortableShortToHalfFloat(short bits) 

Source Link

Document

Convert short bits to a half-float value that maintains ordering.

Usage

From source file:org.elasticsearch.index.mapper.NumberFieldTypeTests.java

License:Apache License

public void testHalfFloatRange() throws IOException {
    // make sure the accuracy loss of half floats only occurs at index time
    // this test checks that searching half floats yields the same results as
    // searching floats that are rounded to the closest half float
    Directory dir = newDirectory();//from  w  w w.  j av  a 2s  .  co m
    IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
    final int numDocs = 10000;
    for (int i = 0; i < numDocs; ++i) {
        Document doc = new Document();
        float value = (randomFloat() * 2 - 1) * 70000;
        float rounded = HalfFloatPoint.sortableShortToHalfFloat(HalfFloatPoint.halfFloatToSortableShort(value));
        doc.add(new HalfFloatPoint("half_float", value));
        doc.add(new FloatPoint("float", rounded));
        w.addDocument(doc);
    }
    final DirectoryReader reader = DirectoryReader.open(w);
    w.close();
    IndexSearcher searcher = newSearcher(reader);
    final int numQueries = 1000;
    for (int i = 0; i < numQueries; ++i) {
        float l = (randomFloat() * 2 - 1) * 70000;
        float u = (randomFloat() * 2 - 1) * 70000;
        boolean includeLower = randomBoolean();
        boolean includeUpper = randomBoolean();
        Query floatQ = NumberFieldMapper.NumberType.FLOAT.rangeQuery("float", l, u, includeLower, includeUpper);
        Query halfFloatQ = NumberFieldMapper.NumberType.HALF_FLOAT.rangeQuery("half_float", l, u, includeLower,
                includeUpper);
        assertEquals(searcher.count(floatQ), searcher.count(halfFloatQ));
    }
    IOUtils.close(reader, dir);
}