如 Titan 文档中所述,composite indexes 用于精确匹配。
一种选择是将属性存储两次,一次使用实际值,一次使用小写值。您可以通过以下几种方式做到这一点:
mgmt = graph.openManagement()
// store the property twice, once with the real value, once with a lowercased value
// for name, we're using different property names
name = mgmt.makePropertyKey('name').dataType(String.class).cardinality(Cardinality.SINGLE).make()
nameLower = mgmt.makePropertyKey('nameLower').dataType(String.class).cardinality(Cardinality.SINGLE).make()
nameLowerIndex = mgmt.buildIndex('nameLowerIndex', Vertex.class).addKey(nameLower).buildCompositeIndex()
// for title, we're using a list
title = mgmt.makePropertyKey('title').dataType(String.class).cardinality(Cardinality.LIST).make()
titleListIndex = mgmt.buildIndex('titleListIndex', Vertex.class).addKey(title).buildCompositeIndex()
mgmt.commit()
v = graph.addVertex()
h = 'HERCULES'
v.property('name', h)
v.property('nameLower', h.toLowerCase())
t = 'GOD'
v.property('title', t)
v.property('title', t.toLowerCase())
graph.tx().commit()
g.V(v).valueMap()
g.V().has('nameLower', 'hercules').values('name')
// within predicate is defined in org.apache.tinkerpop.gremlin.process.traversal.P
g.V().has('title', within('god')).values('title').next()
另一种选择是使用带有Mapping.TEXT 和文本谓词的混合索引,但要注意full-text search 所涉及的问题。
// 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)
// 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')