IT序号网

Neo4j:Index索引

flyfish 2021年06月14日 数据库 290 0

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)
参考: IT虾米网.

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中配置。
参考:IT虾米网

# 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:
IT虾米网

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

评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!