Search in sources :

Example 1 with IntRangeField

use of org.apache.lucene.document.IntRangeField in project carbondata by apache.

the class LuceneIndexWriter method addField.

private static void addField(Document doc, Object key, String fieldName, Field.Store store) {
    // get field name
    if (key instanceof Byte) {
        // byte type , use int range to deal with byte, lucene has no byte type
        byte value = (Byte) key;
        IntRangeField field = new IntRangeField(fieldName, new int[] { Byte.MIN_VALUE }, new int[] { Byte.MAX_VALUE });
        field.setIntValue(value);
        doc.add(field);
        // if need store it , add StoredField
        if (store == Field.Store.YES) {
            doc.add(new StoredField(fieldName, (int) value));
        }
    } else if (key instanceof Short) {
        // short type , use int range to deal with short type, lucene has no short type
        short value = (Short) key;
        IntRangeField field = new IntRangeField(fieldName, new int[] { Short.MIN_VALUE }, new int[] { Short.MAX_VALUE });
        field.setShortValue(value);
        doc.add(field);
        // if need store it , add StoredField
        if (store == Field.Store.YES) {
            doc.add(new StoredField(fieldName, (int) value));
        }
    } else if (key instanceof Integer) {
        // int type , use int point to deal with int type
        int value = (Integer) key;
        doc.add(new IntPoint(fieldName, new int[] { value }));
        // if need store it , add StoredField
        if (store == Field.Store.YES) {
            doc.add(new StoredField(fieldName, value));
        }
    } else if (key instanceof Long) {
        // long type , use long point to deal with long type
        long value = (Long) key;
        doc.add(new LongPoint(fieldName, new long[] { value }));
        // if need store it , add StoredField
        if (store == Field.Store.YES) {
            doc.add(new StoredField(fieldName, value));
        }
    } else if (key instanceof Float) {
        float value = (Float) key;
        doc.add(new FloatPoint(fieldName, new float[] { value }));
        if (store == Field.Store.YES) {
            doc.add(new FloatPoint(fieldName, value));
        }
    } else if (key instanceof Double) {
        double value = (Double) key;
        doc.add(new DoublePoint(fieldName, new double[] { value }));
        if (store == Field.Store.YES) {
            doc.add(new DoublePoint(fieldName, value));
        }
    } else if (key instanceof String) {
        String strValue = (String) key;
        doc.add(new TextField(fieldName, strValue, store));
    } else if (key instanceof Boolean) {
        boolean value = (Boolean) key;
        IntRangeField field = new IntRangeField(fieldName, new int[] { 0 }, new int[] { 1 });
        field.setIntValue(value ? 1 : 0);
        doc.add(field);
        if (store == Field.Store.YES) {
            doc.add(new StoredField(fieldName, value ? 1 : 0));
        }
    }
}
Also used : IntRangeField(org.apache.lucene.document.IntRangeField) LongPoint(org.apache.lucene.document.LongPoint) LongPoint(org.apache.lucene.document.LongPoint) DoublePoint(org.apache.lucene.document.DoublePoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) IntPoint(org.apache.lucene.document.IntPoint) StoredField(org.apache.lucene.document.StoredField) FloatPoint(org.apache.lucene.document.FloatPoint) DoublePoint(org.apache.lucene.document.DoublePoint) TextField(org.apache.lucene.document.TextField)

Example 2 with IntRangeField

use of org.apache.lucene.document.IntRangeField in project carbondata by apache.

the class LuceneDataMapWriter method addField.

