Search in sources :

Example 1 with JanusGraphPredicate

use of org.janusgraph.graphdb.query.JanusGraphPredicate in project atlas by apache.

the class NativeJanusGraphQuery method has.

@Override
public void has(String propertyName, QueryOperator op, Object value) {
    JanusGraphPredicate pred;
    if (op instanceof ComparisionOperator) {
        Compare c = getGremlinPredicate((ComparisionOperator) op);
        pred = JanusGraphPredicate.Converter.convert(c);
    } else {
        pred = getGremlinPredicate((MatchingOperator) op);
    }
    query.has(propertyName, pred, value);
}
Also used : ComparisionOperator(org.apache.atlas.repository.graphdb.AtlasGraphQuery.ComparisionOperator) MatchingOperator(org.apache.atlas.repository.graphdb.AtlasGraphQuery.MatchingOperator) JanusGraphPredicate(org.janusgraph.graphdb.query.JanusGraphPredicate) Compare(org.apache.tinkerpop.gremlin.process.traversal.Compare)

Example 2 with JanusGraphPredicate

use of org.janusgraph.graphdb.query.JanusGraphPredicate in project janusgraph by JanusGraph.

the class LuceneIndex method convertQuery.

private SearchParams convertQuery(Condition<?> condition, final KeyInformation.StoreRetriever information, final LuceneCustomAnalyzer delegatingAnalyzer) {
    final SearchParams params = new SearchParams();
    if (condition instanceof PredicateCondition) {
        final PredicateCondition<String, ?> atom = (PredicateCondition) condition;
        Object value = atom.getValue();
        final String key = atom.getKey();
        KeyInformation ki = information.get(key);
        final JanusGraphPredicate janusgraphPredicate = atom.getPredicate();
        if (value == null && janusgraphPredicate == Cmp.NOT_EQUAL) {
            // some fields like Integer omit norms but have docValues
            params.addQuery(new DocValuesFieldExistsQuery(key), BooleanClause.Occur.SHOULD);
            // some fields like Text have no docValue but have norms
            params.addQuery(new NormsFieldExistsQuery(key), BooleanClause.Occur.SHOULD);
        } else if (value instanceof Number) {
            Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on numeric types: %s", janusgraphPredicate);
            params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, (Number) value));
        } else if (value instanceof String) {
            if (janusgraphPredicate == Cmp.LESS_THAN) {
                params.addQuery(TermRangeQuery.newStringRange(key, null, value.toString(), false, false));
            } else if (janusgraphPredicate == Cmp.LESS_THAN_EQUAL) {
                params.addQuery(TermRangeQuery.newStringRange(key, null, value.toString(), false, true));
            } else if (janusgraphPredicate == Cmp.GREATER_THAN) {
                params.addQuery(TermRangeQuery.newStringRange(key, value.toString(), null, false, false));
            } else if (janusgraphPredicate == Cmp.GREATER_THAN_EQUAL) {
                params.addQuery(TermRangeQuery.newStringRange(key, value.toString(), null, true, false));
            } else {
                final Mapping map = Mapping.getMapping(ki);
                final String stringFieldKey;
                if (Mapping.getMapping(ki) == Mapping.TEXTSTRING) {
                    stringFieldKey = getDualFieldName(key, ki).orElse(key);
                } else {
                    stringFieldKey = key;
                }
                if ((map == Mapping.DEFAULT || map == Mapping.TEXT) && !Text.HAS_CONTAINS.contains(janusgraphPredicate))
                    throw new IllegalArgumentException("Text mapped string values only support CONTAINS queries and not: " + janusgraphPredicate);
                if (map == Mapping.STRING && Text.HAS_CONTAINS.contains(janusgraphPredicate))
                    throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + janusgraphPredicate);
                if (janusgraphPredicate == Text.CONTAINS) {
                    tokenize(params, map, delegatingAnalyzer, ((String) value).toLowerCase(), key, janusgraphPredicate);
                } else if (janusgraphPredicate == Text.CONTAINS_PREFIX) {
                    tokenize(params, map, delegatingAnalyzer, (String) value, key, janusgraphPredicate);
                } else if (janusgraphPredicate == Text.PREFIX) {
                    params.addQuery(new PrefixQuery(new Term(stringFieldKey, (String) value)));
                } else if (janusgraphPredicate == Text.REGEX) {
                    final RegexpQuery rq = new RegexpQuery(new Term(stringFieldKey, (String) value));
                    params.addQuery(rq);
                } else if (janusgraphPredicate == Text.CONTAINS_REGEX) {
                    // This is terrible -- there is probably a better way
                    // putting this to lowercase because Text search is supposed to be case insensitive
                    final RegexpQuery rq = new RegexpQuery(new Term(key, ".*" + (((String) value).toLowerCase()) + ".*"));
                    params.addQuery(rq);
                } else if (janusgraphPredicate == Cmp.EQUAL || janusgraphPredicate == Cmp.NOT_EQUAL) {
                    tokenize(params, map, delegatingAnalyzer, (String) value, stringFieldKey, janusgraphPredicate);
                } else if (janusgraphPredicate == Text.FUZZY) {
                    params.addQuery(new FuzzyQuery(new Term(stringFieldKey, (String) value), Text.getMaxEditDistance((String) value)));
                } else if (janusgraphPredicate == Text.CONTAINS_FUZZY) {
                    value = ((String) value).toLowerCase();
                    final Builder b = new BooleanQuery.Builder();
                    for (final String term : Text.tokenize((String) value)) {
                        b.add(new FuzzyQuery(new Term(key, term), Text.getMaxEditDistance(term)), BooleanClause.Occur.MUST);
                    }
                    params.addQuery(b.build());
                } else
                    throw new IllegalArgumentException("Relation is not supported for string value: " + janusgraphPredicate);
            }
        } else if (value instanceof Geoshape) {
            Preconditions.checkArgument(janusgraphPredicate instanceof Geo, "Relation not supported on geo types: %s", janusgraphPredicate);
            final Shape shape = ((Geoshape) value).getShape();
            final SpatialOperation spatialOp = SPATIAL_PREDICATES.get(janusgraphPredicate);
            final SpatialArgs args = new SpatialArgs(spatialOp, shape);
            params.addQuery(getSpatialStrategy(key, information.get(key)).makeQuery(args));
        } else if (value instanceof Date) {
            Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on date types: %s", janusgraphPredicate);
            params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, ((Date) value).getTime()));
        } else if (value instanceof Instant) {
            Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on instant types: %s", janusgraphPredicate);
            params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, ((Instant) value).toEpochMilli()));
        } else if (value instanceof Boolean) {
            Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on boolean types: %s", janusgraphPredicate);
            final int intValue;
            switch((Cmp) janusgraphPredicate) {
                case EQUAL:
                    intValue = ((Boolean) value) ? 1 : 0;
                    params.addQuery(IntPoint.newRangeQuery(key, intValue, intValue));
                    break;
                case NOT_EQUAL:
                    intValue = ((Boolean) value) ? 0 : 1;
                    params.addQuery(IntPoint.newRangeQuery(key, intValue, intValue));
                    break;
                default:
                    throw new IllegalArgumentException("Boolean types only support EQUAL or NOT_EQUAL");
            }
        } else if (value instanceof UUID) {
            Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on UUID types: %s", janusgraphPredicate);
            if (janusgraphPredicate == Cmp.EQUAL) {
                params.addQuery(new TermQuery(new Term(key, value.toString())));
            } else if (janusgraphPredicate == Cmp.NOT_EQUAL) {
                final BooleanQuery.Builder q = new BooleanQuery.Builder();
                q.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
                q.add(new TermQuery(new Term(key, value.toString())), BooleanClause.Occur.MUST_NOT);
                params.addQuery(q.build());
            } else {
                throw new IllegalArgumentException("Relation is not supported for UUID type: " + janusgraphPredicate);
            }
        } else {
            throw new IllegalArgumentException("Unsupported type: " + value);
        }
    } else if (condition instanceof Not) {
        final SearchParams childParams = convertQuery(((Not) condition).getChild(), information, delegatingAnalyzer);
        params.addQuery(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
        params.addParams(childParams, BooleanClause.Occur.MUST_NOT);
    } else if (condition instanceof And) {
        for (final Condition c : condition.getChildren()) {
            final SearchParams childParams = convertQuery(c, information, delegatingAnalyzer);
            params.addParams(childParams, BooleanClause.Occur.MUST);
        }
    } else if (condition instanceof Or) {
        for (final Condition c : condition.getChildren()) {
            final SearchParams childParams = convertQuery(c, information, delegatingAnalyzer);
            params.addParams(childParams, BooleanClause.Occur.SHOULD);
        }
    } else
        throw new IllegalArgumentException("Invalid condition: " + condition);
    return params;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Shape(org.locationtech.spatial4j.shape.Shape) Or(org.janusgraph.graphdb.query.condition.Or) Builder(org.apache.lucene.search.BooleanQuery.Builder) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) Mapping(org.janusgraph.core.schema.Mapping) NormsFieldExistsQuery(org.apache.lucene.search.NormsFieldExistsQuery) Builder(org.apache.lucene.search.BooleanQuery.Builder) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation) RegexpQuery(org.apache.lucene.search.RegexpQuery) JanusGraphPredicate(org.janusgraph.graphdb.query.JanusGraphPredicate) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation) UUID(java.util.UUID) PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) Condition(org.janusgraph.graphdb.query.condition.Condition) TermQuery(org.apache.lucene.search.TermQuery) PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Cmp(org.janusgraph.core.attribute.Cmp) Instant(java.time.Instant) Geoshape(org.janusgraph.core.attribute.Geoshape) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Date(java.util.Date) DoublePoint(org.apache.lucene.document.DoublePoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) Geo(org.janusgraph.core.attribute.Geo) Not(org.janusgraph.graphdb.query.condition.Not) PrefixQuery(org.apache.lucene.search.PrefixQuery) And(org.janusgraph.graphdb.query.condition.And) FuzzyQuery(org.apache.lucene.search.FuzzyQuery)

