Search in sources :

Example 16 with Mapping

use of org.janusgraph.core.schema.Mapping in project janusgraph by JanusGraph.

the class SolrIndex method supports.

@Override
public boolean supports(KeyInformation information) {
    final Class<?> dataType = information.getDataType();
    final Mapping mapping = Mapping.getMapping(information);
    if (Number.class.isAssignableFrom(dataType) || dataType == Date.class || dataType == Instant.class || dataType == Boolean.class || dataType == UUID.class) {
        return mapping == Mapping.DEFAULT;
    } else if (AttributeUtil.isString(dataType)) {
        return mapping == Mapping.DEFAULT || mapping == Mapping.TEXT || mapping == Mapping.STRING;
    } else if (AttributeUtil.isGeo(dataType)) {
        return mapping == Mapping.DEFAULT || mapping == Mapping.PREFIX_TREE;
    }
    return false;
}
Also used : Instant(java.time.Instant) Mapping(org.janusgraph.core.schema.Mapping) UUID(java.util.UUID)

Example 17 with Mapping

use of org.janusgraph.core.schema.Mapping in project janusgraph by JanusGraph.

the class LuceneIndex method addToDocument.

private void addToDocument(String store, String docID, Document doc, List<IndexEntry> content, Map<String, Shape> geoFields, KeyInformation.IndexRetriever information) {
    Preconditions.checkNotNull(doc);
    for (final IndexEntry e : content) {
        Preconditions.checkArgument(!e.hasMetaData(), "Lucene index does not support indexing meta data: %s", e);
        if (log.isTraceEnabled())
            log.trace("Adding field [{}] on document [{}]", e.field, docID);
        if (doc.getField(e.field) != null)
            doc.removeFields(e.field);
        if (e.value instanceof Number) {
            final Field field;
            final Field sortField;
            if (AttributeUtil.isWholeNumber((Number) e.value)) {
                field = new LongPoint(e.field, ((Number) e.value).longValue());
                sortField = new NumericDocValuesField(e.field, ((Number) e.value).longValue());
            } else {
                // double or float
                field = new DoublePoint(e.field, ((Number) e.value).doubleValue());
                sortField = new DoubleDocValuesField(e.field, ((Number) e.value).doubleValue());
            }
            doc.add(field);
            doc.add(sortField);
        } else if (AttributeUtil.isString(e.value)) {
            final String str = (String) e.value;
            final Mapping mapping = Mapping.getMapping(store, e.field, information);
            final Field field;
            switch(mapping) {
                case DEFAULT:
                case TEXT:
                    // lowering the case for case insensitive text search
                    field = new TextField(e.field, str.toLowerCase(), Field.Store.YES);
                    break;
                case STRING:
                    // if this field uses a custom analyzer, it must be stored as a TextField
                    // (or the analyzer, even if it is a KeywordAnalyzer won't be used)
                    field = new TextField(e.field, str, Field.Store.YES);
                    break;
                default:
                    throw new IllegalArgumentException("Illegal mapping specified: " + mapping);
            }
            doc.add(field);
        } else if (e.value instanceof Geoshape) {
            final Shape shape = ((Geoshape) e.value).getShape();
            geoFields.put(e.field, shape);
            doc.add(new StoredField(e.field, GEOID + e.value.toString()));
        } else if (e.value instanceof Date) {
            doc.add(new LongPoint(e.field, (((Date) e.value).getTime())));
        } else if (e.value instanceof Instant) {
            doc.add(new LongPoint(e.field, (((Instant) e.value).toEpochMilli())));
        } else if (e.value instanceof Boolean) {
            doc.add(new IntPoint(e.field, ((Boolean) e.value) ? 1 : 0));
        } else if (e.value instanceof UUID) {
            // Solr stores UUIDs as strings, we we do the same.
            final Field field = new StringField(e.field, e.value.toString(), Field.Store.YES);
            doc.add(field);
        } else {
            throw new IllegalArgumentException("Unsupported type: " + e.value);
        }
    }
    for (final Map.Entry<String, Shape> geo : geoFields.entrySet()) {
        if (log.isTraceEnabled())
            log.trace("Updating geo-indexes for key {}", geo.getKey());
        final KeyInformation ki = information.get(store, geo.getKey());
        final SpatialStrategy spatialStrategy = getSpatialStrategy(geo.getKey(), ki);
        for (final IndexableField f : spatialStrategy.createIndexableFields(geo.getValue())) {
            if (doc.getField(f.name()) != null) {
                doc.removeFields(f.name());
            }
            doc.add(f);
            if (spatialStrategy instanceof PointVectorStrategy) {
                doc.add(new DoubleDocValuesField(f.name(), f.numericValue() == null ? null : f.numericValue().doubleValue()));
            }
        }
    }
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) Mapping(org.janusgraph.core.schema.Mapping) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) PointVectorStrategy(org.apache.lucene.spatial.vector.PointVectorStrategy) Instant(java.time.Instant) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Mapping (org.janusgraph.core.schema.Mapping)17 Instant (java.time.Instant)8 UUID (java.util.UUID)6 Geoshape (org.janusgraph.core.attribute.Geoshape)6 IndexMapping (org.janusgraph.diskstorage.es.IndexMappings.IndexMapping)6 IOException (java.io.IOException)4 UncheckedIOException (java.io.UncheckedIOException)4 Cmp (org.janusgraph.core.attribute.Cmp)4 PermanentBackendException (org.janusgraph.diskstorage.PermanentBackendException)4 Date (java.util.Date)3 Geo (org.janusgraph.core.attribute.Geo)3 KeyInformation (org.janusgraph.diskstorage.indexing.KeyInformation)3 JanusGraphPredicate (org.janusgraph.graphdb.query.JanusGraphPredicate)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 JanusGraphException (org.janusgraph.core.JanusGraphException)2 Parameter (org.janusgraph.core.schema.Parameter)2 BackendException (org.janusgraph.diskstorage.BackendException)2 TemporaryBackendException (org.janusgraph.diskstorage.TemporaryBackendException)2