目录
HugeGraph 索引介绍
二级索引
组合索引
范围索引
全文索引
HugeGraph 索引介绍
二级索引
创建schema和添加数据
schema.propertyKey("name").asText().ifNotExist().create();
schema.propertyKey("uid").asLong().ifNotExist().create();
schema.propertyKey("city").asText().ifNotExist().create();
schema.propertyKey("mid").asText().ifNotExist().create();
schema.propertyKey("url").asText().ifNotExist().create();
schema.propertyKey("date").asText().ifNotExist().create();
schema.vertexLabel("article")
.properties("name", "uid", "city","mid","url")
.useCustomizeStringId()
.ifNotExist()
.create();
schema.indexLabel("articleByNameSecondary")
.onV("article")
.by("name")
.secondary()
.ifNotExist()
.create();
graph.addVertex(T.label, "article",T.id,"id7", "mid", "lakjdflkjalskdjfl", "name", "北京三联WiFi熟练度附近", "uid", 1234567897,"city","未知","url","http://test.com//IodUhh2bV");
graph.addVertex(T.label, "article",T.id,"id2", "mid", "lakjdflkjalskdjfl", "name", "北京三联法律文件而来反馈", "uid", 1234567891,"city","未知","url","http://test.com//IodUhh2bV");
graph.addVertex(T.label, "article",T.id,"id3", "mid", "lakjdflkjalskdjfl", "name", "深圳广州WiFi熟练度附近", "uid", 1234567892,"city","未知","url","http://test.com//IodUhh2bV");
graph.addVertex(T.label, "article",T.id,"id4", "mid", "lakjdflkjalskdjfl", "name", "深圳WiFi熟练度附近", "uid", 1234567893,"city","未知","url","http://test.com//IodUhh2bV");
graph.addVertex(T.label, "article",T.id,"id5", "mid", "lakjdflkjalskdjfl", "name", "广州测试", "uid", 1234567894,"city","未知","url","http://test.com//IodUhh2bV");
graph.addVertex(T.label, "article",T.id,"id6", "mid", "lakjdflkjalskdjfl", "name", "数据", "uid", 1234567895,"city","未知","url","http://test.com//IodUhh2bV");
根据name查询,该方法只能全不值匹配,不能模糊匹配
模糊匹配使用该方法,不推荐这么使用,filter过滤数据,当数据量很大的时候性能会下降
g.V().hasLabel("article").filter{it.get().property("name").value().contains("北京")};
或者使用下面这个方法进行正则匹配
g.V().hasLabel("article").filter{it.get().property("name").value().matches("北京(.*)")};
可以对结果进行过滤
组合索引
待补充
范围索引
待补充
全文索引
创建索引标签
设置定点article的name字段为全文索引(可以模糊检索)
schema.indexLabel("articleByNameSearch")
.onV("article")
.by("name")
.search()
.ifNotExist()
.create();
其他schema设置
schema.propertyKey("name").asText().ifNotExist().create();
schema.propertyKey("uid").asLong().ifNotExist().create();
schema.propertyKey("city").asText().ifNotExist().create();
schema.propertyKey("mid").asText().ifNotExist().create();
schema.propertyKey("url").asText().ifNotExist().create();
schema.propertyKey("date").asText().ifNotExist().create();
schema.vertexLabel("article")
.properties("name", "uid", "city","mid","url")
// .primaryKeys("name")
.useCustomizeStringId()
.ifNotExist()
.create();
添加数据
graph.addVertex(T.label, "article",T.id,"id7", "mid", "lakjdflkjalskdjfl", "name", "北京三联WiFi熟练度附近", "uid", 1234567897,"city","未知","url","http://test.com//IodUhh2bV");
graph.addVertex(T.label, "article",T.id,"id2", "mid", "lakjdflkjalskdjfl", "name", "北京三联法律文件而来反馈", "uid", 1234567891,"city","未知","url","http://test.com//IodUhh2bV");
graph.addVertex(T.label, "article",T.id,"id3", "mid", "lakjdflkjalskdjfl", "name", "深圳广州WiFi熟练度附近", "uid", 1234567892,"city","未知","url","http://test.com//IodUhh2bV");
graph.addVertex(T.label, "article",T.id,"id4", "mid", "lakjdflkjalskdjfl", "name", "深圳WiFi熟练度附近", "uid", 1234567893,"city","未知","url","http://test.com//IodUhh2bV");
graph.addVertex(T.label, "article",T.id,"id5", "mid", "lakjdflkjalskdjfl", "name", "广州测试", "uid", 1234567894,"city","未知","url","http://test.com//IodUhh2bV");
graph.addVertex(T.label, "article",T.id,"id6", "mid", "lakjdflkjalskdjfl", "name", "数据", "uid", 1234567895,"city","未知","url","http://test.com//IodUhh2bV");
数据添加成功,查看数据
id | label | properties |
id2 | article | {"name":"北京三联法律文件而来反馈","uid":1234567891,"city":"未知","mid":"lakjdflkjalskdjfl","url":"http://test.com//IodUhh2bV"} |
id3 | article | {"name":"深圳广州WiFi熟练度附近","uid":1234567892,"city":"未知","mid":"lakjdflkjalskdjfl","url":"http://test.com//IodUhh2bV"} |
id4 | article | {"name":"深圳WiFi熟练度附近","uid":1234567893,"city":"未知","mid":"lakjdflkjalskdjfl","url":"http://test.com//IodUhh2bV"} |
id5 | article | {"name":"广州测试","uid":1234567894,"city":"未知","mid":"lakjdflkjalskdjfl","url":"http://test.com//IodUhh2bV"} |
id6 | article | {"name":"数据","uid":1234567895,"city":"未知","mid":"lakjdflkjalskdjfl","url":"http://test.com//IodUhh2bV"} |
id7 | article | {"name":"北京三联WiFi熟练度附近","uid":1234567897,"city":"未知","mid":"lakjdflkjalskdjfl","url":"http://test.com//IodUhh2bV"} |
查询
添加索引的数据会进行分词,我们在HugeGraph中可以看到配置文件 hugegraph.properties 中有配置分词项,这里使用jieba分词
search.text_analyzer=jieba
search.text_analyzer_mode=INDEX
执行查询
g.V().hasLabel("article").has("name", Text.contains("熟练度"))
查询结果:
id | label | properties |
id3 | article | {"name":"深圳广州WiFi熟练度附近","uid":1234567892,"city":"未知","mid":"lakjdflkjalskdjfl","url":"http://test.com//IodUhh2bV"} |
id4 | article | {"name":"深圳WiFi熟练度附近","uid":1234567893,"city":"未知","mid":"lakjdflkjalskdjfl","url":"http://test.com//IodUhh2bV"} |
id7 | article | {"name":"北京三联WiFi熟练度附近","uid":1234567897,"city":"未知","mid":"lakjdflkjalskdjfl","url":"http://test.com//IodUhh2bV"} |
我们可以看出可以模糊查询了
我们再查询“熟练”,发现数据也可以出来
当查询“熟”一个字的时候发现没有数据,搜索“练度”也没有数据,原因是索引将数据进行分词,将分词后的每个词进行索引,如果你搜索的词在分词的时候没有分词出来,最终搜索的结果就会搜索不到,“熟”,“练度”这两个字(词),在JieBa分词中没有,所以搜索不到。
所以查询使用使用Text.contains(),也并非我们开发中判断字符串中是否包含该字符串的意思。