如何重建Neo4j Lucene索引? - Neo4j.rb

时间:2011-09-16 04:38:35

标签: ruby-on-rails lucene jruby ruby-on-rails-3.1 neo4j

我在Rails 3.1上使用Neo4j.rb gem(1.2.2)在Neo4j(1.4)上运行

我遇到了neo4j索引被破坏的问题,我不能再运行数据库,正如在this等几个论坛中提到的那样我删除了db / index目录并且它有效。但是我需要再次重建索引。

我在文档中找不到关于如何重建索引的任何内容,有人可以帮忙吗?

非常感谢!

3 个答案:

答案 0 :(得分:1)

您应该进入数据库目录并删除

  • 名为index的目录
  • 文件index.db

稍后遍历节点和边的孔集,更新每个节点的属性。

/ purbon

答案 1 :(得分:1)

我的问题很相似 - 升级到neo4j 1.5(从1.4开始)后,我的索引被破坏了。 我的情况: 我有两个索引:

  • __types__:用于索引持久对象的类型(由spring-data-neo4j 2.0.0.RC1提供)
  • User:用于索引用户名字段,以便我可以在它们之后进行查找

这导致了一个主要问题,我可以通过他们的id找到所有节点,但是在用户名之后无法进行查找,或者列出某种类型的所有对象。

修复(我将提供java代码,但其他语言的想法也是一样):

/* begin a transaction */
Transaction tx = graphDatabaseService.beginTx(); 
/* for all nodes in the database */
for (Node node : graphDatabaseService.getAllNodes()) { 
    /* reconstruct the saved object based on the __type__ property on the node - the result is a class that was annotated with @NodeEntity */
    DefaultDbNode ddn = neo4jTemplate.createEntityFromStoredType(node,
            null);
    /* reindex this node, adding it to the __types__ index, with key "className" (it is used by spring-data-neo4j) with the value __type__ */
    graphDatabaseService.index().forNodes("__types__")
            .add(node, "className", node.getProperty("__type__")); 
    /* if the reconstructed object is a User object */
    if (ddn instanceof User) { 
        /* add it to the User index, with the key "username" (which is also the saved fields name) */
        graphDatabaseService.index().forNodes("User")
                .add(node, "username", node.getProperty("username")); 
    }
}
/* end transaction */
tx.success();
tx.finish();

希望这可以帮助你或某人!

答案 2 :(得分:1)

感谢所有试图提供帮助的人。在我的情况下,我通过以下步骤成功解决了问题:

第1步按照Neo4j的Michael Hunger的推荐(通过邮件列表),我使用了一个名为checkindex的工具来删除损坏的索引条目Lucene and Solr's Checkindex

第2步删除损坏的索引条目后,剩下的问题是构建它们,以便Lucene可以再次开始查询它们。这可以简单地使用Model.addindex(:index_name)来完成。请注意,此操作需要包含在Neo4j :: Transaction中。在我的情况下,我在railsconsole上运行它,但我想你也可以在rails应用程序中对它们进行编码。

示例:

Neo4j::Transaction.run do
  User.all.each do |user|
    user.add_index(:first_name)
    user.add_index(:email)
    user.save
  end
end

希望这可以帮助那些面临同样问题的人。

干杯