我刚刚开始使用neo4j和cypher。我正在构建一个数据库来存储不同化学品的名称和同义词。我目前这样做的方式是这样的:
CREATE CONSTRAINT ON (flow:Flow) ASSERT flow.name IS UNIQUE;
CREATE (a:Flow {name: "Ethene"})-[:synonym]->(b:Flow {name: "Ethylene"});
(即使同义词是双向关系,cypher语法也需要-->
语法。
现在 - 说我想添加一个新流程,它是两者的同义词,
CREATE (c:Flow {name: "C2H4"})-[:synonym]->(b:Flow {name: "Ethylene"})
之后,我将有3个节点处于线性关系(a)-[:synonym]->(b)<-[:synonym]-(c)
。但同义词是可传递的,因此同义词的图形应该是完整的(a-[:synonym]-c
关系缺失)
有没有办法自动创建与节点和所有节点的同义词的:synonym
关系?
以下似乎有效,除了它感觉笨拙并且也不是幂等的(如果它再次运行,它会创建c-->c
关系):
// create a new synonym to 'Ethylene' with the name 'C2H04'
MATCH (j:Flow {name:"Ethylene"})-[:synonym]-(k)
WITH j,k
MERGE (c:Flow {name:"C2H04"})-[:synonym]->(j)
MERGE (c)-[:synonym]->(k)
有没有更好的方法来创建这些关系?或者,有没有办法折叠所有同义词关系,以便查询将返回节点的所有同义词,即使它们没有直接连接?
答案 0 :(得分:1)
我建议更改模型。让我们添加一个中间节点,例如“synonym”来连接所有同义词:
(S:Synonym)
(A1:Flow {name: "Ethene"})-[:synonym]->(S)
(A2:Flow {name: "Ethylene"})-[:synonym]->(S)
(A3:Flow {name: "C2H4"})-[:synonym]->(S)
创建示例查询:
MERGE (A1:Flow {name: "Ethene"})
MERGE (A2:Flow {name: "Ethylene"})
MERGE (A3:Flow {name: "C2H4"})
MERGE (A1)-[:synonym]->(S:Synonym)
MERGE (A2)-[:synonym]->(S)
MERGE (A3)-[:synonym]->(S)
MERGE (A11:Flow {name: "Ferrum"})
MERGE (A12:Flow {name: "Iron"})
MERGE (A13:Flow {name: "Fe"})
MERGE (A11)-[:synonym]->(S2:Synonym)
MERGE (A12)-[:synonym]->(S2)
MERGE (A13)-[:synonym]->(S2)
获取所有同义词组的示例查询:
MATCH (S:Synonym) WITH S
MATCH (S)<-[:synonym]-(F:Flow)
WITH S, collect(F) as synonyms
RETURN synonyms
获取化学品的所有同义词的示例查询:
MATCH (A:Flow {name:'Ethene'})
OPTIONAL MATCH (A)-[:synonym]->(S:Synonym)<-[:synonym]-(F:Flow)
RETURN A as chemical, collect(F) as synonyms
添加新化学品作为现有化学品的同义词:
MATCH (A:Flow {name:'C2H4'})
MERGE (A)-[:synonym]->(S:Synonym)
MERGE (New:Flow {name:'Aethylen'})
MERGE (New)-[:synonym]->(S)
<强> 更新 强>:
如果您不想更改模型,可以使用具有可变路径长度的查询来获取化学品的所有同义词:
MATCH (A:Flow {name: "Ethylene"})
OPTIONAL MATCH (A)-[:synonym*]-(F:Flow)
RETURN A as chemical, collect(F) as synonyms
答案 1 :(得分:1)
如果同一化学物质的所有同义词都存在于单个节点内,那么总体来说会简单得多。例如:
componentWillReceiveProps(nProps){
console.log(this.props.count); // logs 1
console.log(nProps.count); // logs 6
}