Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated IMDB example to use migrations. Updated som documentation. [#111
state:resolved] [#108 state:resolved]
- Loading branch information
andreas
committed
Mar 2, 2010
1 parent
3d3e6bb
commit e86e250
Showing
13 changed files
with
163 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
IMDB_FILE = 'data/test-actors.list' | ||
|
||
Neo4j.migration 1, "Create DB by parsing IMDB file" do | ||
up do | ||
puts "Migration 1, processing #{IMDB_FILE} file ..." | ||
Neo4j::Transaction.run do | ||
movies = {} | ||
current_actor = nil | ||
actors = 0 | ||
no_films = 0 | ||
|
||
File.open(IMDB_FILE).each_line do |line| | ||
next if line.strip.empty? | ||
|
||
tab_items = line.split("\t") | ||
|
||
unless tab_items.empty? | ||
if !tab_items[0].empty? | ||
current_actor = Actor.new | ||
current_actor.name = tab_items.shift.strip | ||
actors += 1 | ||
# puts "Parse new actor no. #{actors} '#{current_actor.name}'" | ||
end | ||
tab_items.shift | ||
|
||
film = tab_items.shift.strip | ||
|
||
# already created film ? | ||
movie = movies[film] | ||
if (movie.nil?) | ||
movie = Movie.new | ||
movie.title = film | ||
movie.year = /\((\d+)(\/.)?\)/.match(film)[1] | ||
movies[film] = movie | ||
# puts "Created new film #{film}" | ||
no_films += 1 | ||
end | ||
|
||
role = tab_items.shift | ||
roleNode = current_actor.acted_in.new(movie) | ||
|
||
unless (role.nil?) | ||
role.strip! | ||
# remove [] | ||
role.slice!(0) | ||
role.chop! | ||
title, character = role.split('-') | ||
roleNode.title = title.strip unless title.nil? | ||
roleNode.character = character.strip unless character.nil? | ||
end | ||
|
||
#puts "Actor: '#{current_actor}' Film '#{film}' Year '#{year}' Title '#{title}' Character '#{character}'" | ||
end | ||
end | ||
puts "created #{actors} actors and #{no_films} films" | ||
end | ||
end | ||
|
||
down do | ||
puts "deleting all movies and actors" | ||
Neo4j::Transaction.run do | ||
Actor.all.each {|a| a.del} | ||
Movie.all.each {|m| m.del} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Neo4j.migration 2, "Index DB" do | ||
up do | ||
|
||
puts "Migration 2, Index DB on #{Lucene::Config[:storage_path]}" | ||
|
||
Neo4j::Transaction.run do | ||
puts "Creating lucene index ..." | ||
Actor.index :name, :tokenized => true | ||
Actor.update_index | ||
end | ||
# only possible to access and query the index after the transaction commits | ||
end | ||
|
||
down do | ||
puts "removing lucene index" | ||
Actor.remove_index :name | ||
# Actor.update_index # maybe nicer way of deleting indexes - hmm, does it work ? | ||
require 'fileutils' | ||
FileUtils.rm_rf Lucene::Config[:storage_path] # quick and dirty way of killing the lucene index | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
Shows how to store and query a neo4j database | ||
|
||
|
||
1. Download the database type: ./install.sh | ||
2. Create the database: jruby create_neo_db.rb | ||
2. Run the application: jruby find_actors.rb willis | ||
|
||
The find_actors.rb uses migration which means that the first time the it is used it will take | ||
a while to process the imdb file and create the index. | ||
|
||
Notice that you can play around with migrations by require the 'find_actor' in JIRB and then for example | ||
Neo4j.migrate! 1 | ||
which will delete the lucene index (migration number 2) | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,54 @@ | ||
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/../../lib") | ||
#require "rubygems" | ||
require "rubygems" | ||
require "neo4j" | ||
|
||
# we have to configure these before the model is loaded | ||
Lucene::Config[:store_on_file] = true | ||
Lucene::Config[:storage_path] = "tmp/lucene" | ||
|
||
|
||
require "model" | ||
require "neo4j/extensions/reindexer" | ||
|
||
#Neo4j::Config[:storage_path] = DB_NEO_DIR | ||
Neo4j.start | ||
Neo4j::Transaction.run do | ||
Actor.index :name, :tokenized => true | ||
Actor.update_index | ||
end | ||
|
||
# have to let the previous transaction finish in order to lucene indexing | ||
# to take place | ||
# Keep lucene index on file system instead of in memory | ||
|
||
|
||
Neo4j::Transaction.run do | ||
puts "Find all actors named willis" | ||
result = Actor.find(:name => "willis") #, Bruce") | ||
# Load Migrations | ||
# Create Database | ||
require '1_create_neo_db' | ||
|
||
puts "Found #{result.size} actors" | ||
result.each {|x| puts x} | ||
# just for fun I have two migrations - first one for importing the database and second for indexing it. | ||
require '2_index_db' | ||
|
||
willis = result[0] | ||
puts "#{willis} acted in:" | ||
willis.rels.outgoing.each {|r| puts r.to_s } | ||
def find_actor(name) | ||
Neo4j::Transaction.run do | ||
puts "Find all actors named #{name}" | ||
result = Actor.find(:name => name) | ||
|
||
willis.acted_in.each { |movie| puts movie } | ||
puts "Found #{result.size} actors" | ||
result.each {|x| puts "#{x.neo_id}\t#{x}"} | ||
end | ||
end | ||
|
||
def find_movies(neo_id) | ||
Neo4j::Transaction.run do | ||
actor = Neo4j.load_node(neo_id) | ||
puts "No actor found with neo id #{neo_id}" if actor.nil? | ||
return if actor.nil? | ||
|
||
puts "#{actor} acted in:" | ||
actor.acted_in_rels.each {|r| puts "Movie #{r.end_node.title} title: #{r.title}"} | ||
end | ||
end | ||
|
||
Neo4j.start | ||
if (ARGV.size == 1) | ||
find_actor(ARGV[0]) | ||
elsif ARGV.size == 2 && ARGV[0] == "-m" | ||
find_movies(ARGV[1]) | ||
else | ||
puts "Usage: jruby find_actors.rb [-m] <actor name|actor neo_id>\n\n -m \tfinds the movies for the given actor neo_id" | ||
end | ||
|
||
Neo4j.stop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.