Search in sources :

Example 1 with Mapping

use of com.thinkaurelius.titan.core.schema.Mapping in project titan by thinkaurelius.

the class LuceneIndex method addToDocument.

private void addToDocument(String store, String docID, Document doc, List<IndexEntry> content, Map<String, Shape> geofields, KeyInformation.IndexRetriever informations) {
    Preconditions.checkNotNull(doc);
    for (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) {
            Field field;
            if (AttributeUtil.isWholeNumber((Number) e.value)) {
                field = new LongField(e.field, ((Number) e.value).longValue(), Field.Store.YES);
            } else {
                //double or float
                field = new DoubleField(e.field, ((Number) e.value).doubleValue(), Field.Store.YES);
            }
            doc.add(field);
        } else if (AttributeUtil.isString(e.value)) {
            String str = (String) e.value;
            Mapping mapping = Mapping.getMapping(store, e.field, informations);
            Field field;
            switch(mapping) {
                case DEFAULT:
                case TEXT:
                    field = new TextField(e.field, str, Field.Store.YES);
                    break;
                case STRING:
                    field = new StringField(e.field, str, Field.Store.YES);
                    break;
                default:
                    throw new IllegalArgumentException("Illegal mapping specified: " + mapping);
            }
            doc.add(field);
        } else if (e.value instanceof Geoshape) {
            Shape shape = ((Geoshape) e.value).convert2Spatial4j();
            geofields.put(e.field, shape);
            doc.add(new StoredField(e.field, GEOID + toWkt(shape)));
        } else if (e.value instanceof Date) {
            doc.add(new LongField(e.field, (((Date) e.value).getTime()), Field.Store.YES));
        } else if (e.value instanceof Instant) {
            doc.add(new LongField(e.field, (((Instant) e.value).toEpochMilli()), Field.Store.YES));
        } else if (e.value instanceof Boolean) {
            doc.add(new IntField(e.field, ((Boolean) e.value) ? 1 : 0, Field.Store.YES));
        } else if (e.value instanceof UUID) {
            //Solr stores UUIDs as strings, we we do the same.
            Field field = new StringField(e.field, e.value.toString(), Field.Store.YES);
            doc.add(field);
        } else {
            throw new IllegalArgumentException("Unsupported type: " + e.value);
        }
    }
    for (Map.Entry<String, Shape> geo : geofields.entrySet()) {
        if (log.isTraceEnabled())
            log.trace("Updating geo-indexes for key {}", geo.getKey());
        for (IndexableField f : getSpatialStrategy(geo.getKey()).createIndexableFields(geo.getValue())) doc.add(f);
    }
}
Also used : Shape(com.spatial4j.core.shape.Shape) Instant(java.time.Instant) Mapping(com.thinkaurelius.titan.core.schema.Mapping) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with Mapping

use of com.thinkaurelius.titan.core.schema.Mapping in project titan by thinkaurelius.

the class LuceneIndex method register.

@Override
public void register(String store, String key, KeyInformation information, BaseTransaction tx) throws BackendException {
    Class<?> dataType = information.getDataType();
    Mapping map = Mapping.getMapping(information);
    Preconditions.checkArgument(map == Mapping.DEFAULT || AttributeUtil.isString(dataType), "Specified illegal mapping [%s] for data type [%s]", map, dataType);
}
Also used : Mapping(com.thinkaurelius.titan.core.schema.Mapping)

Example 3 with Mapping

use of com.thinkaurelius.titan.core.schema.Mapping in project titan by thinkaurelius.

the class ElasticSearchIndex method register.

