DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. neo4j: Creating a Custom Index With neo4j.rb

neo4j: Creating a Custom Index With neo4j.rb

Mark Needham user avatar by
Mark Needham
·
Aug. 15, 12 · Interview
Like (0)
Save
Tweet
Share
4.96K Views

Join the DZone community and get the full member experience.

Join For Free

As I mentioned in my last post, I’ve been playing around with the TFL Bus stop location and routes API. One thing I wanted to do was load all the bus stops into a neo4j database using the neo4j.rb gem.

I initially populated the database via neography, but it was taking around 20 minutes per run, and I figured it’d probably be much quicker to populate it directly rather than using the REST API.

Creating nodes is reasonably simple, and the code to add bus stops looks like this:

require 'neo4j'
 
Neo4j::Transaction.run do
  stops_to_add = [ {:name => "Walworth Road", :code => 10001 }]
 
  stops_to_add.each do |stop|
    node = Neo4j::Node.new(:name => stop[:name], :code => stop[:code], :type => "stop")
    puts "Code: #{stop[:code]}, Stop: #{stop[:name]}"
  end
end


I wanted to be able to search for bus stops using cypher, so I needed to create an index for each stop to allow me to do that easily.

I initially tried creating a Stop class and defining the index in there, as suggested in the documentation, but from what I could tell it created an index named after the string representation of the Stop object, making it difficult to use in cypher.

Eventually I came across another page that said I needed to create a ‘custom index’ if I wanted to be able to reference it by name.

I ended up with the following:

class StopsIndex
  extend Neo4j::Core::Index::ClassMethods
  include Neo4j::Core::Index
 
  self.node_indexer do
    index_names :exact => 'stops'
    trigger_on :type => "stop"
  end
 
  index :code
end


As far as I understand, this index gets triggered when you’re inside a transaction while adding a node of type ‘stop’, which is what I’m doing here.

With the index defined this way, it’s now possible to look up stops using cypher:

START stop = node:stops(code = "10001")
RETURN stop


And later, when I wanted to add a ‘route’ between stops in the code, I could look up the stops like so:

Neo4j::Transaction.run do
  stop1 = StopsIndex.find("code: \"10001\"").first
  stop2 = StopsIndex.find("code: \"10002\"").first				
  Neo4j::Relationship.new(:route, stop1, stop2, { :bus_number => 1 })
end

The full code for this is on github.

 

 

 

 

 

Neo4j

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top 5 PHP REST API Frameworks
  • Beginners’ Guide to Run a Linux Server Securely
  • Why Open Source Is Much More Than Just a Free Tier
  • Asynchronous HTTP Requests With RxJava

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: