简体   繁体   English

计算Neo4j中的关系对

[英]Count relationship pairs in Neo4j

I'm starting dealing with Neo4j so I'm not really proficient on this topic. 我正在开始处理Neo4j,所以我对这个话题并不十分精通。 In figure below I have 2-mode (bipartite) graph where green node presents "document" and red node presents "term" which occur in particular document. 在下图中,我有2模式(二分图),其中绿色节点呈现“文档”,红色节点呈现在特定文档中出现的“术语”。 (Real-world graph is actually huge: about 20.000.000 documents and 25.000 terms). (真实世界的图形实际上是巨大的:大约20.000.000个文档和25.000个术语)。

I wonder how I go about counting co-occurrence pairs of terms in neo4j (in Cypher or Java). 我想知道如何计算neo4j中的共现术语对(在Cypher或Java中)。 The desired output from query should be: 查询所需的输出应为:

# Example: Pair (term-1, term-2) occurs in doc-1 and in doc-3
# Frequency for pair (term-1, term-2) should be 2
# termA | term B | frequency
term-1 | term-2 | 2
term-1 | term-3 | 1
term-2 | term-3 | 2

二分图

Graph is available at http://console.neo4j.org/r/7fmo7c 图表可在http://console.neo4j.org/r/7fmo7c上找到

Code to reproduce test graph in Neo4j 用于在Neo4j中重现测试图的代码

set name root
mkrel -t ROOT -c -v
cd 1
set name doc-1
set type document
mkrel -t HAVE -cv
cd 2
set name term-1
set type term
cd ..
mkrel -t HAVE -cv
cd 3
set name term-2
set type term
cd ..
mkrel -t HAVE -cv
cd 4
set name term-3
set type term
mkrel -t HAVE -d INCOMING -c
cd 5
set name doc-2
set type document
mkrel -t HAVE -d OUTGOING -n 3
cd 3
mkrel -t HAVE -d INCOMING -c
cd 6
set name doc-3
set type document
mkrel -t HAVE -d OUTGOING -n 2

Code to reproduce test graph in Java 用Java重现测试图的代码

import org.neo4j.graphdb.DynamicRelationshipType;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;

public class CountPairs {
    private static final String DB_PATH = "test.db";
    private static GraphDatabaseService graphDb;

    public static void main(String[] args) {
        graphDb = new GraphDatabaseFactory().
                newEmbeddedDatabaseBuilder(DB_PATH).
                setConfig(GraphDatabaseSettings.node_keys_indexable, "name, type").
                setConfig(GraphDatabaseSettings.node_auto_indexing, "true").
                newGraphDatabase();

        Transaction tx = graphDb.beginTx();
        Node doc1, doc2, doc3 = null;
        Node term1, term2, term3 = null;
        Relationship rel1, rel2, rel3, rel4, rel5, rel6, rel7 = null;
        try
        {
            // Create nodes
            doc1 = graphDb.createNode();
            doc2 = graphDb.createNode();
            doc3 = graphDb.createNode();
            term1 = graphDb.createNode();
            term2 = graphDb.createNode();
            term3 = graphDb.createNode();
            // Set properties
            doc1.setProperty("name", "doc1");
            doc1.setProperty("type", "document");
            doc2.setProperty("name", "doc2");
            doc2.setProperty("type", "document");
            doc3.setProperty("name", "doc3");
            doc3.setProperty("type", "document");
            // Set properties
            term1.setProperty("name", "term1");
            term1.setProperty("type", "term");
            term2.setProperty("name", "term2");
            term2.setProperty("type", "term");
            term3.setProperty("name", "term3");
            term3.setProperty("type", "term");
            // Create relations
            rel1 = doc1.createRelationshipTo(term1, DynamicRelationshipType.withName("HAVE"));
            rel2 = doc1.createRelationshipTo(term2, DynamicRelationshipType.withName("HAVE"));
            rel3 = doc1.createRelationshipTo(term3, DynamicRelationshipType.withName("HAVE"));
            rel4 = doc2.createRelationshipTo(term2, DynamicRelationshipType.withName("HAVE"));
            rel5 = doc2.createRelationshipTo(term3, DynamicRelationshipType.withName("HAVE"));
            rel6 = doc3.createRelationshipTo(term1, DynamicRelationshipType.withName("HAVE"));
            rel7 = doc3.createRelationshipTo(term2, DynamicRelationshipType.withName("HAVE"));

            tx.success();
        }
        catch(Exception e)
        {
            tx.failure();
        }
        finally
        {
            tx.finish();
        }

        graphDb.shutdown();
    }
}
start t1=node(*), t2=node(*) 
where has(t1.type) and has(t2.type) and t1.type='term' and t2.type='term' and id(t1) < id(t2)
with t1, t2 
match t1<-[:HAVE]-doc-[:HAVE]->t2 
where doc.type='document' 
return t1, t2, count(doc)

You can try this here : http://console.neo4j.org/r/pshvqx 你可以在这里试试: http//console.neo4j.org/r/pshvqx

I hope this is what you want. 我希望这就是你想要的。 Further, for better performance, I would suggest you to put index on nodes of type 'term' and use index in the start clause to get t1 and t2 此外,为了获得更好的性能,我建议你在“term”类型的节点上放置索引,并在start子句中使用index来获取t1t2

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

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