Skip to content

Instantly share code, notes, and snippets.

@espeed
Created January 4, 2012 04:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save espeed/1558540 to your computer and use it in GitHub Desktop.
Save espeed/1558540 to your computer and use it in GitHub Desktop.
Using Neo4j Fulltext Index with Gremlin
// Example using the Neo4j Fulltext Index with Gremlin-Groovy
// by James Thornton, http://jamesthornton.com
import com.tinkerpop.blueprints.pgm.impls.neo4j.util.Neo4jVertexSequence;
import com.tinkerpop.blueprints.pgm.impls.neo4j.util.Neo4jEdgeSequence;
Graph g = new Neo4jGraph('/tmp/neo4jmovies');
indexManager = g.getRawGraph().index();
indexConfig = ["provider":"lucene", "type":"fulltext"]
fulltextMovies = indexManager.forNodes( "movies-fulltext", indexConfig);
// Add some nodes and add them to the fulltext index
g.setMaxBufferSize(0);
g.startTransaction();
matrix = g.addVertex(['name':'The Matrix']);
reloaded = g.addVertex(['name':'The Matrix Reloaded']);
neo = g.addVertex(['name':'Neo']);
g.addEdge(matrix,neo,'character');
g.addEdge(reloaded,neo,'character');
fulltextMovies.add( matrix.getRawElement(), "title", "The Matrix" );
fulltextMovies.add( reloaded.getRawElement(), "title", "The Matrix Reloaded" );
g.stopTransaction(TransactionalGraph.Conclusion.SUCCESS);
// Return matched movie names
hits = fulltextMovies.query( "title", "matrix" )
vertices = new Neo4jVertexSequence(hits,g);
println vertices._().name.toList();
// Return movie character names
hits = fulltextMovies.query( "title", "matrix" )
vertices = new Neo4jVertexSequence(hits,g);
println vertices._().out('character').name.toList();
g.shutdown();
@espeed
Copy link
Author

espeed commented Jan 4, 2012

To run this, do:

$ ./gremlin.sh -e neo4j-fulltext-gremlin.groovy

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