HugeGraph 圖數據庫索引介紹 - 範圍索引,全文索引

目錄

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(),也並非我們開發中判斷字符串中是否包含該字符串的意思。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章