Search in sources :

Example 6 with LegacyFloatField

use of org.apache.solr.legacy.LegacyFloatField in project lucene-solr by apache.

the class TestFieldCacheSort method testLegacyFloatMissing.

/** Tests sorting on type legacy float with a missing value */
public void testLegacyFloatMissing() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new LegacyFloatField("value", -1.3f, Field.Store.YES));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new LegacyFloatField("value", 4.2f, Field.Store.YES));
    writer.addDocument(doc);
    IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", Type.LEGACY_FLOAT));
    writer.close();
    IndexSearcher searcher = newSearcher(ir);
    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) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) LegacyFloatField(org.apache.solr.legacy.LegacyFloatField) 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 7 with LegacyFloatField

use of org.apache.solr.legacy.LegacyFloatField in project lucene-solr by apache.

the class TestFieldCacheSort method testLegacyFloat.

/** Tests sorting on type legacy float */
public void testLegacyFloat() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(new LegacyFloatField("value", 30.1f, Field.Store.YES));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new LegacyFloatField("value", -1.3f, Field.Store.YES));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new LegacyFloatField("value", 4.2f, Field.Store.YES));
    writer.addDocument(doc);
    IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", Type.LEGACY_FLOAT));
    writer.close();
    IndexSearcher searcher = newSearcher(ir);
    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) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) LegacyFloatField(org.apache.solr.legacy.LegacyFloatField) 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 8 with LegacyFloatField

use of org.apache.solr.legacy.LegacyFloatField in project lucene-solr by apache.

the class TrieDateFieldSource method createField.

@Override
public IndexableField createField(SchemaField field, Object value) {
    boolean indexed = field.indexed();
    boolean stored = field.stored();
    boolean docValues = field.hasDocValues();
    if (!indexed && !stored && !docValues) {
        if (log.isTraceEnabled())
            log.trace("Ignoring unindexed/unstored field: " + field);
        return null;
    }
    LegacyFieldType ft = new LegacyFieldType();
    ft.setStored(stored);
    ft.setTokenized(true);
    ft.setOmitNorms(field.omitNorms());
    ft.setIndexOptions(field.indexOptions());
    switch(type) {
        case INTEGER:
            ft.setNumericType(LegacyNumericType.INT);
            break;
        case FLOAT:
            ft.setNumericType(LegacyNumericType.FLOAT);
            break;
        case LONG:
            ft.setNumericType(LegacyNumericType.LONG);
            break;
        case DOUBLE:
            ft.setNumericType(LegacyNumericType.DOUBLE);
            break;
        case DATE:
            ft.setNumericType(LegacyNumericType.LONG);
            break;
        default:
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + type);
    }
    ft.setNumericPrecisionStep(precisionStep);
    final org.apache.lucene.document.Field f;
    switch(type) {
        case INTEGER:
            int i = (value instanceof Number) ? ((Number) value).intValue() : Integer.parseInt(value.toString());
            f = new LegacyIntField(field.getName(), i, ft);
            break;
        case FLOAT:
            float fl = (value instanceof Number) ? ((Number) value).floatValue() : Float.parseFloat(value.toString());
            f = new LegacyFloatField(field.getName(), fl, ft);
            break;
        case LONG:
            long l = (value instanceof Number) ? ((Number) value).longValue() : Long.parseLong(value.toString());
            f = new LegacyLongField(field.getName(), l, ft);
            break;
        case DOUBLE:
            double d = (value instanceof Number) ? ((Number) value).doubleValue() : Double.parseDouble(value.toString());
            f = new LegacyDoubleField(field.getName(), d, ft);
            break;
        case DATE:
            Date date = (value instanceof Date) ? ((Date) value) : DateMathParser.parseMath(null, value.toString());
            f = new LegacyLongField(field.getName(), date.getTime(), ft);
            break;
        default:
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + type);
    }
    return f;
}
Also used : LegacyFieldType(org.apache.solr.legacy.LegacyFieldType) LegacyDoubleField(org.apache.solr.legacy.LegacyDoubleField) Date(java.util.Date) MutableValueDate(org.apache.lucene.util.mutable.MutableValueDate) LegacyLongField(org.apache.solr.legacy.LegacyLongField) LegacyFloatField(org.apache.solr.legacy.LegacyFloatField) SolrException(org.apache.solr.common.SolrException) LegacyIntField(org.apache.solr.legacy.LegacyIntField)

Aggregations

LegacyFloatField (org.apache.solr.legacy.LegacyFloatField)8 Document (org.apache.lucene.document.Document)7 IndexReader (org.apache.lucene.index.IndexReader)6 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)6 Directory (org.apache.lucene.store.Directory)5 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)4 Sort (org.apache.lucene.search.Sort)4 SortField (org.apache.lucene.search.SortField)4 TopDocs (org.apache.lucene.search.TopDocs)4 LegacyIntField (org.apache.solr.legacy.LegacyIntField)3 LegacyDoubleField (org.apache.solr.legacy.LegacyDoubleField)2 LegacyLongField (org.apache.solr.legacy.LegacyLongField)2 Date (java.util.Date)1 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)1 Field (org.apache.lucene.document.Field)1 FieldType (org.apache.lucene.document.FieldType)1 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)1 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)1 TextField (org.apache.lucene.document.TextField)1