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

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

问题描述

我正在用Java嵌入NEO4J. 假设有一个名为NODE_abc的节点,它具有一些属性. 我想选择节点,以便可以使用getProperty()获取属性.

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().

我想选择NODE_abc作为mynode,以便可以使用mynode.getProperty()获取节点"NODE_abc"的属性.

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".

节点"NODE_abc"的名称存储在一个变量中, 说String str ="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).

通常,在Neo4J中,如果需要通过属性(而不是通过遍历)查询节点,则可以创建索引.例如,您可以创建一个名为"actors"的索引:

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.

但是,与SQL不同,在Neo4J中,您必须手动将节点添加到索引:

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" ) );

然后,您可以查询与指定查询匹配的 all 个节点的索引(索引不保证唯一性):

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();

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

请注意,Neo4J索引实际上并不从节点读取属性,您必须明确告诉它如何为节点建立索引.您可以提供未存储在节点上的要索引的任意信息,但我不建议这样做.

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天全站免登陆