Search in sources :

Example 1 with Not

use of org.janusgraph.graphdb.query.condition.Not in project janusgraph by JanusGraph.

the class SolrIndex method buildQueryFilter.

public String buildQueryFilter(Condition<JanusGraphElement> condition, KeyInformation.StoreRetriever information) {
    if (condition instanceof PredicateCondition) {
        final PredicateCondition<String, JanusGraphElement> atom = (PredicateCondition<String, JanusGraphElement>) condition;
        final Object value = atom.getValue();
        final String key = atom.getKey();
        final JanusGraphPredicate predicate = atom.getPredicate();
        if (value instanceof Number) {
            final String queryValue = escapeValue(value);
            Preconditions.checkArgument(predicate instanceof Cmp, "Relation not supported on numeric types: " + predicate);
            final Cmp numRel = (Cmp) predicate;
            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) {
            final Mapping map = getStringMapping(information.get(key));
            assert map == Mapping.TEXT || map == Mapping.STRING;
            if (map == Mapping.TEXT && !(Text.HAS_CONTAINS.contains(predicate) || predicate instanceof Cmp))
                throw new IllegalArgumentException("Text mapped string values only support CONTAINS and Compare queries and not: " + predicate);
            if (map == Mapping.STRING && Text.HAS_CONTAINS.contains(predicate))
                throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + predicate);
            // Special case
            if (predicate == Text.CONTAINS) {
                return tokenize(information, value, key, predicate, ParameterType.TEXT_ANALYZER.findParameter(information.get(key).getParameters(), null));
            } else if (predicate == Text.PREFIX || predicate == Text.CONTAINS_PREFIX) {
                return (key + ":" + escapeValue(value) + "*");
            } else if (predicate == Text.REGEX || predicate == Text.CONTAINS_REGEX) {
                return (key + ":/" + value + "/");
            } else if (predicate == Cmp.EQUAL) {
                final String tokenizer = ParameterType.STRING_ANALYZER.findParameter(information.get(key).getParameters(), null);
                if (tokenizer != null) {
                    return tokenize(information, value, key, predicate, tokenizer);
                } else {
                    return (key + ":\"" + escapeValue(value) + "\"");
                }
            } else if (predicate == Cmp.NOT_EQUAL) {
                return ("-" + key + ":\"" + escapeValue(value) + "\"");
            } else if (predicate == Text.FUZZY || predicate == Text.CONTAINS_FUZZY) {
                return (key + ":" + escapeValue(value) + "~");
            } else if (predicate == Cmp.LESS_THAN) {
                return (key + ":[* TO \"" + escapeValue(value) + "\"}");
            } else if (predicate == Cmp.LESS_THAN_EQUAL) {
                return (key + ":[* TO \"" + escapeValue(value) + "\"]");
            } else if (predicate == Cmp.GREATER_THAN) {
                return (key + ":{\"" + escapeValue(value) + "\" TO *]");
            } else if (predicate == Cmp.GREATER_THAN_EQUAL) {
                return (key + ":[\"" + escapeValue(value) + "\" TO *]");
            } else {
                throw new IllegalArgumentException("Relation is not supported for string value: " + predicate);
            }
        } else if (value instanceof Geoshape) {
            final Mapping map = Mapping.getMapping(information.get(key));
            Preconditions.checkArgument(predicate instanceof Geo && predicate != Geo.DISJOINT, "Relation not supported on geo types: " + predicate);
            Preconditions.checkArgument(map == Mapping.PREFIX_TREE || predicate == Geo.WITHIN || predicate == Geo.INTERSECT, "Relation not supported on geopoint types: " + predicate);
            final Geoshape geo = (Geoshape) value;
            if (geo.getType() == Geoshape.Type.CIRCLE && (predicate == Geo.INTERSECT || map == Mapping.DEFAULT)) {
                final 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 && (predicate == Geo.INTERSECT || map == Mapping.DEFAULT)) {
                final Geoshape.Point southwest = geo.getPoint(0);
                final Geoshape.Point northeast = geo.getPoint(1);
                return (key + ":[" + southwest.getLatitude() + "," + southwest.getLongitude() + " TO " + northeast.getLatitude() + "," + northeast.getLongitude() + "]");
            } else if (map == Mapping.PREFIX_TREE) {
                return key + ":\"" + SPATIAL_PREDICATES.get(predicate) + "(" + geo + ")\" distErrPct=0";
            } else {
                throw new IllegalArgumentException("Unsupported or invalid search shape type: " + geo.getType());
            }
        } else if (value instanceof Date || value instanceof Instant) {
            final String s = value.toString();
            final String queryValue = escapeValue(value instanceof Date ? toIsoDate((Date) value) : value.toString());
            Preconditions.checkArgument(predicate instanceof Cmp, "Relation not supported on date types: " + predicate);
            final Cmp numRel = (Cmp) predicate;
            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) {
            final Cmp numRel = (Cmp) predicate;
            final 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 (predicate == Cmp.EQUAL) {
                return (key + ":\"" + escapeValue(value) + "\"");
            } else if (predicate == Cmp.NOT_EQUAL) {
                return ("-" + key + ":\"" + escapeValue(value) + "\"");
            } else {
                throw new IllegalArgumentException("Relation is not supported for uuid value: " + predicate);
            }
        } else
            throw new IllegalArgumentException("Unsupported type: " + value);
    } else if (condition instanceof Not) {
        final String sub = buildQueryFilter(((Not) condition).getChild(), information);
        if (StringUtils.isNotBlank(sub))
            return "-(" + sub + ")";
        else
            return "";
    } else if (condition instanceof And) {
        final int numChildren = ((And) condition).size();
        final StringBuilder sb = new StringBuilder();
        for (final Condition<JanusGraphElement> c : condition.getChildren()) {
            final String sub = buildQueryFilter(c, information);
            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) {
        final StringBuilder sb = new StringBuilder();
        int element = 0;
        for (final Condition<JanusGraphElement> c : condition.getChildren()) {
            final String sub = buildQueryFilter(c, information);
            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);
    }
}
Also used : Or(org.janusgraph.graphdb.query.condition.Or) Mapping(org.janusgraph.core.schema.Mapping) JanusGraphElement(org.janusgraph.core.JanusGraphElement) JanusGraphPredicate(org.janusgraph.graphdb.query.JanusGraphPredicate) UUID(java.util.UUID) PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) Condition(org.janusgraph.graphdb.query.condition.Condition) PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) Cmp(org.janusgraph.core.attribute.Cmp) Instant(java.time.Instant) Geoshape(org.janusgraph.core.attribute.Geoshape) Date(java.util.Date) Geo(org.janusgraph.core.attribute.Geo) Not(org.janusgraph.graphdb.query.condition.Not) And(org.janusgraph.graphdb.query.condition.And)

