我正在构建一些C#代码以查询我的neo4j数据库,并使用Cytoscape.js在网页中显示结果。我正在尝试通过代码执行此查询:
`MATCH (n) WHERE toLower(n.Name) CONTAINS toLower('spoofing') WITH n MATCH (`n)-[r]-(otherNodes) RETURN n, r, otherNodes
当我通过neo4j浏览器运行此查询时,我得到58个节点,155个关系。这是我的C#代码,试图完成相同的查询:
strSearch = "toLower(n.Name) CONTAINS '" + txtSearchPhrase.Text.Trim().ToLower() + "'";
var test = client.Cypher
.Match("(n)")
.Where(strSearch)
.With("n")
.Match("(n)-[r]-(otherNodes)")
.Return((n, r, otherNodes) => new
{
n = n.As<NodeInfo>(),
r = Return.As<string>("type(r)"),
otherNodes = otherNodes.As<NodeInfo>() //otherNodes.CollectAs<NodeInfo>()
});
var results = test.Results.ToList();
for (int i = 0; i < results.Count; i++)
{
Type type = results[i].GetType();
//JSON node.
string strSource_id = results[i].n.Type + "_" + results[i].n.Id.ToString();
string strDest_id = results[i].otherNodes.Type + "_" + results[i].otherNodes.Id.ToString();
//Source.
clsWebJsonNode myclsWebJsonNode = new clsWebJsonNode() { Id = results[i].n.Id, id = strSource_id, label = strSource_id , title = results[i].n.Name, Type = results[i].n.Type, objBlob = results[i].n.objBlob, DescriptionSummaryObjective = results[i].n.DescriptionSummaryObjective, CVSS = results[i].n.CVSS, Status = results[i].n.Status, Abstraction = results[i].n.Abstraction };
NodeColor(ref myclsWebJsonNode);
string strNodeJson = JsonConvert.SerializeObject(myclsWebJsonNode, Newtonsoft.Json.Formatting.Indented);
myWebJson.AddNodeJson(strNodeJson);
//Destination.
myclsWebJsonNode = new clsWebJsonNode() { Id = results[i].otherNodes.Id, id = strDest_id, label = strDest_id , title = results[i].otherNodes.Name, Type = results[i].otherNodes.Type, objBlob = results[i].otherNodes.objBlob, DescriptionSummaryObjective = results[i].otherNodes.DescriptionSummaryObjective, CVSS = results[i].otherNodes.CVSS, Status = results[i].otherNodes.Status, Abstraction = results[i].otherNodes.Abstraction };
NodeColor(ref myclsWebJsonNode);
strNodeJson = JsonConvert.SerializeObject(myclsWebJsonNode, Newtonsoft.Json.Formatting.Indented);
myWebJson.AddNodeJson(strNodeJson);
//JSON link.
clsWebJsonLink myclsWebJsonLink = new clsWebJsonLink() { target = strSource_id, source = strDest_id, label = results[i].r, id = (strSource_id + "--" + results[i].r + "--*" + strDest_id) };
string strLinkJson = JsonConvert.SerializeObject(myclsWebJsonLink, Newtonsoft.Json.Formatting.Indented);
myWebJson.AddLinkJson(strLinkJson);
}
执行此代码时,我得到58个节点,103个关系。另外,我不确定如何真正知道关系的发展方向。我认为“ n”将被视为源(尾部),“ otherNodes”将被视为目的地(头)。但是我的许多关系方向与neo4j浏览器中显示的内容都不匹配。
有人对这里发生的事情有任何见识吗?
答案 0 :(得分:0)
首先,我建议像这样简化您的初始请求
MATCH (n)-[r]-(otherNodes)
WHERE toLower(n.Name) CONTAINS toLower('spoofing')
RETURN n, r, otherNodes
然后,如果您想像这样n
始终是您的起始节点,则必须为模式匹配提供指导(注意在匹配模式中添加>
)
MATCH (n)-[r]->(otherNodes)
WHERE toLower(n.Name) CONTAINS toLower('spoofing')
RETURN n, r, otherNodes