2013-06-03 47 views
0

我對neo4j非常陌生。我讀過這個問題(Cypher Query not finding Node),但它不起作用。我收到錯誤,找不到auto_node_index。也許是因爲我在使用BatchInserter?使用索引進行批插入後沒有結果

對於我的實驗,我使用neo4j 1.8.2和編程語言Java與嵌入式數據庫。

我想使用BatchInserter和BatchInserterIndex將一些數據放入數據庫,如http://docs.neo4j.org/chunked/milestone/batchinsert.html中所述。

BatchInserter myInserter = BatchInserters.inserter(DB_PATH); 
    BatchInserterIndexProvider indexProvider = 
      new LuceneBatchInserterIndexProvider(myInserter); 
    BatchInserterIndex persons = 
      indexProvider.nodeIndex("persons", MapUtil.stringMap("type", "exact")); 
    persons.setCacheCapacity("name", 10000); 

首先,我讀了一個TGF-文件中的數據,創建節點,並把它放到插入這樣的:

properties = MapUtil.map("name", actualNodeName, "birthday", birthdayValue); 
    long node = myInserter.createNode(properties); 
nodes.add(node); 
persons.flush(); 

插入工作正常,但是當我要搜索一個節點與Cypher支架,結果是空

ExecutionEngine engine = new ExecutionEngine(db); 
    String query = 
     "start n=node:persons(name='nameToSearch') " 
     + " match n-[:KNOWS]->m " 
     + " return n.id, m "; 
    ExecutionResult result = engine.execute(query); 
    System.out.println(result); 

在另一邊,當我使用橫移類,並開始對根節點的搜索,我收到節點至極通過與名稱的節點被connetced「 nameTo搜索」。

有人可以解釋我,爲什麼我不能用Cypher獲得節點!

這裏是批量插入完整的方法:

public long batchImport() throws IOException{ 

    String actualLine; 
    ArrayList<Long> nodes = new ArrayList<Long>(); 
    Map<String,Object> properties = new HashMap<String,Object>(); 

    //delete all nodes and edges in the database 
    FileUtils.deleteRecursively(new File(DB_PATH)); 

    BatchInserter myInserter = BatchInserters.inserter(DB_PATH); 
    BatchInserterIndexProvider indexProvider = 
      new LuceneBatchInserterIndexProvider(myInserter); 
    BatchInserterIndex persons = 
      indexProvider.nodeIndex("persons", MapUtil.stringMap("type", "exact")); 
    persons.setCacheCapacity("name", 10000); 

    long execTime = 0; 
    try{ 
     //Get the file which contains the graph informations 
     FileReader inputFile = new FileReader(UtilFunctions.searchFile(new File(PATH_OUTPUT_MERGED_FILES), "nodesAndEdges").get(0)); 
     LineNumberReader inputLine = new LineNumberReader(inputFile); 

     // Read nodes up to symbol # 
     execTime = System.nanoTime(); 
     while ((actualLine=inputLine.readLine()).charAt(0) != '#'){ 

     StringTokenizer myTokenizer = new StringTokenizer(actualLine); 
     // Read node number 
     String actualNodeNumber = myTokenizer.nextToken(); 
     // Read node name 
     String actualNodeName = myTokenizer.nextToken() + " " + myTokenizer.nextToken(); 
     //Read property    
     myTokenizer.nextToken(); 
     String actualNodePropertyKey = BIRTHDAY_KEY; 
     String actualNodePropertyValue = myTokenizer.nextToken(); 
     actualNodePropertyValue = actualNodePropertyValue.substring(1, actualNodePropertyValue.length()-1); 

     // Insert node information       
     properties = MapUtil.map("name", actualNodeName, "birthday", actualNodePropertyValue, "id", actualNodeNumber); 
     long node = myInserter.createNode(properties); 
     nodes.add(node); 
     persons.flush(); 
    } 

    // Read edges up to end of file 
    int countEdges = 0; 
    while ((actualLine=inputLine.readLine()) != null){ 
     StringTokenizer myTokenizer = new StringTokenizer(actualLine); 
     // Read start node number 
     String actualStartNodeNumber = myTokenizer.nextToken(); 
     // Read destination node number 
     String actualDestinationNodeNumber = myTokenizer.nextToken(); 
     // Read relationship type 
     String actualRelType = myTokenizer.nextToken(); 

     // Insert node information into ArrayList 
     int positionStartNode = Integer.parseInt(actualStartNodeNumber); 
     int positionDestinationNode = Integer.parseInt(actualDestinationNodeNumber); 

     properties.clear(); 

     if (countEdges == 0) { 
      myInserter.createRelationship(0, nodes.get(positionStartNode-1), RelType.ROOT, properties); 
      myInserter.createRelationship(nodes.get(positionStartNode-1), nodes.get(positionDestinationNode-1), RelType.KNOWS, properties); 
     } 
     else 
     { 
      myInserter.(nodes.get(positionStartNode-1), nodes.get(positionDestinationNode-1), RelType.KNOWS, properties); 
     } 
     countEdges++; 
    }     
    indexProvider.shutdown(); 
    myInserter.shutdown(); 
    execTime = System.nanoTime() - execTime; 
    // Close input file 
    inputLine.close(); 
    inputFile.close(); 

    } 
    catch (Throwable e){ 
     System.out.println(e.getMessage()); 
     e.printStackTrace(); 
    } 
    return execTime; 
}  
+0

在第二個代碼示例中,您有'nodes.add(node)',其中'nodes'定義在哪裏? – Nicholas

+0

抱歉,這不是完整的代碼片段。 'nodes'在'myInserter'之前用這種方式定義:'ArrayList nodes = new ArrayList ();' –

+0

你應該首先從「start n = node(*)return n」開始,看看是否有任何東西數據庫!這樣你就會知道它是一個導入還是查詢問題... – bendaizer

回答

0

您缺少呼叫profiles.add(node, <indexProperties>)。因此,你永遠不會向索引添加任何東西。

+0

Thx!我失明瞭作爲一隻蝙蝠,並沒有看到它!:(我忘了聲明'persons.add(節點,屬性)' –

0

至關重要的是,使用Batchinserter API調用的代碼shutdown()上都BatchInserterIndexProviderBatchInserter。也許你已經錯過了你的代碼。

如果這不能解決問題,請發佈您的代碼。

+0

不幸的是,這不是我的錯。我已經改變了我的帖子,並把完整的方法在帖子的末尾 –

相關問題