From 26ac6645e29d126b00ed9a88c190305c34b12b22 Mon Sep 17 00:00:00 2001 From: Jermy Li Date: Tue, 5 Jan 2021 14:55:07 +0800 Subject: [PATCH] support P.textcontains() for rest-api query (#1312) how to use: /graphs/hugegraph/graph/vertices?properties={"city":"P.textcontains(\"shanghai\")"} improve: #1309 Change-Id: I2d7c0b8c58c6ac53cd6b99414859cb0510393f6c --- .../java/com/baidu/hugegraph/backend/query/Condition.java | 4 ++++ .../baidu/hugegraph/backend/tx/GraphIndexTransaction.java | 4 +++- .../baidu/hugegraph/traversal/optimize/ConditionP.java | 2 +- .../baidu/hugegraph/traversal/optimize/TraversalUtil.java | 8 ++++++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Condition.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Condition.java index 75213d67c3..4509202a6d 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Condition.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/Condition.java @@ -382,6 +382,10 @@ public static Relation textContainsAny(Id key, Set words) { return new UserpropRelation(key, RelationType.TEXT_CONTAINS_ANY, words); } + public static Condition contains(Id key, Object value) { + return new UserpropRelation(key, RelationType.CONTAINS, value); + } + /** * Condition defines */ diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java index 9a460bec2a..8b856f9b39 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java @@ -86,7 +86,6 @@ import com.baidu.hugegraph.type.define.Action; import com.baidu.hugegraph.type.define.HugeKeys; import com.baidu.hugegraph.type.define.IndexType; -import com.baidu.hugegraph.type.define.SchemaStatus; import com.baidu.hugegraph.util.CollectionUtil; import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.InsertionOrderUtil; @@ -1359,6 +1358,9 @@ private static NoIndexException noIndexException(HugeGraph graph, if (query.hasNeqCondition()) { mismatched.add("not-equal"); } + if (mismatched.isEmpty()) { + mismatched.add(query.relations().toString()); + } return new NoIndexException("Don't accept query based on properties " + "%s that are not indexed in %s, " + "may not match %s condition", diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/ConditionP.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/ConditionP.java index 206e3de4e8..6a60c3f88f 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/ConditionP.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/ConditionP.java @@ -34,7 +34,7 @@ private ConditionP(final BiPredicate predicate, super(predicate, value); } - public static ConditionP textContains(String value) { + public static ConditionP textContains(Object value) { return new ConditionP(RelationType.TEXT_CONTAINS, value); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java index a19bc63ee5..385c8a0565 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/traversal/optimize/TraversalUtil.java @@ -806,6 +806,8 @@ public static P parsePredicate(String predicate) { return P.outside(params[0], params[1]); case "within": return P.within(predicateArgs(value)); + case "textcontains": + return ConditionP.textContains(predicateArg(value)); case "contains": // Just for inner use case like auth filter return ConditionP.contains(predicateArg(value)); @@ -868,6 +870,12 @@ public static Condition parsePredicate(PropertyKey pk, String predicate) { validValues.add(validPropertyValue(v, pk)); } return Condition.in(pk.id(), validValues); + case "textcontains": + validValue = validPropertyValue(value, pk); + return Condition.textContains(pk.id(), (String) validValue); + case "contains": + validValue = validPropertyValue(value, pk); + return Condition.contains(pk.id(), validValue); default: throw new NotSupportException("predicate '%s'", method); }