Search in sources :

Example 21 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class TraversalUtil method convOr.

public static Condition convOr(HugeGraph graph, HugeType type, HasContainer has) {
    P<?> p = has.getPredicate();
    assert p instanceof OrP;
    @SuppressWarnings("unchecked") List<P<Object>> predicates = ((OrP<Object>) p).getPredicates();
    if (predicates.size() < 2) {
        throw newUnsupportedPredicate(p);
    }
    Condition cond = null;
    for (P<Object> predicate : predicates) {
        HasContainer newHas = new HasContainer(has.getKey(), predicate);
        Condition newCond = convHas2Condition(newHas, type, graph);
        if (cond == null) {
            cond = newCond;
        } else {
            cond = Condition.or(newCond, cond);
        }
    }
    return cond;
}
Also used : AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP) P(org.apache.tinkerpop.gremlin.process.traversal.P) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) Condition(com.baidu.hugegraph.backend.query.Condition) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP)

Example 22 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class TraversalUtil method fillConditionQuery.

public static ConditionQuery fillConditionQuery(ConditionQuery query, List<HasContainer> hasContainers, HugeGraph graph) {
    HugeType resultType = query.resultType();
    for (HasContainer has : hasContainers) {
        Condition condition = convHas2Condition(has, resultType, graph);
        query.query(condition);
    }
    return query;
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) HugeType(com.baidu.hugegraph.type.HugeType)

Example 23 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class TraversalUtil method convHas2Condition.

public static Condition convHas2Condition(HasContainer has, HugeType type, HugeGraph graph) {
    P<?> p = has.getPredicate();
    E.checkArgument(p != null, "The predicate of has(%s) is null", has);
    BiPredicate<?, ?> bp = p.getBiPredicate();
    Condition condition;
    if (keyForContainsKeyOrValue(has.getKey())) {
        condition = convContains2Relation(graph, has);
    } else if (bp instanceof Compare) {
        condition = convCompare2Relation(graph, type, has);
    } else if (bp instanceof RelationType) {
        condition = convRelationType2Relation(graph, type, has);
    } else if (bp instanceof Contains) {
        condition = convIn2Relation(graph, type, has);
    } else if (p instanceof AndP) {
        condition = convAnd(graph, type, has);
    } else if (p instanceof OrP) {
        condition = convOr(graph, type, has);
    } else {
        // TODO: deal with other Predicate
        throw newUnsupportedPredicate(p);
    }
    return condition;
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) RelationType(com.baidu.hugegraph.backend.query.Condition.RelationType) Contains(org.apache.tinkerpop.gremlin.process.traversal.Contains) Compare(org.apache.tinkerpop.gremlin.process.traversal.Compare) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP)

Example 24 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class TraversalUtil method convAnd.

public static Condition convAnd(HugeGraph graph, HugeType type, HasContainer has) {
    P<?> p = has.getPredicate();
    assert p instanceof AndP;
    @SuppressWarnings("unchecked") List<P<Object>> predicates = ((AndP<Object>) p).getPredicates();
    if (predicates.size() < 2) {
        throw newUnsupportedPredicate(p);
    }
    Condition cond = null;
    for (P<Object> predicate : predicates) {
        HasContainer newHas = new HasContainer(has.getKey(), predicate);
        Condition newCond = convHas2Condition(newHas, type, graph);
        if (cond == null) {
            cond = newCond;
        } else {
            cond = Condition.and(newCond, cond);
        }
    }
    return cond;
}
Also used : AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) OrP(org.apache.tinkerpop.gremlin.process.traversal.util.OrP) P(org.apache.tinkerpop.gremlin.process.traversal.P) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) Condition(com.baidu.hugegraph.backend.query.Condition) AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)

Example 25 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class GraphIndexTransaction method constructSearchQuery.

private ConditionQuery constructSearchQuery(ConditionQuery query, MatchedIndex index) {
    ConditionQuery newQuery = query;
    Set<Id> indexFields = new HashSet<>();
    // Convert has(key, text) to has(key, textContainsAny(word1, word2))
    for (IndexLabel il : index.indexLabels()) {
        if (il.indexType() != IndexType.SEARCH) {
            continue;
        }
        Id indexField = il.indexField();
        String fieldValue = (String) newQuery.userpropValue(indexField);
        Set<String> words = this.segmentWords(fieldValue);
        indexFields.add(indexField);
        newQuery = newQuery.copy();
        newQuery.unsetCondition(indexField);
        newQuery.query(Condition.textContainsAny(indexField, words));
    }
    // Register results filter to compare property value and search text
    newQuery.registerResultsFilter(element -> {
        assert element != null;
        for (Condition cond : query.conditions()) {
            Object key = cond.isRelation() ? ((Relation) cond).key() : null;
            if (key instanceof Id && indexFields.contains(key)) {
                // This is an index field of search index
                Id field = (Id) key;
                HugeProperty<?> property = element.getProperty(field);
                String propValue = propertyValueToString(property.value());
                String fieldValue = (String) query.userpropValue(field);
                if (this.matchSearchIndexWords(propValue, fieldValue)) {
                    continue;
                }
                return false;
            }
            if (!cond.test(element)) {
                return false;
            }
        }
        return true;
    });
    return newQuery;
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Id(com.baidu.hugegraph.backend.id.Id) HashSet(java.util.HashSet)

Aggregations

Condition (com.baidu.hugegraph.backend.query.Condition)49 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)29 Test (org.junit.Test)29 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)16 ArrayList (java.util.ArrayList)13 Id (com.baidu.hugegraph.backend.id.Id)12 Collection (java.util.Collection)8 Date (java.util.Date)8 SyspropRelation (com.baidu.hugegraph.backend.query.Condition.SyspropRelation)6 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)4 RangeConditions (com.baidu.hugegraph.backend.query.Condition.RangeConditions)4 Relation (com.baidu.hugegraph.backend.query.Condition.Relation)4 HugeException (com.baidu.hugegraph.HugeException)3 IdPrefixQuery (com.baidu.hugegraph.backend.query.IdPrefixQuery)3 IdRangeQuery (com.baidu.hugegraph.backend.query.IdRangeQuery)3 HugeType (com.baidu.hugegraph.type.HugeType)3 Directions (com.baidu.hugegraph.type.define.Directions)3 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)3 AndP (org.apache.tinkerpop.gremlin.process.traversal.util.AndP)3 OrP (org.apache.tinkerpop.gremlin.process.traversal.util.OrP)3