Search in sources :

Example 11 with SpatialStrategy

use of org.apache.lucene.spatial.SpatialStrategy 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)

Example 12 with SpatialStrategy

use of org.apache.lucene.spatial.SpatialStrategy in project titan by thinkaurelius.

the class LuceneExample method getSpatialStrategy.

private SpatialStrategy getSpatialStrategy(String key) {
    SpatialStrategy strategy = spatial.get(key);
    if (strategy == null) {
        final int maxLevels = 11;
        SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
        strategy = new RecursivePrefixTreeStrategy(grid, key);
        spatial.put(key, strategy);
    }
    return strategy;
}
Also used : RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) SpatialPrefixTree(org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) GeohashPrefixTree(org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)

Aggregations

SpatialStrategy (org.apache.lucene.spatial.SpatialStrategy)12 RecursivePrefixTreeStrategy (org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)6 SpatialPrefixTree (org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree)6 GeohashPrefixTree (org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)5 DoublePoint (org.apache.lucene.document.DoublePoint)3 LongPoint (org.apache.lucene.document.LongPoint)3 Mapping (org.janusgraph.core.schema.Mapping)3 Instant (java.time.Instant)2 IntPoint (org.apache.lucene.document.IntPoint)2 ValueSource (org.apache.lucene.queries.function.ValueSource)2 CompositeSpatialStrategy (org.apache.lucene.spatial.composite.CompositeSpatialStrategy)2 PointVectorStrategy (org.apache.lucene.spatial.vector.PointVectorStrategy)2 GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)2 Shape (org.locationtech.spatial4j.shape.Shape)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 DoubleDocValuesField (org.apache.lucene.document.DoubleDocValuesField)1 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)1