Neo4j:逐步创建自动索引

Ara*_*and 23 database indexing neo4j database-indexes cypher

我正在创建一个新的Neo4j数据库.我有一种名为User的节点,我想要一个关于用户IdentifierEmailAddress属性的索引.如何在数据库是新的时设置索引?我注意到在neo4j.properties文件中看起来支持创建索引.但是,当我这样设置时

# Autoindexing

# 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=EmailAddress,Identifier
Run Code Online (Sandbox Code Playgroud)

并添加一个节点并执行查询以查找我知道存在的标识符

START n=node:Identifier(Identifier = "USER0")
RETURN n;
Run Code Online (Sandbox Code Playgroud)

然后我得到了

MissingIndexException: Index `Identifier` does not exist
Run Code Online (Sandbox Code Playgroud)

如何创建索引并在启动查询中使用它?我只想使用配置文件和cypher来实现这一点.即目前我只在电动工具控制台玩.

Ara*_*and 51

将以下内容添加到neo4j.properties文件中

# Autoindexing

# 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=EmailAddress,Identifier
Run Code Online (Sandbox Code Playgroud)

为节点创建自动索引

neo4j-sh (0)$ index --create node_auto_index -t Node
Run Code Online (Sandbox Code Playgroud)

检查它们是否存在

neo4j-sh (0)$ index --indexes
Run Code Online (Sandbox Code Playgroud)

应该回来

Node indexes:
node_auto_index
Run Code Online (Sandbox Code Playgroud)

查询时使用以下语法指定索引

start a = node:node_auto_index(Identifier="USER0")
return a;
Run Code Online (Sandbox Code Playgroud)

由于节点是自动索引的,因此索引的名称是 node_auto_index

此信息来自本页底部的评论

更新

如果您想要在打开自动索引之前索引当前数据(其中Property_Name是索引的名称)

START nd =node(*) 
WHERE has(nd.Property_Name)
WITH nd
SET nd.Property_Name = nd.Property_Name
RETURN count(nd);
Run Code Online (Sandbox Code Playgroud)

  • 使用http:// localhost:7474/webadmin.页面加载后,选择控制台选项卡.你可以在那里执行shell命令. (2认同)

bog*_*gle 8

在Neo4j 2.0中,您应该使用标签和新约束

    CREATE CONSTRAINT ON (n:User) ASSERT n.Identifier IS UNIQUE
    CREATE CONSTRAINT ON (n:User) ASSERT n.EmailAddress IS UNIQUE
Run Code Online (Sandbox Code Playgroud)

如果电子邮件不是每个用户唯一,则只需创建一个普通索引:

    CREATE INDEX ON :User(EmailAddress)
Run Code Online (Sandbox Code Playgroud)


Som*_*luk 8

Indexes mainly made on property which is used for where condition. In Neo4j 2.0, indexes are easy to make now.

Create index on a label

CREATE INDEX ON :Person(name)
Run Code Online (Sandbox Code Playgroud)

Drop index on a label

DROP INDEX ON :Person(name)
Run Code Online (Sandbox Code Playgroud)

Create uniqueness constraint

CREATE CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE
Run Code Online (Sandbox Code Playgroud)

Drop uniqueness constraint

DROP CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE
Run Code Online (Sandbox Code Playgroud)

For listing all indexes and constraints in neo4j-browser, following command is useful

:schema
Run Code Online (Sandbox Code Playgroud)

List indices and constraints for specific label with:

:schema ls -l :YourLabel
Run Code Online (Sandbox Code Playgroud)