2016-03-08 35 views
1

我已經在titan 1.0的屬性上創建了一個組合索引。現在我想對該屬性執行不區分大小寫的搜索。如何在titan 1.0中使用tinkerpop API在組合索引中執行不區分大小寫的搜索3

綜合指數正在創建如下:

TitanManagement mgmt = graph.openManagement(); 

TitanManagement.IndexBuilder nameIndexBuilder = mgmt.buildIndex("name_comp_idx", Vertex.class).addKey("name"); 

titanGraphIndex = nameIndexBuilder.buildCompositeIndex(); 

頂點:

TitanVertex vertex= graph.addVertex(T.label, "company"); 
     entity.property("name", "APPLE"); 

下面查詢正被用於搜索與TinkerPop有關API的鈦圖表3.

graph.traversal().V().has("name", "apple").toList() 

但沒有結果返回。

任何人都可以請告訴如何在泰坦複合索引上進行不區分大小寫的搜索?有沒有其他辦法可以達到同樣的效果?

回答

2

如泰坦文檔中所述,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')