简体   繁体   English

Neo4j graph.index()。forNodes找不到节点

[英]Neo4j graph.index().forNodes not finding nodes

I'm trying to create unique nodes of type TEST with a unique property of "id". 我正在尝试创建具有唯一属性“ id”的TEST类型的唯一节点。

However the forNodes() method is not detecting duplicates, is there any better method using Java API only and why does the below not work? 但是,forNodes()方法未检测到重复项,是否有仅使用Java API的更好的方法,为什么下面的方法不起作用?

public class Neo4jTest
{
    public static void main(String args[])
    {
        GraphDatabaseService graph = new TestGraphDatabaseFactory().newImpermanentDatabase();

        Label testLabel = new Label()
        {
            @Override
            public String name()
            {
                return "TEST";
            }
        };

        try (Transaction tx = graph.beginTx())
        {
            graph.schema()
                    .constraintFor(testLabel)
                    .assertPropertyIsUnique("id")
                    .create();
            tx.success();
        }

        try (Transaction tx = graph.beginTx())
        {
            int k = 99;
            for (int i = 0; i < 4; i++)
            {
                System.out.println("indexing... i="+i);
                Index<Node> testIndex = graph.index().forNodes(testLabel.name());
                IndexHits<Node> testIterator = testIndex.get("id", k);

                if (!testIterator.hasNext())
                {
                    System.out.println("creating node... i="+i);
                    Node testNode = graph.createNode(testLabel);
                    testNode.setProperty("id", k);
                    tx.success();
                }
            }
        }
    }
}

The above returns: 以上返回:

indexing... i=0
creating node... i=0
indexing... i=1
creating node... i=1
Exception in thread "main" org.neo4j.graphdb.ConstraintViolationException: Node 0 already exists with label TEST and property "id"=[99]

shouldn't the above detect when i=1 that there's already a node with id = 99??? 当i = 1时,上面的命令不应该检测到已经有一个ID = 99的节点吗???

EDIT : same error also in different transactions.. 编辑 :在不同的事务中同样的错误。

public class Neo4jTest
{

    public static void main(String args[])
    {
        GraphDatabaseService graph = new TestGraphDatabaseFactory().newImpermanentDatabase();

        Label testLabel = new Label()
        {
            @Override
            public String name()
            {
                return "TEST";
            }
        };

        try (Transaction tx = graph.beginTx())
        {
            graph.schema()
                    .constraintFor(testLabel)
                    .assertPropertyIsUnique("id")
                    .create();
            tx.success();
        }

        int k = 99;

        try (Transaction tx = graph.beginTx())
        {
            System.out.println("indexing... i=" + 0);
            Index<Node> testIndex = graph.index().forNodes(testLabel.name());
            IndexHits<Node> testIterator = testIndex.get("id", k);

            if (!testIterator.hasNext())
            {
                System.out.println("creating node... i=" + 0);
                Node testNode = graph.createNode(testLabel);
                testNode.setProperty("id", k);

            }
            tx.success();
        }

        try (Transaction tx = graph.beginTx())
        {
            System.out.println("indexing... i=" + 1);
            Index<Node> testIndex = graph.index().forNodes(testLabel.name());
            IndexHits<Node> testIterator = testIndex.get("id", k);

            if (!testIterator.hasNext())
            {
                System.out.println("creating node... i=" + 1);
                Node testNode = graph.createNode(testLabel);
                testNode.setProperty("id", k);
            }
            tx.success();
        }
    }
}

The real problem that you are finding is that testIterator is returning false when it should return true. 您发现的真正问题是testIterator在应返回true时返回false。 When i == 1 the iterator should have returned !true because it already has one no further insertion should have happened. 当i == 1时,迭代器应该返回!true,因为它已经有一个了,就不应该再进行插入了。

But it is not working as designed then it proceeds to insert and you get an exception as it should be. 但是它不能按设计工作,然后继续插入,您应该得到应有的异常。 The graph is recognizing the uniqueness violation. 该图正在识别唯一性违规。

What you don't know is if the iterator is being cached without updating after the transaction is committed 您不知道的是,提交事务后是否在不更新的情况下缓存了迭代器

if (!testIterator.hasNext())
{
...
}

Notice that going from 0 to 1 nodes does NOT involve any uniqueness. 请注意,从0到1的节点不涉及任何唯一性。 This is where the iterator is failing: not being updated 这是迭代器失败的地方:未更新

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

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