Skip to content

Instantly share code, notes, and snippets.

@pluradj
Created March 7, 2016 17:50
Show Gist options
  • Save pluradj/ff1e1b1bcea0753adbb1 to your computer and use it in GitHub Desktop.
Save pluradj/ff1e1b1bcea0753adbb1 to your computer and use it in GitHub Desktop.
Titan 0.5.4 mixed indexes
// WARN com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx - Query requires iterating over all vertices
// http://s3.thinkaurelius.com/docs/titan/0.5.4/index-parameters.html
import com.thinkaurelius.titan.core.attribute.Text
graph = TitanFactory.open('conf/titan-cassandra-es.properties')
g = graph
// Elasticsearch has a 1s default refresh interval
n = 1500
// Full-text search
mgmt = graph.getManagementSystem()
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.setProperty('name', 'hercules')
graph.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').property('name')
g.V().has('name', Cmp.EQUAL, 'god').property('name')
g.query().has('name', 'hercules').vertices().get(0).property('name')
// full-text search predicates will work fine without warning
g.V().has('name', Text.CONTAINS, 'hercules').property('name')
g.V().has('name', Text.CONTAINS_PREFIX, 'hercules').property('name')
g.V().has('name', Text.CONTAINS_REGEX, 'hercules').property('name')
// String search
mgmt = graph.getManagementSystem()
title = mgmt.makePropertyKey('title').dataType(String.class).cardinality(Cardinality.SINGLE).make()
titleIndex = mgmt.buildIndex('titleIndex', Vertex.class).addKey(title, Mapping.STRING.getParameter()).buildMixedIndex('search')
mgmt.commit()
v = graph.addVertex()
v.setProperty('title', 'god')
graph.commit()
// wait for a moment
Thread.sleep(n)
// equals is available for string search predicates so no warnings
g.V().has('title', 'god').property('title')
g.V().has('title', Cmp.EQUAL, 'god').property('title')
g.query().has('title', 'god').vertices().get(0).property('title')
g.V().has('title', Text.PREFIX, 'god').property('title')
g.V().has('title', Text.REGEX, 'god').property('title')
// Both full-text and string search
mgmt = graph.getManagementSystem()
location = mgmt.makePropertyKey('location').dataType(String.class).cardinality(Cardinality.SINGLE).make()
locationIndex = mgmt.buildIndex('locationIndex', Vertex.class).addKey(location, Mapping.TEXTSTRING.getParameter()).buildMixedIndex('search')
mgmt.commit()
v = graph.addVertex()
v.setProperty('location', 'sky')
graph.commit()
// wait for a moment
Thread.sleep(n)
// indexed for both so no warnings
g.V().has('location', 'sky').property('location')
g.V().has('location', Cmp.EQUAL, 'sky').property('location')
g.query().has('location', 'sky').vertices().get(0).property('location')
g.V().has('location', Text.PREFIX, 'sky').property('location')
g.V().has('location', Text.REGEX, 'sky').property('location')
g.V().has('location', Text.CONTAINS, 'sky').property('location')
g.V().has('location', Text.CONTAINS_PREFIX, 'sky').property('location')
g.V().has('location', Text.CONTAINS_REGEX, 'sky').property('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