@Override
public void register(String store, String key, KeyInformation information, BaseTransaction tx) throws BackendException {
    XContentBuilder mapping;
    Class<?> dataType = information.getDataType();
    Mapping map = Mapping.getMapping(information);
    Preconditions.checkArgument(map == Mapping.DEFAULT || AttributeUtil.isString(dataType), "Specified illegal mapping [%s] for data type [%s]", map, dataType);
    try {
        mapping = XContentFactory.jsonBuilder().startObject().startObject(store).field(TTL_FIELD, new HashMap<String, Object>() {

            {
                put("enabled", true);
            }
        }).startObject("properties").startObject(key);
        if (AttributeUtil.isString(dataType)) {
            if (map == Mapping.DEFAULT)
                map = Mapping.TEXT;
            log.debug("Registering string type for {} with mapping {}", key, map);
            mapping.field("type", "string");
            switch(map) {
                case STRING:
                    mapping.field("index", "not_analyzed");
                    break;
                case TEXT:
                    //default, do nothing
                    break;
                case TEXTSTRING:
                    mapping.endObject();
                    //add string mapping
                    mapping.startObject(getDualMappingName(key));
                    mapping.field("type", "string");
                    mapping.field("index", "not_analyzed");
                    break;
                default:
                    throw new AssertionError("Unexpected mapping: " + map);
            }
        } else if (dataType == Float.class) {
            log.debug("Registering float type for {}", key);
            mapping.field("type", "float");
        } else if (dataType == Double.class) {
            log.debug("Registering double type for {}", key);
            mapping.field("type", "double");
        } else if (dataType == Byte.class) {
            log.debug("Registering byte type for {}", key);
            mapping.field("type", "byte");
        } else if (dataType == Short.class) {
            log.debug("Registering short type for {}", key);
            mapping.field("type", "short");
        } else if (dataType == Integer.class) {
            log.debug("Registering integer type for {}", key);
            mapping.field("type", "integer");
        } else if (dataType == Long.class) {
            log.debug("Registering long type for {}", key);
            mapping.field("type", "long");
        } else if (dataType == Boolean.class) {
            log.debug("Registering boolean type for {}", key);
            mapping.field("type", "boolean");
        } else if (dataType == Geoshape.class) {
            log.debug("Registering geo_point type for {}", key);
            mapping.field("type", "geo_point");
        } else if (dataType == Date.class || dataType == Instant.class) {
            log.debug("Registering date type for {}", key);
            mapping.field("type", "date");
        } else if (dataType == Boolean.class) {
            log.debug("Registering boolean type for {}", key);
            mapping.field("type", "boolean");
        } else if (dataType == UUID.class) {
            log.debug("Registering uuid type for {}", key);
            mapping.field("type", "string");
            mapping.field("index", "not_analyzed");
        }
        mapping.endObject().endObject().endObject().endObject();
    } catch (IOException e) {
        throw new PermanentBackendException("Could not render json for put mapping request", e);
    }
    try {
        PutMappingResponse response = client.admin().indices().preparePutMapping(indexName).setIgnoreConflicts(false).setType(store).setSource(mapping).execute().actionGet();
    } catch (Exception e) {
        throw convert(e);
    }
}
Also used : Mapping(com.thinkaurelius.titan.core.schema.Mapping) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) TitanException(com.thinkaurelius.titan.core.TitanException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) IOException(java.io.IOException) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 4 with Mapping

use of com.thinkaurelius.titan.core.schema.Mapping in project titan by thinkaurelius.

the class SolrIndex method supports.

@Override
public boolean supports(KeyInformation information, TitanPredicate titanPredicate) {
    Class<?> dataType = information.getDataType();
    Mapping mapping = Mapping.getMapping(information);
    if (mapping != Mapping.DEFAULT && !AttributeUtil.isString(dataType))
        return false;
    if (information.getCardinality() != Cardinality.SINGLE) {
        return false;
    }
    if (Number.class.isAssignableFrom(dataType)) {
        return titanPredicate instanceof Cmp;
    } else if (dataType == Geoshape.class) {
        return titanPredicate == Geo.WITHIN;
    } else if (AttributeUtil.isString(dataType)) {
        switch(mapping) {
            case DEFAULT:
            case TEXT:
                return titanPredicate == Text.CONTAINS || titanPredicate == Text.CONTAINS_PREFIX || titanPredicate == Text.CONTAINS_REGEX;
            case STRING:
                return titanPredicate == Cmp.EQUAL || titanPredicate == Cmp.NOT_EQUAL || titanPredicate == Text.REGEX || titanPredicate == Text.PREFIX;
        }
    } else if (dataType == Date.class || dataType == Instant.class) {
        if (titanPredicate instanceof Cmp)
            return true;
    } else if (dataType == Boolean.class) {
        return titanPredicate == Cmp.EQUAL || titanPredicate == Cmp.NOT_EQUAL;
    } else if (dataType == UUID.class) {
        return titanPredicate == Cmp.EQUAL || titanPredicate == Cmp.NOT_EQUAL;
    }
    return false;
}
Also used : Mapping(com.thinkaurelius.titan.core.schema.Mapping)

Example 5 with Mapping

use of com.thinkaurelius.titan.core.schema.Mapping in project titan by thinkaurelius.

the class SolrIndex method buildQueryFilter.

