节点和关系都可以设置自己的属性。 属性是由Key-Value键值对组成,键名是字符串。属性值是要么是原始值,要么是原始值类型的一个数组。比如+String+,+int+和i+int[]+都是合法的。
注意
null不是一个合法的属性值。 Nulls能代替模仿一个不存在的Key。
…/_images/image3.9.png
属性值类型
Type Description Value range
boolean true/false
byte 8-bit integer -128 to 127, inclusive
short 16-bit integer -32768 to 32767, inclusive
int 32-bit integer -2147483648 to 2147483647, inclusive
long 64-bit integer -9223372036854775808 to 9223372036854775807, inclusive
float 32-bit IEEE 754 floating-point number
double 64-bit IEEE 754 floating-point number
char 16-bit unsigned integers representing Unicode characters u0000 to uffff (0 to 65535)
String sequence of Unicode characters
如果要了解float/double类型的更多细节,请参考:Java Language Specification。
本人Cypher语法 同样小白 仅分享(有不正确的地方请包涵)
neo4j 中文网
创建一个标签为zhan的节点 (标签:将数据进行分类,方便管理)
create(n:zhan);// 这里的n就是随便起的一个变量名,代指当前实体或者关系。
创建一个标签为zhan 且带有属性值的节点
create(n:zhan{id:1,name:'小明',age:20});
按照标签查询
match (n:zhan) return n;
按照id进行查询
match (n:zhan) where id(n) = 38023 return n;
多属性条件查询
match (n:zhan) where n.name = '小明'and n.age = 20 and n.id = 1 return n;
添加多标签
match (n:zhan) where id(n) = 38023 set n:zhan1 return n;
match (n:zhan) where id(n) = 38023 set n:zhan2 return n;
移除标签
match (n:zhan) where id(n) = 38023 remove n:zhan2 return n;
match (n:zhan) where id(n) = 38023 remove n:zhan1 return n;
修改属性
match (n:zhan) where n.name='小明' set n.name = '小红' return n;
match (n:zhan) where n.name='小明' set n.name = '小红' set n.age = '200' return n;
删除实体
match (n:zhan) where n.name = '小王' delete n;
相信 已发现 规律 * —— *
新建关系和实体
1. 无实体
create (n:zhan{name:'小张'})-[r:test] -> (m:zhan{name:'小李'})
create (n:zhan{name:'小黄'})-[r:test{name:'夫妻'}] -> (m:zhan{name:'小八'})
2.有实体
match (n:zhan),(m:zhan) where n.name = '小李' and m.name = '小八'
create (n)-[r:test{name:'儿子'}]->(m) ;
修改属性
match p = (n)-[r:test]->(m) where r.name='朋友' set r.name='闺蜜' return p;
关系添加属性
match p = (n)-[r:test]->(m) where r.name='闺蜜' set r.color='红色' return p;
删除关系
match (n)-[r:relation]->(m) where r.name = '闺蜜' delete r;
修改标签
【注意】neo4j是不支持修改关系标签
修改标签的操作是重新创建一个关系删除原关系
MATCH (n)-[r:test]->(m) where r.name='闺蜜' #搜出来想要修改的关系
CREATE (n)-[r2:relation]->(m) # 新建关系
SET r2 = r # copy原属性
DELETE r # 删除
高级搜索 …
【注意】:Spring boot 版本 2.2 以上
<dependency>
<groupId>org.neo4jgroupId>
<artifactId>neo4j-ogm-http-driverartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-neo4jartifactId>
dependency>
neo4j:
uri: bolt://******:***
username: neo4j
password: ''
@RelationshipEntity //表示关系类型
@Data //get set
@AllArgsConstructor//会生成一个包含所有变量的构造方法,默认生成的方法是 public 的
@NoArgsConstructor // 生成一个无参数的构造方法
public class KnowledgePointsRelation {
@Id
@GeneratedValue
private Long id;
private Date createTime;
private String name;
/**
* 起始节点的实体
*/
@StartNode
private KnowledgePointsNose KnowledgePointsFrom;
/**
* 终止节点的实体
*/
@EndNode
private KnowledgePointsNose KnowledgePointsTo;
public KnowledgePointsRelation(KnowledgePointsNose KnowledgePointsFrom,KnowledgePointsNose KnowledgePointsTo,String name){
this.KnowledgePointsFrom=KnowledgePointsFrom;
this.KnowledgePointsTo=KnowledgePointsTo;
this.name=name;
}
}
public interface KnowledgePointsNoseRepository extends Neo4jRepository<KnowledgePointsNose,Long> {
}
public interface KnowledgePointsRelationRepository extends Neo4jRepository<KnowledgePointsRelation,Long> {
}
同样:
neo4j 结合现有方法
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.springframework.data.repository;
import java.util.Optional;
@NoRepositoryBean
public interface CrudRepository<T, ID> extends Repository<T, ID> {
<S extends T> S save(S var1);
<S extends T> Iterable<S> saveAll(Iterable<S> var1);
Optional<T> findById(ID var1);
boolean existsById(ID var1);
Iterable<T> findAll();
Iterable<T> findAllById(Iterable<ID> var1);
long count();
void deleteById(ID var1);
void delete(T var1);
void deleteAll(Iterable<? extends T> var1);
void deleteAll();
}
CustomerNode customerNode=new CustomerNode();
customerNode.setName(name);
customerNode.setAge(age);
customerRepository.save(customerNode);
CustomerNode customerNodeTo=customerRepository.findByName(nameTo);
if(customerNodeTo !=null){
CustomerRelation customerRelation=new CustomerRelation(customerNode,customerNodeTo,remark);
customerRelationRepository.save(customerRelation);
}