此问题在此处已有答案:
Neo4j return nodes connected by relationships of a given label and property(2个答案)
26天前关闭。
我怀疑我遇到了一个bug,使用Go SDK查询时,得到的数据结果与Neo4j浏览器中的不一致,缺少几个Relationship。
Neo4j版本:Neo4j内核5.12.0社区
Neo4j模式:单示例
驱动程序版本:Go driver v5.12.0
操作系统:centos
我的密码是
MATCH (n1:AAA_IngressOfSecurityGroup {AssetsAccountId:"aaaa"})-[:IngressOfSecurityGroup]->(n2:AAA_SecurityGroup {AssetsAccountId:"aaaa"})-[:SecurityGroupOfNetworkInterface]->(n3:AAA_NetworkInterface {AssetsAccountId:"aaaa"})-[:NetworkInterfaceOfCvm]->(n4:AAA_CvmInstance {AssetsAccountId:"aaaa",AssetUniqueId:"aaaa-165d2740e7fa",AssetsVersion:"1702108924"}) WHERE n4.InstanceState = 'RUNNING' AND n4.RestrictState = 'NORMAL' AND n1.Action = 'ACCEPT' AND (n1.Ipv6CidrBlock = "::/0" OR n1.CidrBlock = "0.0.0.0/0") AND toLower(n1.Port) = 'all' AND (toLower(n1.Protocol) = 'tcp' or toLower(n1.Protocol) = 'udp' or toLower(n1.Protocol)= 'all' ) WITH n1, n2, n3, n4 WHERE ALL (groupId IN n3.GroupSet WHERE NOT EXISTS { MATCH (n2_other:AAA_SecurityGroup {AssetsAccountId:"aaaa", SecurityGroupId: groupId})-[:IngressOfSecurityGroup]->(n1_other:AAA_IngressOfSecurityGroup) WHERE n1_other.Action = 'DROP' AND ((n1_other.CidrBlock = "0.0.0.0/0" OR n1_other.Ipv6CidrBlock = "::/0") AND toLower(n1_other.Port) = 'all') AND NOT EXISTS { MATCH (n1_smaller:AAA_IngressOfSecurityGroup)-[:IngressOfSecurityGroup]->(n2_other) WHERE toInteger(n1_smaller.PolicyIndex) < toInteger(n1_other.PolicyIndex) } } ) OPTIONAL MATCH p2 = (n7:AAA_PublicIp {AssetsAccountId:"aaaa"})-[:PublicIpToVpc]->(n6:AAA_Vpc {AssetsAccountId:"aaaa"})-[:SubnetsInVPC]->(n5:AAA_Subnet {AssetsAccountId:"aaaa"})-[:NetworkInterfaceInSubnet]->(n3) WHERE p2 IS NOT NULL OPTIONAL MATCH p3 = (n8:AAA_Eip {AssetsAccountId:"aaaa"})-[:EipOToVpc]->(n6)-[:SubnetsInVPC]->(n5)-[:NetworkInterfaceInSubnet]->(n3) WHERE p3 IS NOT NULL WITH DISTINCT n4, n1, n2, n3, n6, n7, p2, p3 RETURN n1, n2, n3,n4, n6, n7, p2, p3
字符串
我用来处理从Neo4j返回的数据的代码如下。
result, session, err := global.Neo4j.RunCypherQuery(body.Cypher, nil)
if err != nil {
logger.Log.Error("RunCypherQuery error: " + err.Error())
response := responses.NewErrorResponse(responses.StatusCodeUnknownErr, err.Error())
c.JSON(http.StatusOK, response)
return
}
var nodes []neo4j.Node
var relationships []neo4j.Relationship
seenNodes := make(map[int64]bool)
seenRelationships := make(map[int64]bool)
for result.Next() {
record := result.Record()
for _, value := range record.Values {
if value == nil {
continue
}
switch v := value.(type) {
case neo4j.Node:
if _, found := seenNodes[v.Id]; !found {
seenNodes[v.GetId()] = true
nodes = append(nodes, v)
}
case neo4j.Relationship:
if _, found := seenRelationships[v.Id]; !found {
seenRelationships[v.GetId()] = true
relationships = append(relationships, v)
}
case neo4j.Path:
for _, node := range v.Nodes {
if _, found := seenNodes[node.Id]; !found {
seenNodes[node.GetId()] = true
nodes = append(nodes, node)
}
}
for _, rel := range v.Relationships {
if _, found := seenRelationships[rel.Id]; !found {
seenRelationships[rel.GetId()] = true
relationships = append(relationships, rel)
}
}
case []interface{}:
for _, item := range v {
switch elem := item.(type) {
case neo4j.Node:
if _, found := seenNodes[elem.GetId()]; !found {
seenNodes[elem.GetId()] = true
nodes = append(nodes, elem)
}
case neo4j.Relationship:
if _, found := seenRelationships[elem.GetId()]; !found {
seenRelationships[elem.GetId()] = true
relationships = append(relationships, elem)
}
default:
logger.Log.Error("Unexpected type in []interface{}: %T", item)
response := responses.NewErrorResponse(responses.StatusCodeUnknownErr, errors.New(fmt.Sprintf("Unexpected type in []interface{}: %T", item)).Error())
c.JSON(http.StatusOK, response)
return
}
}
default:
logger.Log.Error("Unexpected type: %T", v)
response := responses.NewErrorResponse(responses.StatusCodeUnknownErr, errors.New(fmt.Sprintf("Unexpected type: %T", v)).Error())
c.JSON(http.StatusOK, response)
return
}
}
}
func (c *Client) RunCypherQuery(query string, params map[string]interface{}) (neo4j.Result, neo4j.Session, error) {
session := c.driver.NewSession(neo4j.SessionConfig{})
fmt.Println(query)
result, err := session.Run(query, params)
if err != nil {
return nil, session, err
}
return result, session, nil
}
的数据
在neo4j桌面搜索结果是
的数据
在我代码中搜索结果是
的
标有红色“X”的位置表示未找到结果。
在RunCypherQuery方法中,在返回的结果中找不到这三个Relationship的数据。
1条答案
按热度按时间bvjxkvbb1#
在最上面的MATCH语句中,我没有为Relationships定义名称,比如r1,所以返回的数据不包括这些关系。奇怪的是,浏览器设法使用未命名的Cypher返回数据,这可能涉及一些幕后处理。通过修改Cypher查询,我解决了这个问题。