2012-04-26

新手的 Neo4j: node_auto_index

跟著 Cypher Cookbook玩耍的時候,遇上了一個怪問題:
ljm:~/neo4j$ ./neo4j-community-1.7/bin/neo4j-shell 
NOTE: Remote Neo4j graph database service 'shell' at port 1337
Welcome to the Neo4j Shell! Enter 'help' for a list of commands

neo4j-sh (0)$ START a=node:node_auto_index(name="Thomas Anderson") RETURN a
MissingIndexException: Index `node_auto_indedex` does not exist
啥? 我 Google 了一下,發現要把 neo4j.properties 裡面的 node_auto_indexing 打開,所以:
node_auto_indexing=true
node_keys_indexable=name,age
relationship_auto_indexing=true
relationship_keys_indexable=IS_ROOT_OF,KNOWS,CODED_BY
咦? 還是沒有用? 再 Google 了一下,發現打開後 **新增的 nodes** 才有 index,因為它是 lazy index。 把 nodes 砍掉重練,於是可以用 Cypher 了... 試試看,很好玩!
neo4j-sh (0)$ START a=node:node_auto_index(name="Cypher"),
b=node:node_auto_index(name="Thomas Anderson")
MATCH p=shortestPath(a-[r*..10]-b)
RETURN p     
+--------------------------------------------+
| p                                          |
+--------------------------------------------+
| (18)<--[KNOWS,22]--(16)<--[KNOWS,19]--(15) |
+--------------------------------------------+
1 row, 1 ms
要從 neo4jrestclient 來 query 也可以:
>>> c = gdb.extensions.CypherPlugin
>>> r = c.execute_query("START a=node:node_auto_index(name='Trinity') MATCH a-->b RETURN a,b")
>>> [d['self'] for d in r['data'][0]]
[u'http://localhost:7474/db/data/node/30', u'http://localhost:7474/db/data/node/32']
>>> [d['data'] for d in r['data'][1]]
[{u'name': u'Trinity'}, {u'age': 29, u'name': u'Thomas Anderson'}]
>>> [d['data'] for d in r['data'][0]]
[{u'name': u'Trinity'}, {u'lastname': u'Reagan', u'name': u'Cypher'}]
再列一些參考資料:

沒有留言: