检查neo4j图中是否存在节点 [英] Check whether nodes exist in a neo4j graph

查看:730
本文介绍了检查neo4j图中是否存在节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意

我让这成为几个问题,而不是我问的简单问题,所以我将后续问题分解为自己的问题

I let this become several questions instead of the simple one I asked, so I am breaking the follow-ups off into their own question here.

原始问题

我收到的ID列表是我首先要测试的一个ID,是否在我的图形中,以及是否正在处理这些节点.

I'm receiving a list of IDs that I am first testing whether any of them are in my graph, and if they /are/ I am processing those nodes further.

例如,...

fids = get_fids(record)  # [100001, 100002, 100003, ... etc]
ids_in_my_graph = filter(id_is_in_graph, fids) # [100002]

def id_is_in_graph(id):
    val = False
    query = """MATCH (user:User {{id_str:"{}"}})
    RETURN user
    """.format(id)
    n=neo4j.CypherQuery(graph_db,query).execute_one()
    if n:
        val = True
    return(val)

您可以想象,通过过滤器执行此操作,顺序测试我的图形中的每个ID是否真的,真的很慢,并且显然使用neo4j不合适.

As you can imagine, doing this with filter, sequentially testing whether each ID is in my graph is really, really slow, and is clearly not properly using neo4j.

如何重新定义查询,以便创建类似(User{id_str: [mylist]})的列表来查询并仅返回图形中的ID?

How would I rephrase my query such that I could create a list like (User{id_str: [mylist]}) to query and return only IDs that are in my graph?

推荐答案

您可能想通过利用cypher的收集功能来使用WHERE ... IN. 此处是相关参考

You may want to use WHERE...IN by exploiting the collection functionality of cypher. Here's the relevant reference

因此您的查询可能如下所示:

So your query might look like this:

MATCH (user:User) 
WHERE user.id_str IN ["100001", "100002", "100003"]
return user;

现在,我不知道一个集合有多大.我怀疑如果您的收藏夹中有1,000件物品,这是否可行.但这至少是将它们分批处理的一种方法.这样可以提高性能.

Now, I don't know how large a collection can be. I doubt this would work if your collection had 1,000 items in it. But at least this is a way of batching them up into chunks. This should improve performance.

还可以查看 Cypher 2.0 refcard

这篇关于检查neo4j图中是否存在节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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