背景
最近需要对公司 hgraphdb hbase 存储替换为neo4j,为了提高查询效率,需要对某些标签下 Node 的属性创建索引,官方提供的材料没有提供 create if not exist,所以需要手动去判断索引存在性,然后写入索引,方法抽取如下:
public static final Function3<String, String, Session, Boolean> INDEX_EXIST_FUNC = (label, property, session) -> {
boolean flag = false;
try {
flag = session.run(String.format(
"CALL db.indexes() YIELD properties,labelsOrTypes where '%s' in labelsOrTypes and '%s' in properties RETURN count(*) as count",
label, property)).list().get(0).get("count", 0) == 0;
} catch (Exception e) {
logger.error("check index exist failure=> label = {},property= {}", label, property);
}
return flag;
};
public static final Function3<String, String, Session, Result> CREATE_INDEX_FUN = (label, property, session) -> {
synchronized (Clazz.class) {
if (INDEX_EXIST_FUNC.apply(label, property, session)) {
session.run(String.format("CREATE INDEX ON :%s(%s)", label, property));
}
return null;
}
};
依赖包
<!-- https://mvnrepository.com/artifact/io.vavr/vavr -->
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.10.3</version>
</dependency>
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>4.1.0</version>
</dependency>
网友评论