2012-10-13 45 views
23

我正在創建一個新的Neo4j數據庫。我有一種名爲User的節點,我希望用戶的屬性索引爲標識符EmailAddress。如何在數據庫是新的時候設置索引?我注意到在neo4j.properties文件中看起來支持創建索引。然而,當我把這些作爲使Neo4j:一步一步地創建一個自動索引

# 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 

,並添加一個節點和做一個查詢找到一個標識符,我知道存在

START n=node:Identifier(Identifier = "USER0") 
RETURN n; 

然後我得到一個

MissingIndexException: Index `Identifier` does not exist 

怎麼辦我創建一個索引並在開始查詢中使用它?我只想使用配置文件和密碼來實現這一點。即目前我只能在Power Tool Console中玩。

+0

請一定要看看下面驚奇的答案 - 如何索引工作在2.0+中簡單得多 - 正如你所說這是一個新的數據庫,所以希望移植到最新的neo4j版本是一個選項... –

回答

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 

創建節點自動索引

neo4j-sh (0)$ index --create node_auto_index -t Node 

檢查,如果他們存在

neo4j-sh (0)$ index --indexes 

應該返回

Node indexes: 
node_auto_index 

當查詢使用以下語法指定索引

start a = node:node_auto_index(Identifier="USER0") 
return a; 

由於節點是自動索引的索引的名稱是node_auto_index

這些信息從一個評論是在this page

底部

更新

如果你想印出X這是有前自動索引被打開當前數據(其中PROPERTY_NAME是索引的名稱)

START nd =node(*) 
WHERE has(nd.Property_Name) 
WITH nd 
SET nd.Property_Name = nd.Property_Name 
RETURN count(nd); 
+0

[如何在Windows上啓動neo4j-sh](http:// stackoverflow。 com/a/19275558/1174169) – cod3monk3y

+2

使用http:// localhost:7474/webadmin。頁面加載後,選擇控制檯選項卡。你可以在那裏執行shell命令。 – MSRS

+1

這個答案真的很老,我不確定這是否仍然是最好的方法。對此的一些澄清是很好的。 –

8

在Neo4j的2.0,你應該使用標籤和新的約束,而不是

CREATE CONSTRAINT ON (n:User) ASSERT n.Identifier IS UNIQUE 
    CREATE CONSTRAINT ON (n:User) ASSERT n.EmailAddress IS UNIQUE 

如果郵件是不是每個用戶唯一的,只是創造而不是簡單的指標:

CREATE INDEX ON :User(EmailAddress) 
8

指數主要由在財產用於where條件。在Neo4j 2.0中,索引很容易製作。

的標籤上的標籤上創建索引

CREATE INDEX ON :Person(name) 

DROP INDEX

DROP INDEX ON :Person(name) 

創建唯一性約束

CREATE CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE 

刪除唯一性約束

DROP CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE 

上市在Neo4j的瀏覽器的所有索引和約束,下面的命令是有用的

:schema 

列表索引以及針對特定標籤的限制:

:schema ls -l :YourLabel