incubator-hugegraph icon indicating copy to clipboard operation
incubator-hugegraph copied to clipboard

[Question] HugeGraph 的 properties 支持多种比较操作符么?

Open Tommu10ve opened this issue 7 months ago • 4 comments

Problem Type (问题类型)

None

Before submit

  • [x] 我已经确认现有的 IssuesFAQ 中没有相同 / 重复问题 (I have confirmed and searched that there are no similar problems in the historical issue and documents)

Environment (环境信息)

  • Server Version: 1.0.0 (Apache Release Version)
  • Backend: RocksDB x nodes, HDD or SSD
  • OS: xx CPUs, xx G RAM, Ubuntu 2x.x / CentOS 7.x
  • Data Size: xx vertices, xx edges

Your Question (问题描述)

操作符 示例 说明 = "city": "Beijing" 等于

"age": {">": 20} 大于 < "age": {"<": 30} 小于 != "name": {"!=": "Alice"} 不等于 in "age": {"in": [20, 30]} 在列表中 contains "tags": {"contains": "VIP"} 包含(集合属性) 类似这种?

Vertex/Edge example (问题点 / 边数据举例)


Schema [VertexLabel, EdgeLabel, IndexLabel] (元数据结构)


Tommu10ve avatar May 30 '25 04:05 Tommu10ve

HugeGraph 的 properties 支持多种比较操作符,包括你提到的 =, >, <, !=, in, 以及 contains 等。这些操作符在 HugeGraph 的查询系统中有明确实现,例如:

  • =(等于):如 "city": "Beijing"
  • >(大于):如 "age": {">": 20}
  • <(小于):如 "age": {"<": 30}
  • !=(不等于):如 "name": {"!=": "Alice"}
  • in(在列表中):如 "age": {"in": [20, 30]}
  • contains(集合属性包含):如 "tags": {"contains": "VIP"}

这些操作符在 HugeGraph 的底层通过 Condition.java 中的 RelationType 枚举实现,支持 EQ (==), GT (>), GTE (>=), LT (<), LTE (<=), NEQ (!=), IN (in), NOT_IN (notin), CONTAINS (contains), TEXT_CONTAINS (textcontains) 等多种操作符,适用于属性过滤和复杂查询需求。具体实现可参考 Condition.java 源码

对于集合属性(如 SET 或 LIST),可以使用 CONTAINSTEXT_CONTAINS 操作符进行查询。例如,Gremlin 查询中可以这样写:

g.V().hasLabel("org").has("alias", CONTAINS, "广州软件")

需要注意,使用 CONTAINSTEXT_CONTAINS 时,相关属性需建立 Search Index,否则无法生效。详细用法和注意事项可参考 相关 issue 讨论

此外,HugeGraph 支持二级索引、范围索引、分片索引和全文索引,分别对应不同类型的属性过滤和检索场景,进一步提升查询灵活性和性能。更多索引类型说明见 这里

如需具体语法或更多示例,可补充说明你的实际查询场景。

To reply, just mention my name @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

dosubot[bot] avatar May 30 '25 04:05 dosubot[bot]

谓词 说明 示例 P.gt(value) 大于 has("age", P.gt(28)) P.lt(value) 小于 has("price", P.lt(100)) P.eq(value) 等于 has("name", P.eq("Alice")) P.neq(value) 不等于 has("status", P.neq("deleted")) P.between(start, end) 介于 has("score", P.between(60, 90)) P.within(values...) 在列表中 has("role", P.within("admin", "user"))

参考代码在哪 @dosu-bot

Tommu10ve avatar May 30 '25 05:05 Tommu10ve

HugeGraph 对谓词 P.gt、P.lt、P.eq、P.neq、P.between、P.within 的支持,核心代码在 TraversalUtil.java 文件中实现,具体在 parsePredicate 方法里会将如 P.gt(18) 这样的字符串解析为 Gremlin 的谓词对象(P.eq, P.neq, P.lt, P.lte, P.gt, P.gte, P.between, P.within 等),并用于属性过滤。你可以在源码中看到如下实现:

TraversalUtil.java 相关代码片段:

switch (method) {
    case "eq":
        return P.eq(predicateNumber(value));
    case "neq":
        return P.neq(predicateNumber(value));
    case "lt":
        return P.lt(predicateNumber(value));
    case "lte":
        return P.lte(predicateNumber(value));
    case "gt":
        return P.gt(predicateNumber(value));
    case "gte":
        return P.gte(predicateNumber(value));
    case "between":
        Number[] params = predicateNumbers(value, 2);
        return P.between(params[0], params[1]);
    case "within":
        return P.within(predicateArgs(value));
    // ...
}

这些谓词在 RESTful API 和 Java Client 里都支持。例如,RESTful 查询可以这样写:

  • properties={"age":"P.gt(28)"}
  • properties={"score":"P.between(60,90)"}
  • properties={"role":"P.within('admin','user')"}

详细用法和所有支持的谓词列表可参考官方文档:Vertex APIEdge API

如果你需要 Java Client 侧的用法,可以参考 HugeGraph Java Client 文档

如需进一步查看谓词如何转为底层 Condition,可以继续阅读 TraversalUtil.java 里的 convCompare2UserpropRelationconvIn2Relation 方法实现。

To reply, just mention my name @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

dosubot[bot] avatar May 30 '25 05:05 dosubot[bot]

Due to the lack of activity, the current issue is marked as stale and will be closed after 20 days, any update will remove the stale label

github-actions[bot] avatar Jun 14 '25 21:06 github-actions[bot]