Search in sources :

Example 31 with DoublePoint

use of org.apache.lucene.document.DoublePoint in project lucene-solr by apache.

the class DistanceFacetsExample method index.

/** Build the example index. */
public void index() throws IOException {
    IndexWriter writer = new IndexWriter(indexDir, new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(OpenMode.CREATE));
    // TODO: we could index in radians instead ... saves all the conversions in getBoundingBoxFilter
    // Add documents with latitude/longitude location:
    // we index these both as DoublePoints (for bounding box/ranges) and as NumericDocValuesFields (for scoring)
    Document doc = new Document();
    doc.add(new DoublePoint("latitude", 40.759011));
    doc.add(new NumericDocValuesField("latitude", Double.doubleToRawLongBits(40.759011)));
    doc.add(new DoublePoint("longitude", -73.9844722));
    doc.add(new NumericDocValuesField("longitude", Double.doubleToRawLongBits(-73.9844722)));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new DoublePoint("latitude", 40.718266));
    doc.add(new NumericDocValuesField("latitude", Double.doubleToRawLongBits(40.718266)));
    doc.add(new DoublePoint("longitude", -74.007819));
    doc.add(new NumericDocValuesField("longitude", Double.doubleToRawLongBits(-74.007819)));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new DoublePoint("latitude", 40.7051157));
    doc.add(new NumericDocValuesField("latitude", Double.doubleToRawLongBits(40.7051157)));
    doc.add(new DoublePoint("longitude", -74.0088305));
    doc.add(new NumericDocValuesField("longitude", Double.doubleToRawLongBits(-74.0088305)));
    writer.addDocument(doc);
    // Open near-real-time searcher
    searcher = new IndexSearcher(DirectoryReader.open(writer));
    writer.close();
}
Also used : WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) IndexSearcher(org.apache.lucene.search.IndexSearcher) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) DoublePoint(org.apache.lucene.document.DoublePoint) Document(org.apache.lucene.document.Document) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 32 with DoublePoint

use of org.apache.lucene.document.DoublePoint 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)

Example 33 with DoublePoint

use of org.apache.lucene.document.DoublePoint in project cxf by apache.

the class TikaLuceneContentExtractor method addField.

private static void addField(final Document document, final LuceneDocumentMetadata documentMetadata, final String name, final String value) {
    final Class<?> type = documentMetadata.getFieldType(name);
    final ParamConverterProvider provider = documentMetadata.getFieldTypeConverter();
    if (type != null) {
        if (Number.class.isAssignableFrom(type)) {
            if (Double.class.isAssignableFrom(type)) {
                Double number = ParamConverterUtils.getValue(Double.class, provider, value);
                document.add(new DoublePoint(name, number));
                document.add(new StoredField(name, number));
            } else if (Float.class.isAssignableFrom(type)) {
                Float number = ParamConverterUtils.getValue(Float.class, provider, value);
                document.add(new FloatPoint(name, number));
                document.add(new StoredField(name, number));
            } else if (Long.class.isAssignableFrom(type)) {
                Long number = ParamConverterUtils.getValue(Long.class, provider, value);
                document.add(new LongPoint(name, number));
                document.add(new StoredField(name, number));
            } else if (Integer.class.isAssignableFrom(type) || Byte.class.isAssignableFrom(type)) {
                Integer number = ParamConverterUtils.getValue(Integer.class, provider, value);
                document.add(new IntPoint(name, number));
                document.add(new StoredField(name, number));
            } else {
                document.add(new StringField(name, value, Store.YES));
            }
            return;
        } else if (Date.class.isAssignableFrom(type)) {
            final Date date = ParamConverterUtils.getValue(Date.class, provider, value);
            final Field field;
            if (date != null) {
                field = new StringField(name, ParamConverterUtils.getString(Date.class, provider, date), Store.YES);
            } else {
                field = new StringField(name, value, Store.YES);
            }
            document.add(field);
            return;
        }
    }
    document.add(new StringField(name, value, Store.YES));
}
Also used : LongPoint(org.apache.lucene.document.LongPoint) Date(java.util.Date) IntPoint(org.apache.lucene.document.IntPoint) StringField(org.apache.lucene.document.StringField) StoredField(org.apache.lucene.document.StoredField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) StoredField(org.apache.lucene.document.StoredField) FloatPoint(org.apache.lucene.document.FloatPoint) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) StringField(org.apache.lucene.document.StringField) DoublePoint(org.apache.lucene.document.DoublePoint)

