Skip to content

Instantly share code, notes, and snippets.

@pluradj
Last active February 14, 2017 17:57
Show Gist options
  • Save pluradj/9bf96f8eae9fa24f3d61 to your computer and use it in GitHub Desktop.
Save pluradj/9bf96f8eae9fa24f3d61 to your computer and use it in GitHub Desktop.
Titan 1.0 mixed indexes
// WARN com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx - Query requires iterating over all vertices
// http://s3.thinkaurelius.com/docs/titan/1.0.0/index-parameters.html
graph = TitanFactory.open('conf/titan-cassandra-es.properties')
g = graph.traversal()
// Elasticsearch has a 1s default refresh interval
n = 1500
// Full-text search
mgmt = graph.openManagement()
name = mgmt.makePropertyKey('name').dataType(String.class).cardinality(Cardinality.SINGLE).make()
nameIndex = mgmt.buildIndex('nameIndex', Vertex.class).addKey(name).buildMixedIndex('search')
mgmt.commit()
v = graph.addVertex()
v.property('name', 'Hercules')
graph.tx().commit()
// wait for a moment
Thread.sleep(n)
// equals is not a full-text search predicate and will get a warning
g.V().has('name', 'Hercules').values('name')
g.V().has('name', eq('Hercules')).values('name')
graph.query().has('name', 'Hercules').vertices().get(0).values('name')
// full-text search predicates will work fine without warning
// text predicates are defined in com.thinkaurelius.titan.core.attribute.Text
// import static com.thinkaurelius.titan.core.attribute.Text.*
g.V().has('name', textContains('hercules')).values('name')
g.V().has('name', textContainsPrefix('hercules')).values('name')
g.V().has('name', textContainsRegex('hercules')).values('name')
// String search
mgmt = graph.openManagement()
title = mgmt.makePropertyKey('title').dataType(String.class).cardinality(Cardinality.SINGLE).make()
titleIndex = mgmt.buildIndex('titleIndex', Vertex.class).addKey(title, Mapping.STRING.asParameter()).buildMixedIndex('search')
mgmt.commit()
v = graph.addVertex()
v.property('title', 'god')
graph.tx().commit()
// wait for a moment
Thread.sleep(n)
// equals is available for string search predicates so no warnings
g.V().has('title', 'god').values('title')
g.V().has('title', eq('god')).values('title')
graph.query().has('title', 'god').vertices().get(0).values('title')
// text predicates are defined in com.thinkaurelius.titan.core.attribute.Text
// import static com.thinkaurelius.titan.core.attribute.Text.*
g.V().has('title', textPrefix('god')).values('title')
g.V().has('title', textRegex('god')).values('title')
// Both full-text and string search
mgmt = graph.openManagement()
location = mgmt.makePropertyKey('location').dataType(String.class).cardinality(Cardinality.SINGLE).make()
locationIndex = mgmt.buildIndex('locationIndex', Vertex.class).addKey(location, Mapping.TEXTSTRING.asParameter()).buildMixedIndex('search')
mgmt.commit()
v = graph.addVertex()
v.property('location', 'sky')
graph.tx().commit()
// wait for a moment
Thread.sleep(n)
// indexed for both so no warnings
g.V().has('location', 'sky').values('location')
g.V().has('location', eq('sky')).values('location')
graph.query().has('location', 'sky').vertices().get(0).values('location')
// text predicates are defined in com.thinkaurelius.titan.core.attribute.Text
// import static com.thinkaurelius.titan.core.attribute.Text.*
g.V().has('location', textPrefix('sky')).values('location')
g.V().has('location', textRegex('sky')).values('location')
g.V().has('location', textContains('sky')).values('location')
g.V().has('location', textContainsPrefix('sky')).values('location')
g.V().has('location', textContainsRegex('sky')).values('location')
@pluradj
Copy link
Author

pluradj commented Mar 8, 2016

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