Search in sources :

Example 11 with FloatPoint

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 })));
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) DoublePoint(org.apache.lucene.document.DoublePoint) LongPoint(org.apache.lucene.document.LongPoint) Document(org.apache.lucene.document.Document)

Example 12 with FloatPoint

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);
}
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)

Example 13 with FloatPoint

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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) StoredField(org.apache.lucene.document.StoredField) FloatPoint(org.apache.lucene.document.FloatPoint) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 14 with FloatPoint

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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) StoredField(org.apache.lucene.document.StoredField) FloatPoint(org.apache.lucene.document.FloatPoint) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 15 with FloatPoint

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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) StoredField(org.apache.lucene.document.StoredField) FloatPoint(org.apache.lucene.document.FloatPoint) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

FloatPoint (org.apache.lucene.document.FloatPoint)22 Document (org.apache.lucene.document.Document)19 IntPoint (org.apache.lucene.document.IntPoint)15 LongPoint (org.apache.lucene.document.LongPoint)15 DoublePoint (org.apache.lucene.document.DoublePoint)14 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)14 Directory (org.apache.lucene.store.Directory)14 IndexReader (org.apache.lucene.index.IndexReader)13 IndexWriter (org.apache.lucene.index.IndexWriter)10 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)10 IndexSearcher (org.apache.lucene.search.IndexSearcher)9 BinaryPoint (org.apache.lucene.document.BinaryPoint)7 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)6 BytesRef (org.apache.lucene.util.BytesRef)6 StoredField (org.apache.lucene.document.StoredField)5 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)5 SortField (org.apache.lucene.search.SortField)5 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)4 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)4 Query (org.apache.lucene.search.Query)4