Example 34 with DoublePoint

use of org.apache.lucene.document.DoublePoint in project janusgraph by JanusGraph.

the class LuceneIndex method buildIndexFields.

private List<IndexableField> buildIndexFields(final Document doc, final KeyInformation.StoreRetriever information) {
    List<IndexableField> fields = new ArrayList<>();
    for (IndexableField field : doc.getFields()) {
        String fieldName = field.name();
        if (fieldName.equals(DOCID)) {
            continue;
        }
        KeyInformation ki = information.get(getOrigFieldName(fieldName));
        boolean isPossibleSortIndex = ki.getCardinality() == Cardinality.SINGLE;
        Class<?> dataType = ki.getDataType();
        if (AttributeUtils.isWholeNumber(dataType)) {
            long value = field.numericValue().longValue();
            fields.add(new LongPoint(fieldName, value));
            if (isPossibleSortIndex) {
                fields.add(new NumericDocValuesField(fieldName, value));
            }
        } else if (AttributeUtils.isDecimal(dataType)) {
            double value = field.numericValue().doubleValue();
            fields.add(new DoublePoint(fieldName, value));
            if (isPossibleSortIndex) {
                fields.add(new DoubleDocValuesField(fieldName, value));
            }
        } else if (AttributeUtils.isString(dataType)) {
            final Mapping mapping = Mapping.getMapping(ki);
            if ((mapping == Mapping.STRING || mapping == Mapping.TEXTSTRING) && isPossibleSortIndex) {
                fields.add(new SortedDocValuesField(fieldName, new BytesRef(field.stringValue())));
            }
        } else if (AttributeUtils.isGeo(dataType)) {
            if (log.isTraceEnabled())
                log.trace("Updating geo-indexes for key {}", fieldName);
            Shape shape;
            try {
                shape = Geoshape.fromWkt(field.stringValue().substring(GEOID.length())).getShape();
            } catch (java.text.ParseException e) {
                throw new IllegalArgumentException("Geoshape was not parsable", e);
            }
            final SpatialStrategy spatialStrategy = getSpatialStrategy(fieldName, ki);
            Collections.addAll(fields, spatialStrategy.createIndexableFields(shape));
        } else if (dataType.equals(Date.class) || dataType.equals(Instant.class)) {
            long value = field.numericValue().longValue();
            fields.add(new LongPoint(fieldName, value));
            if (isPossibleSortIndex) {
                fields.add(new NumericDocValuesField(fieldName, value));
            }
        } else if (dataType.equals(Boolean.class)) {
            fields.add(new IntPoint(fieldName, field.numericValue().intValue() == 1 ? 1 : 0));
            if (isPossibleSortIndex) {
                fields.add(new NumericDocValuesField(fieldName, field.numericValue().intValue()));
            }
        }
    }
    return fields;
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) Instant(java.time.Instant) ArrayList(java.util.ArrayList) Mapping(org.janusgraph.core.schema.Mapping) LongPoint(org.apache.lucene.document.LongPoint) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) Date(java.util.Date) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation) IndexableField(org.apache.lucene.index.IndexableField) IntPoint(org.apache.lucene.document.IntPoint) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) DoubleDocValuesField(org.apache.lucene.document.DoubleDocValuesField) DoublePoint(org.apache.lucene.document.DoublePoint) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) ParseException(org.apache.lucene.queryparser.classic.ParseException) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

DoublePoint (org.apache.lucene.document.DoublePoint)34 Document (org.apache.lucene.document.Document)24 LongPoint (org.apache.lucene.document.LongPoint)21 IntPoint (org.apache.lucene.document.IntPoint)18 FloatPoint (org.apache.lucene.document.FloatPoint)17 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)16 Directory (org.apache.lucene.store.Directory)16 IndexReader (org.apache.lucene.index.IndexReader)15 StoredField (org.apache.lucene.document.StoredField)13 IndexSearcher (org.apache.lucene.search.IndexSearcher)12 IndexWriter (org.apache.lucene.index.IndexWriter)11 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)11 Field (org.apache.lucene.document.Field)9 DoubleDocValuesField (org.apache.lucene.document.DoubleDocValuesField)8 BinaryPoint (org.apache.lucene.document.BinaryPoint)7 StringField (org.apache.lucene.document.StringField)7 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)7 BytesRef (org.apache.lucene.util.BytesRef)7 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)6 SortField (org.apache.lucene.search.SortField)6