簡體   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