繁体   English   中英

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

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

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

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

以上返回:

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的节点吗???

编辑 :在不同的事务中同样的错误。

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,因为它已经有一个了,就不应该再进行插入了。

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

您不知道的是,提交事务后是否在不更新的情况下缓存了迭代器

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

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

暂无
暂无

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

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