Neo4j3.5学习笔记——Using indexs

跟着官网代码学习.jpg–jdk 1.8.0 & neo4j 3.5
https://neo4j.com/docs/java-reference/current/java-embedded/

目标:建立一个用户数据库,通过名称查找到用户
1. Neo4j中索引的使用方法
1)neo4j可以对noderelationship的属性建立索引,索引中的node(relationship)和属性对key-value为多对多的关系。一个node(relationship)可以在某索引中存储多个属性对,一个属性对也可以对应到多个node(relationship)。
2)<>:表示域

private static Index<Node> nodeIndex;

----代码表示建立一个private static的在Node域上的Index,名为nodeIndex。
3)一个Relationship在Neo4j上显示的关联:因此Node,setproperty()函数就Node.setproperty(属性,属性的内容)

 {
  "start": {
"identity": 0,
"labels": [],
"properties": {
"message": "Hello, "
    }
  },
  "end": {
"identity": 1,
"labels": [],
"properties": {
"message": "World!"
    }
  },

4)nodeIndex用法: 在neo4j中,索引可以分为两类:neo4j本身既是关于relationship的索引实现+基于独立索引引擎,如Apache Lucene的索引机制。通常情况下,我们所说的索引指第二种情况。按照索引的对象可以将索引分为两类:基于node的索引和基于relationship的索引。

nodeIndex = graphDb.index().forNodes( "nodes" );
nodeIndex.add(node1,"name",node1.getProperty("name"));
nodeIndex.add(node1,"name","haha");
nodeIndex.add(node2,"name",node2.getProperty("name"));
nodeIndex.add(node2,"name","haha");

5)更新索引时,需要手工删除对应的更新项,然后在添加更新后的项。

// create a node with a property
// so we have something to update later on
Node fishburn = graphDb.createNode();
fishburn.setProperty( "name", "Fishburn" );
// index it
nodeIndex.add( fishburn, "name", fishburn.getProperty( "name" ) );
// update the index entry
// when the property value changes
nodeIndex.remove( fishburn, "name", fishburn.getProperty( "name" ) );
fishburn.setProperty( "name", "Laurence Fishburn" );
nodeIndex.add( fishburn, "name", fishburn.getProperty( "name" ) );

6)索引的查询可以使用get方法,也可以使用query方法,相对与get,query方法功能更强大一些,get方法进行一些精确的key-value匹配。

//Get方法
Node foundUser = nodeIndex.get( USERNAME_KEY, userName ).getSingle();
//Query方法
for ( Node movie : movies.query( "title:*Matrix* AND year:1999" ) ) {	
	// This will return "The Matrix" from 1999 only.
}

4. [补充] 使用静态方法的好处:
1)static方法是类中的一个成员方法,不需要引用变量即可直接调用;
2)静态方法效率上比实例更加高,但静态方法的缺点是不能直接销毁,但实例可以;
3)静态方法和静态变量创建后只使用同一块内存,而实例的会创建多个内存;
4)静态方法为类所有,可以通过对象来使用,也可以通过类来使用。但一般提倡通过类名来使用,因为静态方法只要定义了类,不必建立类的实例就可使用。静态方法只能用类的静态成员。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值