public String buildQueryFilter(Condition<TitanElement> condition, KeyInformation.StoreRetriever informations) {
    if (condition instanceof PredicateCondition) {
        PredicateCondition<String, TitanElement> atom = (PredicateCondition<String, TitanElement>) condition;
        Object value = atom.getValue();
        String key = atom.getKey();
        TitanPredicate titanPredicate = atom.getPredicate();
        if (value instanceof Number) {
            String queryValue = escapeValue(value);
            Preconditions.checkArgument(titanPredicate instanceof Cmp, "Relation not supported on numeric types: " + titanPredicate);
            Cmp numRel = (Cmp) titanPredicate;
            switch(numRel) {
                case EQUAL:
                    return (key + ":" + queryValue);
                case NOT_EQUAL:
                    return ("-" + key + ":" + queryValue);
                case LESS_THAN:
                    //use right curly to mean up to but not including value
                    return (key + ":[* TO " + queryValue + "}");
                case LESS_THAN_EQUAL:
                    return (key + ":[* TO " + queryValue + "]");
                case GREATER_THAN:
                    //use left curly to mean greater than but not including value
                    return (key + ":{" + queryValue + " TO *]");
                case GREATER_THAN_EQUAL:
                    return (key + ":[" + queryValue + " TO *]");
                default:
                    throw new IllegalArgumentException("Unexpected relation: " + numRel);
            }
        } else if (value instanceof String) {
            Mapping map = getStringMapping(informations.get(key));
            assert map == Mapping.TEXT || map == Mapping.STRING;
            if (map == Mapping.TEXT && !titanPredicate.toString().startsWith("CONTAINS"))
                throw new IllegalArgumentException("Text mapped string values only support CONTAINS queries and not: " + titanPredicate);
            if (map == Mapping.STRING && titanPredicate.toString().startsWith("CONTAINS"))
                throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + titanPredicate);
            //Special case
            if (titanPredicate == Text.CONTAINS) {
                //e.g. - if terms tomorrow and world were supplied, and fq=text:(tomorrow  world)
                //sample data set would return 2 documents: one where text = Tomorrow is the World,
                //and the second where text = Hello World. Hence, we are decomposing the query string
                //and building an AND query explicitly because we need AND semantics
                value = ((String) value).toLowerCase();
                List<String> terms = Text.tokenize((String) value);
                if (terms.isEmpty()) {
                    return "";
                } else if (terms.size() == 1) {
                    return (key + ":(" + escapeValue(terms.get(0)) + ")");
                } else {
                    And<TitanElement> andTerms = new And<TitanElement>();
                    for (String term : terms) {
                        andTerms.add(new PredicateCondition<String, TitanElement>(key, titanPredicate, term));
                    }
                    return buildQueryFilter(andTerms, informations);
                }
            }
            if (titanPredicate == Text.PREFIX || titanPredicate == Text.CONTAINS_PREFIX) {
                return (key + ":" + escapeValue(value) + "*");
            } else if (titanPredicate == Text.REGEX || titanPredicate == Text.CONTAINS_REGEX) {
                return (key + ":/" + value + "/");
            } else if (titanPredicate == Cmp.EQUAL) {
                return (key + ":\"" + escapeValue(value) + "\"");
            } else if (titanPredicate == Cmp.NOT_EQUAL) {
                return ("-" + key + ":\"" + escapeValue(value) + "\"");
            } else {
                throw new IllegalArgumentException("Relation is not supported for string value: " + titanPredicate);
            }
        } else if (value instanceof Geoshape) {
            Geoshape geo = (Geoshape) value;
            if (geo.getType() == Geoshape.Type.CIRCLE) {
                Geoshape.Point center = geo.getPoint();
                return ("{!geofilt sfield=" + key + " pt=" + center.getLatitude() + "," + center.getLongitude() + " d=" + geo.getRadius() + //distance in kilometers
                "} distErrPct=0");
            } else if (geo.getType() == Geoshape.Type.BOX) {
                Geoshape.Point southwest = geo.getPoint(0);
                Geoshape.Point northeast = geo.getPoint(1);
                return (key + ":[" + southwest.getLatitude() + "," + southwest.getLongitude() + " TO " + northeast.getLatitude() + "," + northeast.getLongitude() + "]");
            } else if (geo.getType() == Geoshape.Type.POLYGON) {
                List<Geoshape.Point> coordinates = getPolygonPoints(geo);
                StringBuilder poly = new StringBuilder(key + ":\"IsWithin(POLYGON((");
                for (Geoshape.Point coordinate : coordinates) {
                    poly.append(coordinate.getLongitude()).append(" ").append(coordinate.getLatitude()).append(", ");
                }
                //close the polygon with the first coordinate
                poly.append(coordinates.get(0).getLongitude()).append(" ").append(coordinates.get(0).getLatitude());
                poly.append(")))\" distErrPct=0");
                return (poly.toString());
            }
        } else if (value instanceof Date || value instanceof Instant) {
            String s = value.toString();
            String queryValue = escapeValue(value instanceof Date ? toIsoDate((Date) value) : value.toString());
            Preconditions.checkArgument(titanPredicate instanceof Cmp, "Relation not supported on date types: " + titanPredicate);
            Cmp numRel = (Cmp) titanPredicate;
            switch(numRel) {
                case EQUAL:
                    return (key + ":" + queryValue);
                case NOT_EQUAL:
                    return ("-" + key + ":" + queryValue);
                case LESS_THAN:
                    //use right curly to mean up to but not including value
                    return (key + ":[* TO " + queryValue + "}");
                case LESS_THAN_EQUAL:
                    return (key + ":[* TO " + queryValue + "]");
                case GREATER_THAN:
                    //use left curly to mean greater than but not including value
                    return (key + ":{" + queryValue + " TO *]");
                case GREATER_THAN_EQUAL:
                    return (key + ":[" + queryValue + " TO *]");
                default:
                    throw new IllegalArgumentException("Unexpected relation: " + numRel);
            }
        } else if (value instanceof Boolean) {
            Cmp numRel = (Cmp) titanPredicate;
            String queryValue = escapeValue(value);
            switch(numRel) {
                case EQUAL:
                    return (key + ":" + queryValue);
                case NOT_EQUAL:
                    return ("-" + key + ":" + queryValue);
                default:
                    throw new IllegalArgumentException("Boolean types only support EQUAL or NOT_EQUAL");
            }
        } else if (value instanceof UUID) {
            if (titanPredicate == Cmp.EQUAL) {
                return (key + ":\"" + escapeValue(value) + "\"");
            } else if (titanPredicate == Cmp.NOT_EQUAL) {
                return ("-" + key + ":\"" + escapeValue(value) + "\"");
            } else {
                throw new IllegalArgumentException("Relation is not supported for uuid value: " + titanPredicate);
            }
        } else
            throw new IllegalArgumentException("Unsupported type: " + value);
    } else if (condition instanceof Not) {
        String sub = buildQueryFilter(((Not) condition).getChild(), informations);
        if (StringUtils.isNotBlank(sub))
            return "-(" + sub + ")";
        else
            return "";
    } else if (condition instanceof And) {
        int numChildren = ((And) condition).size();
        StringBuilder sb = new StringBuilder();
        for (Condition<TitanElement> c : condition.getChildren()) {
            String sub = buildQueryFilter(c, informations);
            if (StringUtils.isBlank(sub))
                continue;
            // b. expression is a single statement in the AND.
            if (!sub.startsWith("-") && numChildren > 1)
                sb.append("+");
            sb.append(sub).append(" ");
        }
        return sb.toString();
    } else if (condition instanceof Or) {
        StringBuilder sb = new StringBuilder();
        int element = 0;
        for (Condition<TitanElement> c : condition.getChildren()) {
            String sub = buildQueryFilter(c, informations);
            if (StringUtils.isBlank(sub))
                continue;
            if (element == 0)
                sb.append("(");
            else
                sb.append(" OR ");
            sb.append(sub);
            element++;
        }
        if (element > 0)
            sb.append(")");
        return sb.toString();
    } else {
        throw new IllegalArgumentException("Invalid condition: " + condition);
    }
    return null;
}
Also used : Mapping(com.thinkaurelius.titan.core.schema.Mapping) TitanElement(com.thinkaurelius.titan.core.TitanElement) Instant(java.time.Instant) TitanPredicate(com.thinkaurelius.titan.graphdb.query.TitanPredicate)

Aggregations

Mapping (com.thinkaurelius.titan.core.schema.Mapping)15 Geoshape (com.thinkaurelius.titan.core.attribute.Geoshape)6 Date (java.util.Date)6 UUID (java.util.UUID)6 Cmp (com.thinkaurelius.titan.core.attribute.Cmp)4 Instant (java.time.Instant)4 TitanElement (com.thinkaurelius.titan.core.TitanElement)3 TitanPredicate (com.thinkaurelius.titan.graphdb.query.TitanPredicate)3 And (com.thinkaurelius.titan.graphdb.query.condition.And)2 Condition (com.thinkaurelius.titan.graphdb.query.condition.Condition)2 Not (com.thinkaurelius.titan.graphdb.query.condition.Not)2 Or (com.thinkaurelius.titan.graphdb.query.condition.Or)2 PredicateCondition (com.thinkaurelius.titan.graphdb.query.condition.PredicateCondition)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Shape (com.spatial4j.core.shape.Shape)1 TitanException (com.thinkaurelius.titan.core.TitanException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1