Example 2 with Not

use of org.janusgraph.graphdb.query.condition.Not in project janusgraph by JanusGraph.

the class ElasticSearchIndex method getFilter.

public Map<String, Object> getFilter(Condition<?> condition, KeyInformation.StoreRetriever information) {
    if (condition instanceof PredicateCondition) {
        final PredicateCondition<String, ?> atom = (PredicateCondition) condition;
        Object value = atom.getValue();
        final String key = atom.getKey();
        final JanusGraphPredicate predicate = atom.getPredicate();
        if (value instanceof Number) {
            Preconditions.checkArgument(predicate instanceof Cmp, "Relation not supported on numeric types: " + predicate);
            final Cmp numRel = (Cmp) predicate;
            switch(numRel) {
                case EQUAL:
                    return compat.term(key, value);
                case NOT_EQUAL:
                    return compat.boolMustNot(compat.term(key, value));
                case LESS_THAN:
                    return compat.lt(key, value);
                case LESS_THAN_EQUAL:
                    return compat.lte(key, value);
                case GREATER_THAN:
                    return compat.gt(key, value);
                case GREATER_THAN_EQUAL:
                    return compat.gte(key, value);
                default:
                    throw new IllegalArgumentException("Unexpected relation: " + numRel);
            }
        } else if (value instanceof String) {
            final Mapping mapping = getStringMapping(information.get(key));
            final String fieldName;
            if (mapping == Mapping.TEXT && !(Text.HAS_CONTAINS.contains(predicate) || predicate instanceof Cmp))
                throw new IllegalArgumentException("Text mapped string values only support CONTAINS and Compare queries and not: " + predicate);
            if (mapping == Mapping.STRING && Text.HAS_CONTAINS.contains(predicate))
                throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + predicate);
            if (mapping == Mapping.TEXTSTRING && !(Text.HAS_CONTAINS.contains(predicate) || predicate instanceof Cmp)) {
                fieldName = getDualMappingName(key);
            } else {
                fieldName = key;
            }
            if (predicate == Text.CONTAINS || predicate == Cmp.EQUAL) {
                return compat.match(key, value);
            } else if (predicate == Text.CONTAINS_PREFIX) {
                if (!ParameterType.TEXT_ANALYZER.hasParameter(information.get(key).getParameters()))
                    value = ((String) value).toLowerCase();
                return compat.prefix(fieldName, value);
            } else if (predicate == Text.CONTAINS_REGEX) {
                if (!ParameterType.TEXT_ANALYZER.hasParameter(information.get(key).getParameters()))
                    value = ((String) value).toLowerCase();
                return compat.regexp(fieldName, value);
            } else if (predicate == Text.PREFIX) {
                return compat.prefix(fieldName, value);
            } else if (predicate == Text.REGEX) {
                return compat.regexp(fieldName, value);
            } else if (predicate == Cmp.NOT_EQUAL) {
                return compat.boolMustNot(compat.match(fieldName, value));
            } else if (predicate == Text.FUZZY || predicate == Text.CONTAINS_FUZZY) {
                return compat.fuzzyMatch(fieldName, value);
            } else if (predicate == Cmp.LESS_THAN) {
                return compat.lt(fieldName, value);
            } else if (predicate == Cmp.LESS_THAN_EQUAL) {
                return compat.lte(fieldName, value);
            } else if (predicate == Cmp.GREATER_THAN) {
                return compat.gt(fieldName, value);
            } else if (predicate == Cmp.GREATER_THAN_EQUAL) {
                return compat.gte(fieldName, value);
            } else
                throw new IllegalArgumentException("Predicate is not supported for string value: " + predicate);
        } else if (value instanceof Geoshape && Mapping.getMapping(information.get(key)) == Mapping.DEFAULT) {
            // geopoint
            final Geoshape shape = (Geoshape) value;
            Preconditions.checkArgument(predicate instanceof Geo && predicate != Geo.CONTAINS, "Relation not supported on geopoint types: " + predicate);
            final Map<String, Object> query;
            switch(shape.getType()) {
                case CIRCLE:
                    final Geoshape.Point center = shape.getPoint();
                    query = compat.geoDistance(key, center.getLatitude(), center.getLongitude(), shape.getRadius());
                    break;
                case BOX:
                    final Geoshape.Point southwest = shape.getPoint(0);
                    final Geoshape.Point northeast = shape.getPoint(1);
                    query = compat.geoBoundingBox(key, southwest.getLatitude(), southwest.getLongitude(), northeast.getLatitude(), northeast.getLongitude());
                    break;
                case POLYGON:
                    final List<List<Double>> points = IntStream.range(0, shape.size()).mapToObj(i -> ImmutableList.of(shape.getPoint(i).getLongitude(), shape.getPoint(i).getLatitude())).collect(Collectors.toList());
                    query = compat.geoPolygon(key, points);
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported or invalid search shape type for geopoint: " + shape.getType());
            }
            return predicate == Geo.DISJOINT ? compat.boolMustNot(query) : query;
        } else if (value instanceof Geoshape) {
            Preconditions.checkArgument(predicate instanceof Geo, "Relation not supported on geoshape types: " + predicate);
            final Geoshape shape = (Geoshape) value;
            final Map<String, Object> geo;
            switch(shape.getType()) {
                case CIRCLE:
                    final Geoshape.Point center = shape.getPoint();
                    geo = ImmutableMap.of(ES_TYPE_KEY, "circle", ES_GEO_COORDS_KEY, ImmutableList.of(center.getLongitude(), center.getLatitude()), "radius", shape.getRadius() + "km");
                    break;
                case BOX:
                    final Geoshape.Point southwest = shape.getPoint(0);
                    final Geoshape.Point northeast = shape.getPoint(1);
                    geo = ImmutableMap.of(ES_TYPE_KEY, "envelope", ES_GEO_COORDS_KEY, ImmutableList.of(ImmutableList.of(southwest.getLongitude(), northeast.getLatitude()), ImmutableList.of(northeast.getLongitude(), southwest.getLatitude())));
                    break;
                case LINE:
                    final List lineCoords = IntStream.range(0, shape.size()).mapToObj(i -> ImmutableList.of(shape.getPoint(i).getLongitude(), shape.getPoint(i).getLatitude())).collect(Collectors.toList());
                    geo = ImmutableMap.of(ES_TYPE_KEY, "linestring", ES_GEO_COORDS_KEY, lineCoords);
                    break;
                case POLYGON:
                    final List polyCoords = IntStream.range(0, shape.size()).mapToObj(i -> ImmutableList.of(shape.getPoint(i).getLongitude(), shape.getPoint(i).getLatitude())).collect(Collectors.toList());
                    geo = ImmutableMap.of(ES_TYPE_KEY, "polygon", ES_GEO_COORDS_KEY, ImmutableList.of(polyCoords));
                    break;
                case POINT:
                    geo = ImmutableMap.of(ES_TYPE_KEY, "point", ES_GEO_COORDS_KEY, ImmutableList.of(shape.getPoint().getLongitude(), shape.getPoint().getLatitude()));
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported or invalid search shape type: " + shape.getType());
            }
            return compat.geoShape(key, geo, (Geo) predicate);
        } else if (value instanceof Date || value instanceof Instant) {
            Preconditions.checkArgument(predicate instanceof Cmp, "Relation not supported on date types: " + predicate);
            final Cmp numRel = (Cmp) predicate;
            if (value instanceof Instant) {
                value = Date.from((Instant) value);
            }
            switch(numRel) {
                case EQUAL:
                    return compat.term(key, value);
                case NOT_EQUAL:
                    return compat.boolMustNot(compat.term(key, value));
                case LESS_THAN:
                    return compat.lt(key, value);
                case LESS_THAN_EQUAL:
                    return compat.lte(key, value);
                case GREATER_THAN:
                    return compat.gt(key, value);
                case GREATER_THAN_EQUAL:
                    return compat.gte(key, value);
                default:
                    throw new IllegalArgumentException("Unexpected relation: " + numRel);
            }
        } else if (value instanceof Boolean) {
            final Cmp numRel = (Cmp) predicate;
            switch(numRel) {
                case EQUAL:
                    return compat.term(key, value);
                case NOT_EQUAL:
                    return compat.boolMustNot(compat.term(key, value));
                default:
                    throw new IllegalArgumentException("Boolean types only support EQUAL or NOT_EQUAL");
            }
        } else if (value instanceof UUID) {
            if (predicate == Cmp.EQUAL) {
                return compat.term(key, value);
            } else if (predicate == Cmp.NOT_EQUAL) {
                return compat.boolMustNot(compat.term(key, value));
            } else {
                throw new IllegalArgumentException("Only equal or not equal is supported for UUIDs: " + predicate);
            }
        } else
            throw new IllegalArgumentException("Unsupported type: " + value);
    } else if (condition instanceof Not) {
        return compat.boolMustNot(getFilter(((Not) condition).getChild(), information));
    } else if (condition instanceof And) {
        final List queries = StreamSupport.stream(condition.getChildren().spliterator(), false).map(c -> getFilter(c, information)).collect(Collectors.toList());
        return compat.boolMust(queries);
    } else if (condition instanceof Or) {
        final List queries = StreamSupport.stream(condition.getChildren().spliterator(), false).map(c -> getFilter(c, information)).collect(Collectors.toList());
        return compat.boolShould(queries);
    } else
        throw new IllegalArgumentException("Invalid condition: " + condition);
}
Also used : PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) INDEX_MAX_RESULT_SET_SIZE(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.INDEX_MAX_RESULT_SET_SIZE) StringUtils(org.apache.commons.lang.StringUtils) Date(java.util.Date) Spliterators(java.util.Spliterators) LoggerFactory(org.slf4j.LoggerFactory) ConfigOption(org.janusgraph.diskstorage.configuration.ConfigOption) Geoshape(org.janusgraph.core.attribute.Geoshape) AbstractESCompat(org.janusgraph.diskstorage.es.compat.AbstractESCompat) BaseTransaction(org.janusgraph.diskstorage.BaseTransaction) IndexProvider(org.janusgraph.diskstorage.indexing.IndexProvider) Cardinality(org.janusgraph.core.Cardinality) IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation) AttributeUtil(org.janusgraph.graphdb.database.serialize.AttributeUtil) Map(java.util.Map) IndexQuery(org.janusgraph.diskstorage.indexing.IndexQuery) SerializationFeature(org.apache.tinkerpop.shaded.jackson.databind.SerializationFeature) LinkedListMultimap(com.google.common.collect.LinkedListMultimap) ES6Compat(org.janusgraph.diskstorage.es.compat.ES6Compat) And(org.janusgraph.graphdb.query.condition.And) Mapping(org.janusgraph.core.schema.Mapping) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ES_GEO_COORDS_KEY(org.janusgraph.diskstorage.es.ElasticSearchConstants.ES_GEO_COORDS_KEY) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) List(java.util.List) INDEX_NAME(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.INDEX_NAME) Parameter(org.janusgraph.core.schema.Parameter) Stream(java.util.stream.Stream) ObjectWriter(org.apache.tinkerpop.shaded.jackson.databind.ObjectWriter) HttpAuthTypes(org.janusgraph.diskstorage.es.rest.util.HttpAuthTypes) Spliterator(java.util.Spliterator) PreInitializeConfigOptions(org.janusgraph.graphdb.configuration.PreInitializeConfigOptions) Not(org.janusgraph.graphdb.query.condition.Not) IntStream(java.util.stream.IntStream) ObjectMapper(org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper) ConfigNamespace(org.janusgraph.diskstorage.configuration.ConfigNamespace) Condition(org.janusgraph.graphdb.query.condition.Condition) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) TemporaryBackendException(org.janusgraph.diskstorage.TemporaryBackendException) Rectangle(org.locationtech.spatial4j.shape.Rectangle) ImmutableList(com.google.common.collect.ImmutableList) Cmp(org.janusgraph.core.attribute.Cmp) IndexFeatures(org.janusgraph.diskstorage.indexing.IndexFeatures) Or(org.janusgraph.graphdb.query.condition.Or) JanusGraphException(org.janusgraph.core.JanusGraphException) ES1Compat(org.janusgraph.diskstorage.es.compat.ES1Compat) StreamSupport(java.util.stream.StreamSupport) Geo(org.janusgraph.core.attribute.Geo) BackendException(org.janusgraph.diskstorage.BackendException) JanusGraphPredicate(org.janusgraph.graphdb.query.JanusGraphPredicate) Logger(org.slf4j.Logger) Configuration(org.janusgraph.diskstorage.configuration.Configuration) BaseTransactionConfigurable(org.janusgraph.diskstorage.BaseTransactionConfigurable) RawQuery(org.janusgraph.diskstorage.indexing.RawQuery) ES_TYPE_KEY(org.janusgraph.diskstorage.es.ElasticSearchConstants.ES_TYPE_KEY) IOException(java.io.IOException) ES_DOC_KEY(org.janusgraph.diskstorage.es.ElasticSearchConstants.ES_DOC_KEY) DefaultTransaction(org.janusgraph.diskstorage.util.DefaultTransaction) Text(org.janusgraph.core.attribute.Text) IndexMapping(org.janusgraph.diskstorage.es.IndexMappings.IndexMapping) BaseTransactionConfig(org.janusgraph.diskstorage.BaseTransactionConfig) ES2Compat(org.janusgraph.diskstorage.es.compat.ES2Compat) ConfigOption.disallowEmpty(org.janusgraph.diskstorage.configuration.ConfigOption.disallowEmpty) Preconditions(com.google.common.base.Preconditions) ParameterType(org.janusgraph.graphdb.types.ParameterType) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) INDEX_NS(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.INDEX_NS) ES5Compat(org.janusgraph.diskstorage.es.compat.ES5Compat) IndexMutation(org.janusgraph.diskstorage.indexing.IndexMutation) Or(org.janusgraph.graphdb.query.condition.Or) Mapping(org.janusgraph.core.schema.Mapping) IndexMapping(org.janusgraph.diskstorage.es.IndexMappings.IndexMapping) JanusGraphPredicate(org.janusgraph.graphdb.query.JanusGraphPredicate) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) UUID(java.util.UUID) PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) Cmp(org.janusgraph.core.attribute.Cmp) Instant(java.time.Instant) Geoshape(org.janusgraph.core.attribute.Geoshape) Date(java.util.Date) Geo(org.janusgraph.core.attribute.Geo) Not(org.janusgraph.graphdb.query.condition.Not) And(org.janusgraph.graphdb.query.condition.And) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 3 with Not

