问题
I have created a composite index on a property in titan 1.0. Now I want to do a case insensitive search on that property.
Composite index being created as below :
TitanManagement mgmt = graph.openManagement();
TitanManagement.IndexBuilder nameIndexBuilder = mgmt.buildIndex("name_comp_idx", Vertex.class).addKey("name");
titanGraphIndex = nameIndexBuilder.buildCompositeIndex();
Vertex :
TitanVertex vertex= graph.addVertex(T.label, "company");
entity.property("name", "APPLE");
Below query is being used to search the titan graph with Tinkerpop API 3.
graph.traversal().V().has("name", "apple").toList()
But no results are returned..
Can anyone please tell how to make a case insensitive search on titan composite index? Is there any other way to achieve the same?
回答1:
As described in the Titan documentation, composite indexes are used for exact matches.
One option would be to store the property twice, once with the real value and once with a lowercased value. Here are a couple ways you could do that:
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()
Another option would be to use a mixed index with Mapping.TEXT
and a text predicate, but be aware of the gotchas involved with 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')
来源:https://stackoverflow.com/questions/35859231/how-to-do-a-case-insensitive-search-in-titan-1-0-on-composite-index-with-tinkerp