我是Neo4j的新手,正在使用REST API创建节点和关系。我有两个节点NA和NB,并且它们与关系RC相连。 'NA - RC - NB'。在创建节点和关系之前,我检查节点及其之间的关系是否不存在。我弄清楚如何检查一个节点是否存在,以及如何检查两个节点之间是否存在关系。我想到了这个Cypher查询。Neo4j:REST API Cypher 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)"
节点有一个属性“名称”。当我执行这个查询时,我得到空'data:[]'。
有什么建议吗?我试图研究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;
}
看起来好像会起作用,尽管在名称字段上使用索引可能会更快,所以您可以在节点上启动而无需扫描整个图形。你能否提供更多的代码 - 特别是你传递参数图的部分? –
你可能是对的,因为当我拿出where子句时,我得到的结果包括我正在寻找的关系,并且在我无法指责的where子句中出现错误。感谢索引建议,我想在我的基本功能工作后添加它。 – user977505