使用索引进行批量插入后,Cypher没有结果

时间:2013-06-03 16:59:06

标签: neo4j cypher

我对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-file中读取数据,创建节点并将其放入插入器中,如下所示:

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

另一方面,当我使用Traverser-class并在rootnode上开始搜索时,我会收到名称为“nameToSearch”的节点所连接的节点。

任何人都可以解释一下,为什么我不能用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; 
 }      

2 个答案:

答案 0 :(得分:0)

使用Batchinserter API的代码在shutdown() BatchInserterIndexProvider上调用BatchInserter至关重要。也许你在代码中错过了这个。

如果这不能解决问题,请发布您的代码。

答案 1 :(得分:0)

您没有致电profiles.add(node, <indexProperties>)。因此,您永远不会向索引添加任何内容。