use of org.apache.lucene.document.FloatPoint in project lucene-solr by apache.
the class TestMemoryIndex method test2DPoints.
public void test2DPoints() throws Exception {
Document doc = new Document();
doc.add(new IntPoint("ints", 0, -100));
doc.add(new IntPoint("ints", 20, 20));
doc.add(new IntPoint("ints", 100, -100));
doc.add(new LongPoint("longs", 0L, -100L));
doc.add(new LongPoint("longs", 20L, 20L));
doc.add(new LongPoint("longs", 100L, -100L));
doc.add(new FloatPoint("floats", 0F, -100F));
doc.add(new FloatPoint("floats", 20F, 20F));
doc.add(new FloatPoint("floats", 100F, -100F));
doc.add(new DoublePoint("doubles", 0D, -100D));
doc.add(new DoublePoint("doubles", 20D, 20D));
doc.add(new DoublePoint("doubles", 100D, -100D));
MemoryIndex mi = MemoryIndex.fromDocument(doc, analyzer);
IndexSearcher s = mi.createSearcher();
assertEquals(1, s.count(IntPoint.newRangeQuery("ints", new int[] { 10, 10 }, new int[] { 30, 30 })));
assertEquals(1, s.count(LongPoint.newRangeQuery("longs", new long[] { 10L, 10L }, new long[] { 30L, 30L })));
assertEquals(1, s.count(FloatPoint.newRangeQuery("floats", new float[] { 10F, 10F }, new float[] { 30F, 30F })));
assertEquals(1, s.count(DoublePoint.newRangeQuery("doubles", new double[] { 10D, 10D }, new double[] { 30D, 30D })));
}
use of org.apache.lucene.document.FloatPoint 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);
}
use of org.apache.lucene.document.FloatPoint in project lucene-solr by apache.
the class TestFieldCacheSort method testFloat.
/** Tests sorting on type float */
public void testFloat() throws IOException {
Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document doc = new Document();
doc.add(new FloatPoint("value", 30.1f));
doc.add(new StoredField("value", 30.1f));
writer.addDocument(doc);
doc = new Document();
doc.add(new FloatPoint("value", -1.3f));
doc.add(new StoredField("value", -1.3f));
writer.addDocument(doc);
doc = new Document();
doc.add(new FloatPoint("value", 4.2f));
doc.add(new StoredField("value", 4.2f));
writer.addDocument(doc);
IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", Type.FLOAT_POINT));
writer.close();
IndexSearcher searcher = newSearcher(ir, false);
Sort sort = new Sort(new SortField("value", SortField.Type.FLOAT));
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
assertEquals(3, td.totalHits);
// numeric order
assertEquals("-1.3", searcher.doc(td.scoreDocs[0].doc).get("value"));
assertEquals("4.2", searcher.doc(td.scoreDocs[1].doc).get("value"));
assertEquals("30.1", searcher.doc(td.scoreDocs[2].doc).get("value"));
TestUtil.checkReader(ir);
ir.close();
dir.close();
}
use of org.apache.lucene.document.FloatPoint in project lucene-solr by apache.
the class TestFieldCacheSort method testFloatReverse.
/** Tests sorting on type float in reverse */
public void testFloatReverse() throws IOException {
Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document doc = new Document();
doc.add(new FloatPoint("value", 30.1f));
doc.add(new StoredField("value", 30.1f));
writer.addDocument(doc);
doc = new Document();
doc.add(new FloatPoint("value", -1.3f));
doc.add(new StoredField("value", -1.3f));
writer.addDocument(doc);
doc = new Document();
doc.add(new FloatPoint("value", 4.2f));
doc.add(new StoredField("value", 4.2f));
writer.addDocument(doc);
IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", Type.FLOAT_POINT));
writer.close();
IndexSearcher searcher = newSearcher(ir, false);
Sort sort = new Sort(new SortField("value", SortField.Type.FLOAT, true));
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
assertEquals(3, td.totalHits);
// reverse numeric order
assertEquals("30.1", searcher.doc(td.scoreDocs[0].doc).get("value"));
assertEquals("4.2", searcher.doc(td.scoreDocs[1].doc).get("value"));
assertEquals("-1.3", searcher.doc(td.scoreDocs[2].doc).get("value"));
TestUtil.checkReader(ir);
ir.close();
dir.close();
}
use of org.apache.lucene.document.FloatPoint in project lucene-solr by apache.
the class TestFieldCacheSort method testFloatMissing.
/** Tests sorting on type float with a missing value */
public void testFloatMissing() throws IOException {
Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document doc = new Document();
writer.addDocument(doc);
doc = new Document();
doc.add(new FloatPoint("value", -1.3f));
doc.add(new StoredField("value", -1.3f));
writer.addDocument(doc);
doc = new Document();
doc.add(new FloatPoint("value", 4.2f));
doc.add(new StoredField("value", 4.2f));
writer.addDocument(doc);
IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", Type.FLOAT_POINT));
writer.close();
IndexSearcher searcher = newSearcher(ir, false);
Sort sort = new Sort(new SortField("value", SortField.Type.FLOAT));
TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort);
assertEquals(3, td.totalHits);
// null is treated as 0
assertEquals("-1.3", searcher.doc(td.scoreDocs[0].doc).get("value"));
assertNull(searcher.doc(td.scoreDocs[1].doc).get("value"));
assertEquals("4.2", searcher.doc(td.scoreDocs[2].doc).get("value"));
TestUtil.checkReader(ir);
ir.close();
dir.close();
}
Aggregations