繁体   English   中英

Neo4j:REST API密码查询以查找两个节点之间的关系

[英]Neo4j: REST API Cypher Query to find relationship between two nodes

我是Neo4j的新手,正在使用REST API创建节点和关系。 我有两个节点NA和NB,它们通过关系RC连接。 'NA-RC-NB'。 在创建节点和关系之前,请检查节点和它们之间的关系是否不存在。 我弄清楚了如何检查一个节点是否存在,并且正在努力检查两个节点之间的关系是否存在。 我想出了这个Cypher查询。

"start x  = node(*), n = node(*) 
match x-[r]->n 
where (x.name? = {from} and type(r) = {rtype} and n.name? = {to}) 
return ID(r), TYPE(r)"

节点具有属性“名称”。 执行此查询时,我得到的是空的“数据:[]”。

有什么建议么? 我尝试查看Neo4j文档和一些教程,但不太能弄清楚这一点。

TIA

这是Java代码:

/** Check if a relationship exists between two nodes */
public boolean relationshipExists(String from /** node name */
, String to /** node name */
, String type) {
    boolean exists = false;

    /** check if relationship exists */
    String url = "http://localhost:7474/db/data/cypher";
    JSONObject jobject = new JSONObject();
    try {
        Map<String, String> params = new HashMap<String, String>();
        params.put("from", from);
        params.put("rtype", type);
        params.put("to", to);
        String query = "start x  = node(*), n = node(*) match x-[r]->n where (x.name? = {from} and type(r) = {rtype} and n.name? = {to}) return ID(r), TYPE(r)";
        jobject.put("query", query);
        jobject.put("params", params);
    } catch (JSONException e) {
        logger.error("Error", e);
    }

    String response = sendQuery(url, jobject.toString());

    try {
        jobject = new JSONObject(response);
        JSONArray data = (JSONArray) jobject.get("data");
        JSONArray next = null;
        for (int index = 0; index < data.length(); index++) {
            next = data.getJSONArray(index);
            if (!next.isNull(1) && next.getString(1).equalsIgnoreCase(type)) {
                exists = (next.getInt(0) > -1) ? true : false;
            }
        }
    } catch (JSONException e) {
        logger.error("Error", e);
    }

    return exists;
}

类型参数列为{type} ,但在参数映射中定义为"rtype" 这为您解决了吗? 您可以尝试不使用参数的查询(只需对其进行硬编码),以查看其是否有效。

也许您可以使用RELATE命令使其与众不同: http ://docs.neo4j.org/chunked/1.8.M03/query-relate.html

这样,无需检查该关系是否已经存在。 简单的说,如果没有,那么它会创建一个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM