2012-10-30 92 views
0

我们最近切换到JRuby on Rails,以便我们可以使用Neo4j,尽管我们只是抓住了Neo4j可以做的事情的表面,但它已经非常棒了。也就是说,我在索引和查找时遇到了一个相当棘手的问题。使用Neo4j和jRuby在Rails上索引旧数据

我只是说“:指数=>:精确”到我的用户模型的created_at领域,我Post模型的created_at字段(有已经是在我的岗位模型CUSTOM_DATE领域的指标):

class User < Neo4j::Rails::Model 
    property :created_at, :type => DateTime, :index => :exact 

class Post < Neo4j::Rails::Model 
    property :created_at, :type => DateTime, :index => :exact 
    property :custom_date, :type => DateTime, :index => :exact 

然后我走进轨控制台来尝试这个代码(这是我从the documentation改编):

User.find(:all, :created_at => 7.days.ago .. DateTime.now).count 

而且它返回0,这是我所知道的是并非如此,因为这样的代码:

new_users = [] 
User.all.each{ |user| new_users << user if user.created_at > 7.days.ago } 
new_users.count 

返回104

同样,当我运行

Post.find(:all, :created_at => 7.days.ago .. DateTime.now).count 

我得到0,但是当我运行

Post.find(:all, :custom_date => 7.days.ago .. DateTime.now).count 

我得到305(A后的CUSTOM_DATE初始设置为)唯一的区别是,当我们移植旧数据库时,我在Post模型的custom_date字段中设置了索引,而我只是将索引添加到了created_at字段。

这是我的问题:我如何根据他们的created_at时间搜索我的用户和帖子?

回答

0

问题是旧数据没有lucene索引。您只声明了一个索引,但未更新旧节点。您需要在所有旧节点上使用add_index

你应该做这样的事情:

Neo4j::Transaction.run do 
    User.all.each do |user| 
    # Since there is a type converter on created_at, we need to find the data 
    # as it is stored in neo4j. 
    val = User._converter(:created_at).to_java(user[:created_at]) 
    user.add_index(:created_at, val) 
    end 
end 

见RSpec的这个https://github.com/andreasronge/neo4j/commit/cffe7a4a2fdb83b71e781e717a2a8011e2ef494d

我还创建了一个github上的问题,因为我认为它应该是更容易做到这一点 - https://github.com/andreasronge/neo4j/issues/220

引入请求,欢迎:)

顺便说一下,在老版本0.4.6中的Neo4j有做支持这与迁移。

+0

工作完美!谢谢。我会刺探帮助并创建Model.reindex!方法,但我只是一名低级前端开发人员/设计师,担任后端工程师;你不会希望我在你的代码库的十英尺范围内。 – jwest