在 Java 中的 NEO4J 中按名称选择节点 [英] Select a node by name in NEO4J in Java

查看:13
本文介绍了在 Java 中的 NEO4J 中按名称选择节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am working in NEO4J embedded in Java. Say there is a node named NODE_abc and it has a few properties. I want to select the node so that i can get the properties using getProperty().

I want to select the NODE_abc as mynode so that i can use mynode.getProperty() to get the properties of the node "NODE_abc".

The name of node "NODE_abc" is stored in a variable, say String str="NODE_abc"

解决方案

I agree with tstorms about indexing and execution of cypher queries. I would point out, however, that node identifiers are ephemeral. They are useful within a limited time window, but then are recycled. So, a node will not necessarily have the same identifier over multiple executions (or once the garbage collector has run).

Generally, in Neo4J, if you need to query nodes by properties (and not through traversal), you create an index. For example, you could create an index named "actors":

IndexManager index = graphDb.index();
Index<Node> actors = index.forNodes( "actors" );

If the index does not exist, this command will create it. Otherwise, it returns the existing index.

However, unlike SQL, in Neo4J you must manually add the node to the index:

Node reeves = graphDb.createNode();
reeves.setProperty( "name", "Keanu Reeves" );
actors.add( reeves, "name", reeves.getProperty( "name" ) );

You can then query the index for all nodes that match the specified query (indices do not guarantee uniqueness):

IndexHits<Node> hits = actors.get( "name", "Keanu Reeves" );
Node reeves = hits.getSingle();

Source: https://neo4j.com/docs/java-reference/current/indexing/#indexing-create

Note that Neo4J indices don't actually read properties from the node, you must explicitly tell it how to index the node. You could provide an arbitrary piece of information on which to index that isn't stored on the node, but I wouldn't recommend it.

这篇关于在 Java 中的 NEO4J 中按名称选择节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