簡體   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