Example 3 with JanusGraphPredicate

use of org.janusgraph.graphdb.query.JanusGraphPredicate 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 == null && predicate == Cmp.NOT_EQUAL) {
            return compat.exists(key);
        } else if (value instanceof Number) {
            Preconditions.checkArgument(predicate instanceof Cmp, "Relation not supported on numeric types: %s", predicate);
            return getRelationFromCmp((Cmp) predicate, key, value);
        } 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 && predicate != Cmp.EQUAL))) {
                fieldName = getDualMappingName(key);
            } else {
                fieldName = key;
            }
            if (predicate == Text.CONTAINS || predicate == Cmp.EQUAL) {
                return compat.match(fieldName, value);
            } else if (predicate == Text.NOT_CONTAINS) {
                return compat.boolMust(ImmutableList.of(compat.exists(fieldName), compat.boolMustNot(compat.match(fieldName, value))));
            } else if (predicate == Text.CONTAINS_PHRASE) {
                return compat.matchPhrase(fieldName, value);
            } else if (predicate == Text.NOT_CONTAINS_PHRASE) {
                return compat.boolMust(ImmutableList.of(compat.exists(fieldName), compat.boolMustNot(compat.matchPhrase(fieldName, 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.NOT_CONTAINS_PREFIX) {
                if (!ParameterType.TEXT_ANALYZER.hasParameter(information.get(key).getParameters()))
                    value = ((String) value).toLowerCase();
                return compat.boolMust(ImmutableList.of(compat.exists(fieldName), compat.boolMustNot(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.NOT_CONTAINS_REGEX) {
                if (!ParameterType.TEXT_ANALYZER.hasParameter(information.get(key).getParameters()))
                    value = ((String) value).toLowerCase();
                return compat.boolMust(ImmutableList.of(compat.exists(fieldName), compat.boolMustNot(compat.regexp(fieldName, value))));
            } else if (predicate == Text.PREFIX) {
                return compat.prefix(fieldName, value);
            } else if (predicate == Text.NOT_PREFIX) {
                return compat.boolMust(ImmutableList.of(compat.exists(fieldName), compat.boolMustNot(compat.prefix(fieldName, value))));
            } else if (predicate == Text.REGEX) {
                return compat.regexp(fieldName, value);
            } else if (predicate == Text.NOT_REGEX) {
                return compat.boolMust(ImmutableList.of(compat.exists(fieldName), compat.boolMustNot(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 == Text.NOT_FUZZY || predicate == Text.NOT_CONTAINS_FUZZY) {
                return compat.boolMust(ImmutableList.of(compat.exists(fieldName), compat.boolMustNot(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: %s", 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: %s", 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: %s", predicate);
            if (value instanceof Instant) {
                value = Date.from((Instant) value);
            }
            return getRelationFromCmp((Cmp) predicate, key, value);
        } 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) Arrays(java.util.Arrays) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) Date(java.util.Date) Spliterators(java.util.Spliterators) ES_LANG_KEY(org.janusgraph.diskstorage.es.ElasticSearchConstants.ES_LANG_KEY) LoggerFactory(org.slf4j.LoggerFactory) ConfigOption(org.janusgraph.diskstorage.configuration.ConfigOption) Geoshape(org.janusgraph.core.attribute.Geoshape) AbstractESCompat(org.janusgraph.diskstorage.es.compat.AbstractESCompat) ESCompatUtils(org.janusgraph.diskstorage.es.compat.ESCompatUtils) 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) Map(java.util.Map) IndexQuery(org.janusgraph.diskstorage.indexing.IndexQuery) LinkedListMultimap(com.google.common.collect.LinkedListMultimap) And(org.janusgraph.graphdb.query.condition.And) Mapping(org.janusgraph.core.schema.Mapping) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) 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) Sets(com.google.common.collect.Sets) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) List(java.util.List) ES_SCRIPT_KEY(org.janusgraph.diskstorage.es.ElasticSearchConstants.ES_SCRIPT_KEY) Parameter(org.janusgraph.core.schema.Parameter) Stream(java.util.stream.Stream) INDEX_NAME(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.INDEX_NAME) 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) ConfigNamespace(org.janusgraph.diskstorage.configuration.ConfigNamespace) Condition(org.janusgraph.graphdb.query.condition.Condition) AttributeUtils(org.janusgraph.graphdb.database.serialize.AttributeUtils) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) Function(java.util.function.Function) 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) 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) Iterator(java.util.Iterator) 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) DefaultTransaction(org.janusgraph.diskstorage.util.DefaultTransaction) ES_DOC_KEY(org.janusgraph.diskstorage.es.ElasticSearchConstants.ES_DOC_KEY) Text(org.janusgraph.core.attribute.Text) BaseTransactionConfig(org.janusgraph.diskstorage.BaseTransactionConfig) ESScriptResponse(org.janusgraph.diskstorage.es.script.ESScriptResponse) ConfigOption.disallowEmpty(org.janusgraph.diskstorage.configuration.ConfigOption.disallowEmpty) Preconditions(com.google.common.base.Preconditions) IndexMapping(org.janusgraph.diskstorage.es.mapping.IndexMapping) ParameterType(org.janusgraph.graphdb.types.ParameterType) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) INDEX_NS(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.INDEX_NS) IndexMutation(org.janusgraph.diskstorage.indexing.IndexMutation) Or(org.janusgraph.graphdb.query.condition.Or) Mapping(org.janusgraph.core.schema.Mapping) IndexMapping(org.janusgraph.diskstorage.es.mapping.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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 4 with JanusGraphPredicate

use of org.janusgraph.graphdb.query.JanusGraphPredicate in project janusgraph by JanusGraph.

the class TinkerPopTextWrappingPredicateTest method shouldReturnNegatedWrappedTestResultForTestAfterNegate.

@Test
public void shouldReturnNegatedWrappedTestResultForTestAfterNegate() {
    TinkerPopTextWrappingPredicate wrappingPredicate = new TinkerPopTextWrappingPredicate(Text.containing);
    JanusGraphPredicate negated = wrappingPredicate.negate();
    assertEquals(TextP.containing("ark").negate().test("marko"), negated.test("marko", "ark"));
}
Also used : JanusGraphPredicate(org.janusgraph.graphdb.query.JanusGraphPredicate) Test(org.junit.jupiter.api.Test)

Example 5 with JanusGraphPredicate

use of org.janusgraph.graphdb.query.JanusGraphPredicate 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 == null && predicate == Cmp.NOT_EQUAL) {
            return key + ":*";
        } else if (value instanceof Number) {
            final String queryValue = escapeValue(value);
            Preconditions.checkArgument(predicate instanceof Cmp, "Relation not supported on numeric types: %s", 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 || predicate == Cmp.NOT_EQUAL) {
                final String tokenizer = ParameterType.STRING_ANALYZER.findParameter(information.get(key).getParameters(), null);
                if (tokenizer != null) {
                    return tokenize(information, value, key, predicate, tokenizer);
                } else if (predicate == Cmp.EQUAL) {
                    return (key + ":\"" + escapeValue(value) + "\"");
                } else {
                    // Cmp.NOT_EQUAL case
                    return ("-" + key + ":\"" + escapeValue(value) + "\"");
                }
            } else if (predicate == Text.FUZZY || predicate == Text.CONTAINS_FUZZY) {
                return (key + ":" + escapeValue(value) + "~" + Text.getMaxEditDistance(value.toString()));
            } 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: %s", predicate);
            Preconditions.checkArgument(map == Mapping.PREFIX_TREE || predicate == Geo.WITHIN || predicate == Geo.INTERSECT, "Relation not supported on geopoint types: %s", 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: %s", 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)

Aggregations

JanusGraphPredicate (org.janusgraph.graphdb.query.JanusGraphPredicate)6 Instant (java.time.Instant)4 Cmp (org.janusgraph.core.attribute.Cmp)4 Geoshape (org.janusgraph.core.attribute.Geoshape)4 Date (java.util.Date)3 UUID (java.util.UUID)3 Geo (org.janusgraph.core.attribute.Geo)3 Mapping (org.janusgraph.core.schema.Mapping)3 And (org.janusgraph.graphdb.query.condition.And)3 Condition (org.janusgraph.graphdb.query.condition.Condition)3 Not (org.janusgraph.graphdb.query.condition.Not)3 Or (org.janusgraph.graphdb.query.condition.Or)3 PredicateCondition (org.janusgraph.graphdb.query.condition.PredicateCondition)3 KeyInformation (org.janusgraph.diskstorage.indexing.KeyInformation)2 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Iterators (com.google.common.collect.Iterators)1 LinkedListMultimap (com.google.common.collect.LinkedListMultimap)1 Multimap (com.google.common.collect.Multimap)1