Search in sources :

Example 1 with HalfFloatPoint

use of org.apache.lucene.document.HalfFloatPoint in project elasticsearch by elastic.

the class NumberFieldTypeTests method testHalfFloatRange.

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();
    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, false);
        Query halfFloatQ = NumberFieldMapper.NumberType.HALF_FLOAT.rangeQuery("half_float", l, u, includeLower, includeUpper, false);
        assertEquals(searcher.count(floatQ), searcher.count(halfFloatQ));
    }
    IOUtils.close(reader, dir);
}
Also used : HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint) IndexSearcher(org.apache.lucene.search.IndexSearcher) FloatPoint(org.apache.lucene.document.FloatPoint) HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) Document(org.apache.lucene.document.Document) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) HalfFloatPoint(org.apache.lucene.document.HalfFloatPoint) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

Document (org.apache.lucene.document.Document)1 FloatPoint (org.apache.lucene.document.FloatPoint)1 HalfFloatPoint (org.apache.lucene.document.HalfFloatPoint)1 IntPoint (org.apache.lucene.document.IntPoint)1 LongPoint (org.apache.lucene.document.LongPoint)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 IndexOrDocValuesQuery (org.apache.lucene.search.IndexOrDocValuesQuery)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 Query (org.apache.lucene.search.Query)1 Directory (org.apache.lucene.store.Directory)1