简体   繁体   English

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

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

I'm new to Neo4j and am using REST API's to create nodes and relationships. 我是Neo4j的新手,正在使用REST API创建节点和关系。 I've two nodes NA and NB and they are connected w/ a relationship RC. 我有两个节点NA和NB,它们通过关系RC连接。 'NA - RC - NB'. 'NA-RC-NB'。 Before I create the nodes and the relationship, I check to see if the nodes and the relationship between them do not exist. 在创建节点和关系之前,请检查节点和它们之间的关系是否不存在。 I figure out how to check if a node exists and am struggling w/ how to check if a relationship between the two nodes exist. 我弄清楚了如何检查一个节点是否存在,并且正在努力检查两个节点之间的关系是否存在。 I came up w/ this Cypher query. 我想出了这个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)"

The nodes have a property 'name'. 节点具有属性“名称”。 I'm getting empty 'data: []' when I execute this query. 执行此查询时,我得到的是空的“数据:[]”。

Any suggestions? 有什么建议么? I tried looking into the Neo4j documentation and some tutorial but not quite able to figure this out. 我尝试查看Neo4j文档和一些教程,但不太能弄清楚这一点。

TIA TIA

Here is the java code: 这是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;
}

The type parameter is listed as {type} , but defined as "rtype" in the parameter map. 类型参数列为{type} ,但在参数映射中定义为"rtype" Does that fix it for you? 这为您解决了吗? You might try the query without the parameters (just hard code them in), to see if it works. 您可以尝试不使用参数的查询(只需对其进行硬编码),以查看其是否有效。

maybe you could make it different with the RELATE command: http://docs.neo4j.org/chunked/1.8.M03/query-relate.html 也许您可以使用RELATE命令使其与众不同: http ://docs.neo4j.org/chunked/1.8.M03/query-relate.html

this way there is no need to check whether or not the relation already exists. 这样,无需检查该关系是否已经存在。 simply, if it doesn't then it creates one. 简单的说,如果没有,那么它会创建一个。

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

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