private boolean addField(Document doc, ColumnPage page, int rowId, Field.Store store) {
    // get field name
    String fieldName = page.getColumnSpec().getFieldName();
    // get field type
    DataType type = page.getDataType();
    if (type == DataTypes.BYTE) {
        // byte type , use int range to deal with byte, lucene has no byte type
        byte value = page.getByte(rowId);
        IntRangeField field = new IntRangeField(fieldName, new int[] { Byte.MIN_VALUE }, new int[] { Byte.MAX_VALUE });
        field.setIntValue(value);
        doc.add(field);
        // if need store it , add StoredField
        if (store == Field.Store.YES) {
            doc.add(new StoredField(fieldName, (int) value));
        }
    } else if (type == DataTypes.SHORT) {
        // short type , use int range to deal with short type, lucene has no short type
        short value = page.getShort(rowId);
        IntRangeField field = new IntRangeField(fieldName, new int[] { Short.MIN_VALUE }, new int[] { Short.MAX_VALUE });
        field.setShortValue(value);
        doc.add(field);
        // if need store it , add StoredField
        if (store == Field.Store.YES) {
            doc.add(new StoredField(fieldName, (int) value));
        }
    } else if (type == DataTypes.INT) {
        // int type , use int point to deal with int type
        int value = page.getInt(rowId);
        doc.add(new IntPoint(fieldName, new int[] { value }));
        // if need store it , add StoredField
        if (store == Field.Store.YES) {
            doc.add(new StoredField(fieldName, value));
        }
    } else if (type == DataTypes.LONG) {
        // long type , use long point to deal with long type
        long value = page.getLong(rowId);
        doc.add(new LongPoint(fieldName, new long[] { value }));
        // if need store it , add StoredField
        if (store == Field.Store.YES) {
            doc.add(new StoredField(fieldName, value));
        }
    } else if (type == DataTypes.FLOAT) {
        float value = page.getFloat(rowId);
        doc.add(new FloatPoint(fieldName, new float[] { value }));
        if (store == Field.Store.YES) {
            doc.add(new FloatPoint(fieldName, value));
        }
    } else if (type == DataTypes.DOUBLE) {
        double value = page.getDouble(rowId);
        doc.add(new DoublePoint(fieldName, new double[] { value }));
        if (store == Field.Store.YES) {
            doc.add(new DoublePoint(fieldName, value));
        }
    } else if (type == DataTypes.STRING) {
        byte[] value = page.getBytes(rowId);
        // TODO: how to get string value
        String strValue = null;
        try {
            strValue = new String(value, 2, value.length - 2, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        doc.add(new TextField(fieldName, strValue, store));
    } else if (type == DataTypes.DATE) {
    // TODO: how to get data value
    } else if (type == DataTypes.TIMESTAMP) {
    // TODO: how to get
    } else if (type == DataTypes.BOOLEAN) {
        boolean value = page.getBoolean(rowId);
        IntRangeField field = new IntRangeField(fieldName, new int[] { 0 }, new int[] { 1 });
        field.setIntValue(value ? 1 : 0);
        doc.add(field);
        if (store == Field.Store.YES) {
            doc.add(new StoredField(fieldName, value ? 1 : 0));
        }
    } else {
        LOGGER.error("unsupport data type " + type);
        throw new RuntimeException("unsupported data type " + type);
    }
    return true;
}
Also used : IntRangeField(org.apache.lucene.document.IntRangeField) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LongPoint(org.apache.lucene.document.LongPoint) LongPoint(org.apache.lucene.document.LongPoint) DoublePoint(org.apache.lucene.document.DoublePoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) IntPoint(org.apache.lucene.document.IntPoint) StoredField(org.apache.lucene.document.StoredField) FloatPoint(org.apache.lucene.document.FloatPoint) DoublePoint(org.apache.lucene.document.DoublePoint) DataType(org.apache.carbondata.core.metadata.datatype.DataType) TextField(org.apache.lucene.document.TextField)

Aggregations

DoublePoint (org.apache.lucene.document.DoublePoint)2 FloatPoint (org.apache.lucene.document.FloatPoint)2 IntPoint (org.apache.lucene.document.IntPoint)2 IntRangeField (org.apache.lucene.document.IntRangeField)2 LongPoint (org.apache.lucene.document.LongPoint)2 StoredField (org.apache.lucene.document.StoredField)2 TextField (org.apache.lucene.document.TextField)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 DataType (org.apache.carbondata.core.metadata.datatype.DataType)1