Example usage for org.apache.lucene.document DoublePoint DoublePoint

List of usage examples for org.apache.lucene.document DoublePoint DoublePoint

Introduction

In this page you can find the example usage for org.apache.lucene.document DoublePoint DoublePoint.

Prototype

public DoublePoint(String name, double... point) 

Source Link

Document

Creates a new DoublePoint, indexing the provided N-dimensional double point.

Usage

From source file:IndexTaxis.java

License:Apache License

static void addOneField(Document doc, String fieldName, String rawValue) {
    // nocommit//from  www  .  ja v  a  2  s  .  c  o m
    /*
    if (fieldName.equals("pick_up_lat")) {
      double value = Double.parseDouble(rawValue);
      doc.add(new DoublePoint(fieldName, value));
      doc.add(new SortedNumericDocValuesField(fieldName, NumericUtils.doubleToSortableLong(value)));
    }
    */
    switch (fieldName) {
    case "vendor_id":
    case "cab_color":
    case "payment_type":
    case "trip_type":
    case "rate_code":
    case "store_and_fwd_flag":
        doc.add(new StringField(fieldName, rawValue, Field.Store.NO));
        doc.add(new SortedSetDocValuesField(fieldName, new BytesRef(rawValue)));
        break;
    case "vendor_name":
        doc.add(new TextField(fieldName, rawValue, Field.Store.NO));
        break;
    case "pick_up_date_time":
    case "drop_off_date_time": {
        long value = Long.parseLong(rawValue);
        doc.add(new LongPoint(fieldName, value));
        doc.add(new SortedNumericDocValuesField(fieldName, value));
    }
        break;
    case "passenger_count": {
        int value = Integer.parseInt(rawValue);
        doc.add(new IntPoint(fieldName, value));
        doc.add(new SortedNumericDocValuesField(fieldName, value));
    }
        break;
    case "trip_distance":
    case "pick_up_lat":
    case "pick_up_lon":
    case "drop_off_lat":
    case "drop_off_lon":
    case "fare_amount":
    case "surcharge":
    case "mta_tax":
    case "extra":
    case "ehail_fee":
    case "improvement_surcharge":
    case "tip_amount":
    case "tolls_amount":
    case "total_amount": {
        double value;
        try {
            value = Double.parseDouble(rawValue);
        } catch (NumberFormatException nfe) {
            System.out.println(
                    "WARNING: failed to parse \"" + rawValue + "\" as double for field \"" + fieldName + "\"");
            return;
        }
        doc.add(new DoublePoint(fieldName, value));
        doc.add(new SortedNumericDocValuesField(fieldName, NumericUtils.doubleToSortableLong(value)));
    }
        break;
    default:
        throw new AssertionError("failed to handle field \"" + fieldName + "\"");
    }
}

From source file:com.czw.search.lucene.example.facet.DistanceFacetsExample.java

License:Apache License

/** 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);/*from w  ww. ja v a 2s.  c om*/

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

From source file:com.semantic.lucene.fields.image.LatField.java

@Override
public void add(Document doc, Double value) {
    doc.add(new DoublePoint(getName(), value));
    doc.add(new StoredField(getName(), value));
}

From source file:com.vmware.xenon.services.common.LuceneDocumentIndexService.java

License:Open Source License

public static Document addNumericField(Document doc, String propertyName, double propertyValue,
        boolean stored) {
    long longPropertyValue = NumericUtils.doubleToSortableLong(propertyValue);

    // StoredField is used if the property needs to be stored in the lucene document
    if (stored) {
        doc.add(new StoredField(propertyName, propertyValue));
    }/*from   w  w w  .  j  av a2s  .  c o  m*/

    // DoublePoint adds an index field to the document that allows for efficient search
    // and range queries
    doc.add(new DoublePoint(propertyName, propertyValue));

    // NumericDocValues allow for efficient group operations for a property.
    // TODO Investigate and revert code to use 'sort' to determine the type of DocValuesField
    doc.add(new NumericDocValuesField(propertyName, longPropertyValue));
    return doc;
}

From source file:com.vmware.xenon.services.common.LuceneIndexDocumentHelper.java

License:Open Source License

private void addNumericField(String propertyName, double propertyValue, boolean stored,
        boolean isCollectionItem, boolean sorted) {
    long longPropertyValue = NumericUtils.doubleToSortableLong(propertyValue);

    if (stored) {
        Field field = isCollectionItem ? new StoredField(propertyName, propertyValue)
                : getAndSetStoredField(propertyName, propertyValue);
        this.doc.add(field);
    }//from   ww w. j  a v a2  s .  c  o  m

    // DoublePoint adds an index field to the document that allows for efficient search
    // and range queries
    if (isCollectionItem) {
        this.doc.add(new DoublePoint(propertyName, propertyValue));
    } else {
        DoublePoint dpField = this.doublePointFields.computeIfAbsent(propertyName, (k) -> {
            return new DoublePoint(propertyName, propertyValue);
        });
        dpField.setDoubleValue(propertyValue);
        this.doc.add(dpField);
    }

    NumericDocValuesField ndField = getAndSetNumericField(propertyName, longPropertyValue, isCollectionItem);
    this.doc.add(ndField);

    if (sorted) {
        // special handling for groupBy queries
        Field sdField = getAndSetSortedStoredField(propertyName + GROUP_BY_PROPERTY_NAME_SUFFIX,
                Double.toString(propertyValue));
        this.doc.add(sdField);
    }

}

From source file:io.druid.extension.lucene.LuceneDocumentBuilder.java

License:Apache License

