Neo4j:Index索引


Indexing in Neo4j: An Overview
by Stefan Armbruster · Jan. 06, 14 · Java Zone

Neo4j是一個圖數據庫,在做圖的檢索時,用index確定圖檢索graph travesal的起始節點start point。過去的數次版本更迭,index索引的實現方式發生了多次變化。這個Overview的主要目的是解釋一下各種index方式的由來和概念,以使neo4j的新用戶不產生概念上的混淆。

No Indexes in the Beginning
一開始,neo4j是沒有Index索引的。在做graph的遍歷的時候,需要從Reference Node開始。只有通過給Node綁定一些標志來獲得Reference Node。Reference Node或者”Node 0”被當作一個全局的變量使用。直到neo4j 1.9.x 版本,GraphDatabaseService類有了getReferenceNode()方法,來獲取Reference Node。當然,getReferenceNode()方法在neo4j 2.0版本以后已經被廢棄了。

Manual Indexes
Manual Index(手動索引,先這么翻譯吧~)在neo4j 1.0版本之前已經開始籌備了,那時候neo4j還沒有Cypher和server模式,只能使用java API操作Graph。Manual Index是通過java API添加的。
建manual Index的方法

IndexManager index = graphDb.index();
Index<Node> nodeIndex = index.forNodes( "nodes" );
Node node = graphDb.createNode();
nodeIndex.add( node, "name", "Thomas Anderson" );
   
   
  
  
          

如果有manual index,可以用cypher查詢:

START n=node:Person(name='abc') RETURN n
   
   
  
  
          

manual index的缺點
1、建manual索引比較麻煩。
2、程序員會濫用index,index應該只用於檢索,而不應該存儲多余的信息。
manual index的優點
可以自己控制建索引是使用什么分詞器(Analyzer)
參考: http://docs.neo4j.org/chunked/stable/indexing-create-advanced.html.

35.10. Configuration and fulltext indexes
At the time of creation extra configuration can be specified to control the behavior of the index and which backend to use. For example to create a Lucene fulltext index:

IndexManager index = graphDb.index();
Index<Node> fulltextMovies = index.forNodes( "movies-fulltext", MapUtil.stringMap( IndexManager.PROVIDER, "lucene", "type", "fulltext"));
fulltextMovies.add( theMatrix, "title", "The Matrix" );
fulltextMovies.add( theMatrixReloaded, "title", "The Matrix Reloaded" );
// search in the fulltext index
Node found = fulltextMovies.query( "title", "reloAdEd" ).getSingle();
   
   
  
  
          

Here’s an example of how to create an exact index which is case-insensitive:

Index<Node> index = graphDb.index().forNodes( "exact-case-insensitive", stringMap( "type", "exact", "to_lower_case", "true" ) );
Node node = graphDb.createNode();
index.add( node, "name", "Thomas Anderson" );
assertContains( index.query( "name", "\"Thomas Anderson\"" ), node );
assertContains( index.query( "name", "\"thoMas ANDerson\"" ), node );
   
   
  
  
          

Automatic Indexes
Neo4j 1.4引入了自動索引(automatic index),使用自動建索引,在config/neo4j.properties中配置。
參考:http://www.cnblogs.com/nyzhai/p/4515102.html

# Enable auto-indexing for nodes, default is false.
node_auto_indexing=true
# The node property keys to be auto-indexed, if enabled.
node_keys_indexable=name,ki
# Enable auto-indexing for relationships, default is false.
relationship_auto_indexing=true
# The relationship property keys to be auto-indexed, if enabled.
relationship_keys_indexable=name,ki
   
   
  
  
          

cypher使用自動索引

START n=node:node_auto_index(name='abc') RETURN n
   
   
  
  
          

Schema Indexes
cypher建schema Index:

CREATE INDEX ON :Person(name);
   
   
  
  
          

使用schema Index:

MATCH (p:Person {name: 'Stefan'}) RETURN p
   
   
  
  
          

cypher查詢時,如果有schema Index會使用索引;如果沒有,會逐條掃描。schema Index索引是透明的。

Reference:
https://dzone.com/articles/indexing-neo4j-overview

原文地址:https://blog.csdn.net/u011697278/article/details/52462420


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM