一、創建Neo4j的Legacy indexing
1.為節點創建索引
官方API的創建示例為:
將一節點添加至索引:
public static void AddNodeIndex(String nid)
{
String txUri=SERVER_ROOT_URI+"index/node/favorites";
WebResource resource = Client.create().resource(txUri);
String entity="{\"value\" : \"n204\",\"uri\" : \"http://192.168.209.128:7474/db/data/node/"+nid+"\",\"key\" : \"n201\"}";
ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON).entity(entity)
.post(ClientResponse.class);
System.out.println(response.getStatus());
System.out.println(response.getEntity(String.class));
response.close();
}
ps:nid是要添加索引的節點ID value是索引值 key是索引名稱
2.通過屬性查找節點
public static void GetNodeByIndex()
{
String txUri=SERVER_ROOT_URI+"index/node/favorites/n201/n201";
WebResource resource = Client.create().resource(txUri);
ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.get(ClientResponse.class);
System.out.println(response.getStatus());
System.out.println(response.getEntity(String.class));
response.close();
}
txUri路徑中:favorites為剛創建的索引名稱,第一個n201是節點索引key,第二個n201是節點索引值
二、自動創建索引(Legacy Automatic Indexes)
What default configuration means depends on how you have configured your database. If you haven’t
changed any indexing configuration, it means the indexes will be using a Lucene-based backend.
數據庫配置之後,就可以自動創建索引
1.配置文件配置
Auto-indexing must be enabled through configuration before we can create or configure them. Firstly
ensure that you’ve added some config like this into your server’s conf/neo4j.properties file:
打開conf/neo4j.properties文件如圖
配置下面的節點
# Enable auto-indexing for nodes, default is false.
node_auto_indexing=true
# The node property keys to be auto-indexed, if enabled.
node_keys_indexable=name,ki
# Enable auto-indexing for relationships, default is false.
relationship_auto_indexing=true
# The relationship property keys to be auto-indexed, if enabled.
relationship_keys_indexable=name,ki
Node_keys_indexable、relationship_keys_indexable對應節點、關系的屬性
配置完成之後重啟服務
重啟三個節點的集群
2。測試索引
插入一個節點和關系
// 創建節點
@Test
public void test2() {
URI uri = CreateSimpleGraph.createNode();
CreateSimpleGraph.addProperty(uri, "name", "張三");
URI uri1 = CreateSimpleGraph.createNode();
CreateSimpleGraph.addProperty(uri1, "name", "李四");
}
// 為節點設置關系
@Test
public void test6() {
for (int i = 1; i < 2; i++) {
try {
URI suri = new URI("http://192.168.209.128:7474/db/data/node/171391");
String uri1="http://192.168.209.128:7474/db/data/node/";
URI euri = new URI("http://192.168.209.128:7474/db/data/node/171392");
URI reluri= CreateSimpleGraph.addRelationship(suri, euri, "家人","{\"ki\" : \"1234567890\", \"name\" : \"無\" }");
System.out.println(reluri);
} catch (URISyntaxException e) {
// 異常信息輸出該內容
e.printStackTrace();
}
}
}
3.通過屬性查找節點
public static void GetNodeByAutoIndex(String ki)
{
// String txUri=SERVER_ROOT_URI+"index/node/node_auto_index/name/"+ki;
String txUri=SERVER_ROOT_URI+"index/auto/node/ki/"+ki;
WebResource resource = Client.create().resource(txUri);
ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.get(ClientResponse.class);
System.out.println(response.getStatus());
System.out.println(response.getEntity(String.class));
response.close();
}
public static void GetRelationshipByAutoIndex(String ki)
{
// String txUri=SERVER_ROOT_URI+"index/node/node_auto_index/name/"+ki;
String txUri=SERVER_ROOT_URI+"index/auto/relationship/ki/"+ki;
WebResource resource = Client.create().resource(txUri);
ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.get(ClientResponse.class);
System.out.println(response.getStatus());
System.out.println(response.getEntity(String.class));
response.close();
}
關系的輸出結果為:
[ {
"extensions" : { },
"metadata" : {
"id" : 337,
"type" : "家人"
},
"data" : {
"name" : "無",
"ki" : "1234567890"
},
"property" : "http://192.168.209.128:7474/db/data/relationship/337/properties/{key}",
"start" : "http://192.168.209.128:7474/db/data/node/171391",
"self" : "http://192.168.209.128:7474/db/data/relationship/337",
"end" : "http://192.168.209.128:7474/db/data/node/171392",
"type" : "家人",
"properties" : "http://192.168.209.128:7474/db/data/relationship/337/properties"
} ]
這裡說明一下,傳值為中文的時候,查詢不出來,可能需要編碼,因為工作暫時沒有用到,就沒有再研究了
更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2015-05/117734p2.htm
Neo4j生成測試數據 http://www.linuxidc.com/Linux/2012-05/61466.htm
Neo4j運行原理 http://www.linuxidc.com/Linux/2012-02/53689.htm
Neo4j High Availability 配置 http://www.linuxidc.com/Linux/2012-02/53688.htm
Neo4J圖數據庫實踐系列
圖數據庫實踐系列 (一)--Neo4J簡介與安裝 http://www.linuxidc.com/Linux/2013-08/88766.htm
圖數據庫實踐系列 (二)--Neo4J空間數據存儲 http://www.linuxidc.com/Linux/2013-08/88767.htm
圖數據庫實踐系列 (三)--Neo4j Spatial的REST集成 http://www.linuxidc.com/Linux/2013-08/88768.htm