public Document buildLuceneDocument(InputRow row) {
    Set<String> excludedDimensions = dimensionsSpec.getDimensionExclusions();
    Document doc = new Document();
    for (String dimensionName : dimensionsSpec.getDimensionNames()) {
        if (excludedDimensions != null && !excludedDimensions.isEmpty()
                && excludedDimensions.contains(dimensionName)) {
            continue;
        }/*from  w  ww . j a va2 s.  c  o  m*/
        DimensionSchema schema = dimensionsSpec.getSchema(dimensionName);
        Object value = row.getRaw(dimensionName);
        if (ValueType.STRING.equals(schema.getValueType())) {
            doc.add(new TextField(dimensionName, String.valueOf(value), Store.NO));
        } else if (ValueType.FLOAT.equals(schema.getValueType())) {
            doc.add(new DoublePoint(dimensionName, (Double) value));
        } else if (ValueType.LONG.equals(schema.getValueType())) {
            doc.add(new LongPoint(dimensionName, (Long) value));
        }
    }
    return doc;
}

From source file:org.apache.carbondata.datamap.lucene.LuceneDataMapWriter.java

License:Apache License

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);/*  ww  w.  jav  a 2  s.  com*/
        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));
        }
    }
}

From source file:org.apache.geode.cache.lucene.internal.repository.serializer.SerializerUtil.java

License:Apache License

/**
 * Add a field to the document./*from www  . ja va  2  s  .co m*/
 * 
 * @return true if the field was successfully added
 */
public static boolean addField(Document doc, String field, Object fieldValue) {
    Class<?> clazz = fieldValue.getClass();
    if (clazz == String.class) {
        doc.add(new TextField(field, (String) fieldValue, Store.NO));
    } else if (clazz == Long.class) {
        doc.add(new LongPoint(field, (Long) fieldValue));
    } else if (clazz == Integer.class) {
        doc.add(new IntPoint(field, (Integer) fieldValue));
    } else if (clazz == Float.class) {
        doc.add(new FloatPoint(field, (Float) fieldValue));
    } else if (clazz == Double.class) {
        doc.add(new DoublePoint(field, (Double) fieldValue));
    } else {
        return false;
    }

    return true;
}

From source file:org.apache.solr.legacy.BBoxStrategy.java

License:Apache License

private Field[] createIndexableFields(Rectangle bbox) {
    Field[] fields = new Field[fieldsLen];
    int idx = -1;
    if (hasStored) {
        fields[++idx] = new StoredField(field_minX, bbox.getMinX());
        fields[++idx] = new StoredField(field_minY, bbox.getMinY());
        fields[++idx] = new StoredField(field_maxX, bbox.getMaxX());
        fields[++idx] = new StoredField(field_maxY, bbox.getMaxY());
    }//from  ww  w .ja  v a  2s  .c o  m
    if (hasDocVals) {
        fields[++idx] = new DoubleDocValuesField(field_minX, bbox.getMinX());
        fields[++idx] = new DoubleDocValuesField(field_minY, bbox.getMinY());
        fields[++idx] = new DoubleDocValuesField(field_maxX, bbox.getMaxX());
        fields[++idx] = new DoubleDocValuesField(field_maxY, bbox.getMaxY());
    }
    if (hasPointVals) {
        fields[++idx] = new DoublePoint(field_minX, bbox.getMinX());
        fields[++idx] = new DoublePoint(field_minY, bbox.getMinY());
        fields[++idx] = new DoublePoint(field_maxX, bbox.getMaxX());
        fields[++idx] = new DoublePoint(field_maxY, bbox.getMaxY());
    }
    if (legacyNumericFieldType != null) {
        fields[++idx] = new LegacyDoubleField(field_minX, bbox.getMinX(), legacyNumericFieldType);
        fields[++idx] = new LegacyDoubleField(field_minY, bbox.getMinY(), legacyNumericFieldType);
        fields[++idx] = new LegacyDoubleField(field_maxX, bbox.getMaxX(), legacyNumericFieldType);
        fields[++idx] = new LegacyDoubleField(field_maxY, bbox.getMaxY(), legacyNumericFieldType);
    }
    if (xdlFieldType != null) {
        fields[++idx] = new Field(field_xdl, bbox.getCrossesDateLine() ? "T" : "F", xdlFieldType);
    }
    assert idx == fields.length - 1;
    return fields;
}

From source file:org.apache.solr.legacy.PointVectorStrategy.java

License:Apache License

/** @see #createIndexableFields(org.locationtech.spatial4j.shape.Shape) */
public Field[] createIndexableFields(Point point) {
    Field[] fields = new Field[fieldsLen];
    int idx = -1;
    if (hasStored) {
        fields[++idx] = new StoredField(fieldNameX, point.getX());
        fields[++idx] = new StoredField(fieldNameY, point.getY());
    }// w  w w.ja v a2  s . c o m
    if (hasDocVals) {
        fields[++idx] = new DoubleDocValuesField(fieldNameX, point.getX());
        fields[++idx] = new DoubleDocValuesField(fieldNameY, point.getY());
    }
    if (hasPointVals) {
        fields[++idx] = new DoublePoint(fieldNameX, point.getX());
        fields[++idx] = new DoublePoint(fieldNameY, point.getY());
    }
    if (legacyNumericFieldType != null) {
        fields[++idx] = new LegacyDoubleField(fieldNameX, point.getX(), legacyNumericFieldType);
        fields[++idx] = new LegacyDoubleField(fieldNameY, point.getY(), legacyNumericFieldType);
    }
    assert idx == fields.length - 1;
    return fields;
}