Skip to content

Instantly share code, notes, and snippets.

@mneedham
Created June 10, 2012 12:41
Show Gist options
  • Save mneedham/2905358 to your computer and use it in GitHub Desktop.
Save mneedham/2905358 to your computer and use it in GitHub Desktop.
Loading stuff into neo via the batch API
# So the problem is inserting data into neo using the batch API. So we have a bunch of people and we want to put them into the graph and also
# add to to the index so that we can search for them.
# The way the batch API works is that you can refer to previous commands by referencing their index in the list of commands (zero indexed)
# e.g. if I want to reference the person added in the first command I would reference that node as {0}
# You should be able to see how that works in the code below.
neo_people_to_load = Set.new
neo_people_to_load << { :name => "Mark Needham", :id => 1 }
neo_people_to_load << { :name => "Jenn Smith", :id => 2 }
neo_people_to_load << { :name => "Chris Ford", :id => 3 }
command_index = 0
people_commands = neo_people_to_load.inject([]) do |acc, person|
acc << [:create_node, {:id => person[:id], :name => person[:name]}]
acc << [:add_node_to_index, "people", "name", person[:name], "{#{command_index}}"]
command_index += 2
acc
end
# So what we want to get is the following:
# [
# [:create_node, {:id=>"1", :name=>"Mark Needham"}], [:add_node_to_index, "people", "name", "Mark Needham", "{0}"],
# [:create_node, {:id=>"2", :name=>"Jenn Smith"}], [:add_node_to_index, "people", "name", "Jenn Smith", "{2}"],
# [:create_node, {:id=>"3", :name=>"Chris Ford"}, [:add_node_to_index, "people", "name", "Chris Ford", "{4}"]
# ]
@philandstuff
Copy link

Seems pretty functional already, except that you can derive command_index from the length of acc, and you don't strictly need to update acc in place (but it doesn't really matter here).

@ctford
Copy link

ctford commented Jun 10, 2012

Ruby's each_with_index method can also hand you the index of the item in the list e.g. something like:

people_commands = neo_people_to_load.each_with_index { |person index|
[[:create_node, {:id => person[:id], :name => person[:name]}]
[:add_node_to_index, "people", "name", person[:name], "{#{index * 2}}"]]
}.flatten(1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment