public
Created

Using Neo4j Fulltext Index with Gremlin

  • Download Gist
neo4j-fulltext-gremlin.groovy
Groovy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// 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();

To run this, do:

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.