Neo4j graph.index().forNodes找不到节点 [英] Neo4j graph.index().forNodes not finding nodes

查看:98
本文介绍了Neo4j graph.index().forNodes找不到节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建具有唯一属性"id"的TEST类型的唯一节点.

I'm trying to create unique nodes of type TEST with a unique property of "id".

但是forNodes()方法未检测到重复项,是否有仅使用Java API的更好的方法?为什么以下方法不起作用?

However the forNodes() method is not detecting duplicates, is there any better method using Java API only and why does the below not work?

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();
                }
            }
        }
    }
}

以上返回:

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]

当i = 1时,上面的检测方法不能检测到已经有一个ID = 99的节点吗?

shouldn't the above detect when i=1 that there's already a node with 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();
        }
    }
}

推荐答案

您发现的真正问题是testIterator在应返回true时返回false.当i == 1时,迭代器应该返回!true,因为它已经有一个了,就不应该再插入了.

The real problem that you are finding is that testIterator is returning false when it should return true. When i == 1 the iterator should have returned !true because it already has one no further insertion should have happened.

但是它不能按设计工作,然后继续插入,您将得到应有的异常.该图正在识别唯一性违规.

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())
{
...
}

请注意,从0到1的节点不涉及任何唯一性.这是迭代器失败的地方:未更新

Notice that going from 0 to 1 nodes does NOT involve any uniqueness. This is where the iterator is failing: not being updated

这篇关于Neo4j graph.index().forNodes找不到节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