简体   繁体   English

py2neo如何根据节点的属性检索节点?

[英]py2neo how to retrieve a node based on node's property?

I've found related methods: 我找到了相关的方法:

  1. find - doesn't work because this version of neo4j doesn't support labels. find - 不起作用,因为此版本的neo4j不支持标签。
  2. match - doesn't work because I cannot specify a relation, because the node has no relations yet. match - 不起作用,因为我无法指定关系,因为该节点还没有关系。
  3. match_one - same as match . match_one - 与match相同。
  4. node - doesn't work because I don't know the id of the node. node - 不起作用,因为我不知道节点的id。

I need an equivalent of: 我需要相当于:

start n = node(*) where n.name? = "wvxvw" return n;

Cypher query. Cypher查询。 Seems like it should be basic, but it really isn't... 看起来它应该是基本的,但它确实不是......

PS. PS。 I'm opposed to using Cypher for too many reasons to mention. 我反对使用Cypher提出太多理由。 So that's not an option either. 所以这也不是一个选择。

Well, you should create indexes so that your start nodes are reduced. 那么,您应该创建索引,以便减少起始节点。 This will be automatically taken care of with the use of labels, but in the meantime, there can be a work around. 这将通过使用标签自动处理,但与此同时,可以解决这个问题。

  1. Create an index, say "label", which will have keys pointing to the different types of nodes you will have (in your case, say 'Person') 创建一个索引,比如说“label”,它将指向你将拥有的不同类型节点的键(在你的情况下,说'Person')
  2. Now while searching you can write the following query : 现在,在搜索时,您可以编写以下查询:

      START n = node:label(key_name='Person') WHERE n.name = 'wvxvw' RETURN n; //key_name is the key's name you will assign while creating the node. 

user797257 seems to be out of the game, but I think this could still be useful: user797257似乎已退出游戏,但我认为这仍然有用:

If you want to get nodes, you need to create an index. 如果要获取节点,则需要创建索引。 An index in Neo4j is the same as in MySQL or any other database (If I understand correctly). Neo4j中的索引与MySQL或任何其他数据库中的索引相同(如果我理解正确的话)。 Labels are basically auto-indexes, but an index offers additional speed. 标签基本上是自动索引,但索引提供了额外的速度。 (I use both). (我同时使用)。

somewhere on top, or in neo4j itself create an index: 在某个地方,或在neo4j本身创建一个索引:

index = graph_db.get_or_create_index(neo4j.Node, "index_name")

Then, create your node as usual, but do add it to the index: 然后,像往常一样创建节点,但是将其添加到索引中:

new_node = batch.create(node({"key":"value"}))
batch.add_indexed_node(index, "key", "value", new_node)

Now, if you need to find your new_node, execute this: 现在,如果您需要找到new_node,请执行以下命令:

 new_node_ref = index.get("key", "value")

This returns a list. 这将返回一个列表。 new_node_ref[0] has the top item, in case you want/expect a single node. new_node_ref[0]具有顶部项目,以防您希望/期望单个节点。

use selector to obtain node from the graph The following code fetches the first node from list of nodes matching the search 使用selector从图中获取节点以下代码从与搜索匹配的节点列表中获取第一个节点

selector = NodeSelector(graph)
node = selector.select("Label",key='value')
nodelist=list(node)
m_node=node.first()

using py2neo , this hacky function will iterate through the properties and values and labels gradually eliminating all nodes that don't match each criteria submitted. 使用py2neo ,这个hacky函数将迭代属性和值,并逐渐消除所有与提交的每个条件不匹配的节点。 The final result will be a list of all (if any) nodes that match all the properties and labels supplied. 最终结果将是与所有提供的属性和标签匹配的所有(如果有)节点的列表。

def find_multiProp(graph, *labels, **properties):
    results = None
    for l in labels:
        for k,v in properties.iteritems():
            if results == None:
                genNodes = lambda l,k,v: graph.find(l, property_key=k, property_value=v)
                results = [r for r in genNodes(l,k,v)]
                continue
            prevResults = results
            results = [n for n in genNodes(l,k,v) if n in prevResults]
    return results

see my other answer for creating a merge_one() that will accept multiple properties... 看到我创建一个接受多个属性的merge_one() 其他答案 ......

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

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