use of org.janusgraph.graphdb.query.condition.Not in project janusgraph by JanusGraph.

the class ElasticSearchIndex method getNewDocument.

public Map<String, Object> getNewDocument(final List<IndexEntry> additions, KeyInformation.StoreRetriever information) throws BackendException {
    // JSON writes duplicate fields one after another, which forces us
    // at this stage to make de-duplication on the IndexEntry list. We don't want to pay the
    // price map storage on the Mutation level because none of other backends need that.
    final Multimap<String, IndexEntry> unique = LinkedListMultimap.create();
    for (final IndexEntry e : additions) {
        unique.put(e.field, e);
    }
    final Map<String, Object> doc = new HashMap<>();
    for (final Map.Entry<String, Collection<IndexEntry>> add : unique.asMap().entrySet()) {
        final KeyInformation keyInformation = information.get(add.getKey());
        final Object value;
        switch(keyInformation.getCardinality()) {
            case SINGLE:
                value = convertToEsType(Iterators.getLast(add.getValue().iterator()).value, Mapping.getMapping(keyInformation));
                break;
            case SET:
            case LIST:
                value = add.getValue().stream().map(v -> convertToEsType(v.value, Mapping.getMapping(keyInformation))).filter(v -> {
                    Preconditions.checkArgument(!(v instanceof byte[]), "Collections not supported for " + add.getKey());
                    return true;
                }).toArray();
                break;
            default:
                value = null;
                break;
        }
        doc.put(add.getKey(), value);
        if (hasDualStringMapping(information.get(add.getKey())) && keyInformation.getDataType() == String.class) {
            doc.put(getDualMappingName(add.getKey()), value);
        }
    }
    return doc;
}
Also used : PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) INDEX_MAX_RESULT_SET_SIZE(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.INDEX_MAX_RESULT_SET_SIZE) StringUtils(org.apache.commons.lang.StringUtils) Date(java.util.Date) Spliterators(java.util.Spliterators) LoggerFactory(org.slf4j.LoggerFactory) ConfigOption(org.janusgraph.diskstorage.configuration.ConfigOption) Geoshape(org.janusgraph.core.attribute.Geoshape) AbstractESCompat(org.janusgraph.diskstorage.es.compat.AbstractESCompat) BaseTransaction(org.janusgraph.diskstorage.BaseTransaction) IndexProvider(org.janusgraph.diskstorage.indexing.IndexProvider) Cardinality(org.janusgraph.core.Cardinality) IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation) AttributeUtil(org.janusgraph.graphdb.database.serialize.AttributeUtil) Map(java.util.Map) IndexQuery(org.janusgraph.diskstorage.indexing.IndexQuery) SerializationFeature(org.apache.tinkerpop.shaded.jackson.databind.SerializationFeature) LinkedListMultimap(com.google.common.collect.LinkedListMultimap) ES6Compat(org.janusgraph.diskstorage.es.compat.ES6Compat) And(org.janusgraph.graphdb.query.condition.And) Mapping(org.janusgraph.core.schema.Mapping) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ES_GEO_COORDS_KEY(org.janusgraph.diskstorage.es.ElasticSearchConstants.ES_GEO_COORDS_KEY) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) List(java.util.List) INDEX_NAME(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.INDEX_NAME) Parameter(org.janusgraph.core.schema.Parameter) Stream(java.util.stream.Stream) ObjectWriter(org.apache.tinkerpop.shaded.jackson.databind.ObjectWriter) HttpAuthTypes(org.janusgraph.diskstorage.es.rest.util.HttpAuthTypes) Spliterator(java.util.Spliterator) PreInitializeConfigOptions(org.janusgraph.graphdb.configuration.PreInitializeConfigOptions) Not(org.janusgraph.graphdb.query.condition.Not) IntStream(java.util.stream.IntStream) ObjectMapper(org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper) ConfigNamespace(org.janusgraph.diskstorage.configuration.ConfigNamespace) Condition(org.janusgraph.graphdb.query.condition.Condition) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) TemporaryBackendException(org.janusgraph.diskstorage.TemporaryBackendException) Rectangle(org.locationtech.spatial4j.shape.Rectangle) ImmutableList(com.google.common.collect.ImmutableList) Cmp(org.janusgraph.core.attribute.Cmp) IndexFeatures(org.janusgraph.diskstorage.indexing.IndexFeatures) Or(org.janusgraph.graphdb.query.condition.Or) JanusGraphException(org.janusgraph.core.JanusGraphException) ES1Compat(org.janusgraph.diskstorage.es.compat.ES1Compat) StreamSupport(java.util.stream.StreamSupport) Geo(org.janusgraph.core.attribute.Geo) BackendException(org.janusgraph.diskstorage.BackendException) JanusGraphPredicate(org.janusgraph.graphdb.query.JanusGraphPredicate) Logger(org.slf4j.Logger) Configuration(org.janusgraph.diskstorage.configuration.Configuration) BaseTransactionConfigurable(org.janusgraph.diskstorage.BaseTransactionConfigurable) RawQuery(org.janusgraph.diskstorage.indexing.RawQuery) ES_TYPE_KEY(org.janusgraph.diskstorage.es.ElasticSearchConstants.ES_TYPE_KEY) IOException(java.io.IOException) ES_DOC_KEY(org.janusgraph.diskstorage.es.ElasticSearchConstants.ES_DOC_KEY) DefaultTransaction(org.janusgraph.diskstorage.util.DefaultTransaction) Text(org.janusgraph.core.attribute.Text) IndexMapping(org.janusgraph.diskstorage.es.IndexMappings.IndexMapping) BaseTransactionConfig(org.janusgraph.diskstorage.BaseTransactionConfig) ES2Compat(org.janusgraph.diskstorage.es.compat.ES2Compat) ConfigOption.disallowEmpty(org.janusgraph.diskstorage.configuration.ConfigOption.disallowEmpty) Preconditions(com.google.common.base.Preconditions) ParameterType(org.janusgraph.graphdb.types.ParameterType) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) INDEX_NS(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.INDEX_NS) ES5Compat(org.janusgraph.diskstorage.es.compat.ES5Compat) IndexMutation(org.janusgraph.diskstorage.indexing.IndexMutation) HashMap(java.util.HashMap) IndexEntry(org.janusgraph.diskstorage.indexing.IndexEntry) Collection(java.util.Collection) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation)

Aggregations

Instant (java.time.Instant)3 Date (java.util.Date)3 UUID (java.util.UUID)3 Cmp (org.janusgraph.core.attribute.Cmp)3 Geo (org.janusgraph.core.attribute.Geo)3 Geoshape (org.janusgraph.core.attribute.Geoshape)3 Mapping (org.janusgraph.core.schema.Mapping)3 Preconditions (com.google.common.base.Preconditions)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Iterators (com.google.common.collect.Iterators)2 LinkedListMultimap (com.google.common.collect.LinkedListMultimap)2 Multimap (com.google.common.collect.Multimap)2 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2