2014-02-10 47 views
0

我在查詢中使用了一系列嵌套的FOREACH循環,該功能在我們用於測試的匹配數據集的許多Neo4j安裝上正常運行。隨着Cypher 2.0.1的最近更改,我的查詢不起作用。如何將我的Neo4j查詢的語法從Cypher 2.0.1恢復到Cypher 2.0.0?

我最初的直覺是取代/var/lib/neo4j/lib/neo4j-cypher-2.0.0.jar文件,但我不想搞砸了。有什麼想法嗎?

樣品圖:http://console.neo4j.org/?id=ktrcwx

這裏的查詢(我強調 「**」 表示發生錯誤的位置):

$ MATCH (total:Recipe) 
>  WITH count(DISTINCT total) AS tots, timestamp() AS time 
>  MATCH (ia:Ingredient)<-[:HAS_INGREDIENT]-(recab:Recipe)-[recHasB:HAS_INGREDIENT]->(ib:Ingredient) 
>  WHERE id(ia)=5167 
>  WITH DISTINCT ib AS idB, count(DISTINCT recab) AS recAB , count(DISTINCT recHasB) AS recB, tots, time 
>  MATCH (i:Ingredient)<-[:HAS_INGREDIENT]-(r:Recipe) 
>  WHERE id(i)=5167 
>  WITH [i, count(DISTINCT r.id), idB, recAB, recB, tots, time] AS c 
>  FOREACH (row IN c | 
>  FOREACH (i1 in **c[0] |** 
>   FOREACH (recA in c[1] | 
>   FOREACH (i2 in c[2]| 
>    FOREACH (recAB in c[3] | 
>    FOREACH (recB in c[4] | 
>     FOREACH (totalRec in c[5] | 
>     CREATE (i1:Ingredient)-[pm1:PMI]->(i2: Ingredient) 
>     SET startNode(pm1).pmiTime = c[6], endNode(pm1).pmiTime = c[6], pm1.weight = log((totalRec*recAB) /(recA*recB)), pm1.pmiTime= c[6] 
>     CREATE (i1:Ingredient)<-[pm2:PMI]-(i2: Ingredient) 
>     SET startNode(pm2).pmiTime = c[6], endNode(pm2).pmiTime = c[6], pm2.weight = log((totalRec*recAB) /(recA*recB)), pm2.pmiTime= c[6] 
>    ) 
>    ) 
>   ) 
>   ) 
>  ) 
>  ) 
> ); 

這裏的錯誤:

SyntaxException: Type mismatch: expected Collection<T> but was Any (line 10, column 25)"  FOREACH (i1 in c[0] |" 

這裏的功能類路徑:

Neo4j Server is running at pid 4347 
NEO4J_HOME:  /var/lib/neo4j 
NEO4J_SERVER_PORT: 7474 
NEO4J_INSTANCE: /var/lib/neo4j 
JAVA_HOME: 
JAVA_OPTS:   -server -XX:+DisableExplicitGC -Dorg.neo4j.server.properties=conf/neo4j-server.properties -Djava.util.logging.config.file=conf/logging.properties -Dlog4j.configuration=file:conf/log4j.properties -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled 
CLASSPATH:   /var/lib/neo4j/lib/concurrentlinkedhashmap-lru-1.3.1.jar:/var/lib/neo4j/lib/geronimo-jta_1.1_spec-1.1.1.jar:/var/lib/neo4j/lib/lucene-core-3.6.2.jar:/var/lib/neo4j/lib/neo4j-cypher-2.0.0.jar:/var/lib/neo4j/lib/neo4j-cypher-commons-2.0.0.jar:/var/lib/neo4j/lib/neo4j-cypher-compiler-1.9-2.0.0.jar:/var/lib/neo4j/lib/neo4j-cypher-compiler-2.0-2.0.0.jar:/var/lib/neo4j/lib/neo4j-graph-algo-2.0.0.jar:/var/lib/neo4j/lib/neo4j-graph-matching-2.0.0.jar:/var/lib/neo4j/lib/neo4j-jmx-2.0.0.jar:/var/lib/neo4j/lib/neo4j-kernel-2.0.0.jar:/var/lib/neo4j/lib/neo4j-lucene-index-2.0.0.jar:/var/lib/neo4j/lib/neo4j-shell-2.0.0.jar:/var/lib/neo4j/lib/neo4j-udc-2.0.0.jar:/var/lib/neo4j/lib/org.apache.servicemix.bundles.jline-0.9.94_1.jar:/var/lib/neo4j/lib/parboiled-core-1.1.6.jar:/var/lib/neo4j/lib/parboiled-scala_2.10-1.1.6.jar:/var/lib/neo4j/lib/scala-library-2.10.3.jar:/var/lib/neo4j/lib/server-api-2.0.0.jar:/var/lib/neo4j/system/lib/asm-3.1.jar:/var/lib/neo4j/system/lib/bcprov-jdk16-140.jar:/var/lib/neo4j/system/lib/commons-beanutils-1.8.0.jar:/var/lib/neo4j/system/lib/commons-beanutils-core-1.8.0.jar:/var/lib/neo4j/system/lib/commons-collections-3.2.1.jar:/var/lib/neo4j/system/lib/commons-compiler-2.6.1.jar:/var/lib/neo4j/system/lib/commons-configuration-1.6.jar:/var/lib/neo4j/system/lib/commons-digester-1.8.1.jar:/var/lib/neo4j/system/lib/commons-io-1.4.jar:/var/lib/neo4j/system/lib/commons-lang-2.4.jar:/var/lib/neo4j/system/lib/commons-logging-1.1.1.jar:/var/lib/neo4j/system/lib/jackson-core-asl-1.9.7.jar:/var/lib/neo4j/system/lib/jackson-jaxrs-1.9.7.jar:/var/lib/neo4j/system/lib/jackson-mapper-asl-1.9.7.jar:/var/lib/neo4j/system/lib/janino-2.6.1.jar:/var/lib/neo4j/system/lib/javax.servlet-3.0.0.v201112011016.jar:/var/lib/neo4j/system/lib/jcl-over-slf4j-1.6.1.jar:/var/lib/neo4j/system/lib/jersey-core-1.9.jar:/var/lib/neo4j/system/lib/jersey-multipart-1.9.jar:/var/lib/neo4j/system/lib/jersey-server-1.9.jar:/var/lib/neo4j/system/lib/jetty-http-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jetty-io-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jetty-security-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jetty-server-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jetty-servlet-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jetty-util-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jetty-webapp-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jetty-xml-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jsr311-api-1.1.2.r612.jar:/var/lib/neo4j/system/lib/logback-access-1.0.9.jar:/var/lib/neo4j/system/lib/logback-classic-1.0.9.jar:/var/lib/neo4j/system/lib/logback-core-1.0.9.jar:/var/lib/neo4j/system/lib/mimepull-1.6.jar:/var/lib/neo4j/system/lib/neo4j-browser-2.0.0.jar:/var/lib/neo4j/system/lib/neo4j-server-2.0.0.jar:/var/lib/neo4j/system/lib/neo4j-server-2.0.0-static-web.jar:/var/lib/neo4j/system/lib/rhino-1.7R3.jar:/var/lib/neo4j/system/lib/rrd4j-2.0.7.jar:/var/lib/neo4j/system/lib/slf4j-api-1.6.2.jar:/var/lib/neo4j/conf/ 

這裏的失靈類路徑:

Neo4j Server is running at pid 1361 
NEO4J_HOME:  /var/lib/neo4j 
NEO4J_SERVER_PORT: 7474 
NEO4J_INSTANCE: /var/lib/neo4j 
JAVA_HOME: 
JAVA_OPTS:   -server -XX:+DisableExplicitGC - Dorg.neo4j.server.properties=conf/neo4j-server.properties -Djava.util.logging.config.file=conf/logging.properties -Dlog4j.configuration=file:conf/log4j.properties -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled 
CLASSPATH:   /var/lib/neo4j/lib/concurrentlinkedhashmap-lru-1.3.1.jar:/var/lib/neo4j/lib/geronimo-jta_1.1_spec-1.1.1.jar:/var/lib/neo4j/lib/lucene-core-3.6.2.jar:/var/lib/neo4j/lib/neo4j-cypher-2.0.1.jar:/var/lib/neo4j/lib/neo4j-cypher-commons-2.0.1.jar:/var/lib/neo4j/lib/neo4j-cypher-compiler-1.9-2.0.1.jar:/var/lib/neo4j/lib/neo4j-cypher-compiler-2.0-2.0.1.jar:/var/lib/neo4j/lib/neo4j-graph-algo-2.0.1.jar:/var/lib/neo4j/lib/neo4j-graph-matching-2.0.1.jar:/var/lib/neo4j/lib/neo4j-jmx-2.0.1.jar:/var/lib/neo4j/lib/neo4j-kernel-2.0.1.jar:/var/lib/neo4j/lib/neo4j-lucene-index-2.0.1.jar:/var/lib/neo4j/lib/neo4j-shell-2.0.1.jar:/var/lib/neo4j/lib/neo4j-udc-2.0.1.jar:/var/lib/neo4j/lib/org.apache.servicemix.bundles.jline-0.9.94_1.jar:/var/lib/neo4j/lib/parboiled-core-1.1.6.jar:/var/lib/neo4j/lib/parboiled-scala_2.10-1.1.6.jar:/var/lib/neo4j/lib/scala-library-2.10.3.jar:/var/lib/neo4j/lib/server-api-2.0.1.jar:/var/lib/neo4j/system/lib/asm-3.1.jar:/var/lib/neo4j/system/lib/bcprov-jdk16-140.jar:/var/lib/neo4j/system/lib/commons-beanutils-1.8.0.jar:/var/lib/neo4j/system/lib/commons-beanutils-core-1.8.0.jar:/var/lib/neo4j/system/lib/commons-collections-3.2.1.jar:/var/lib/neo4j/system/lib/commons-compiler-2.6.1.jar:/var/lib/neo4j/system/lib/commons-configuration-1.6.jar:/var/lib/neo4j/system/lib/commons-digester-1.8.1.jar:/var/lib/neo4j/system/lib/commons-io-1.4.jar:/var/lib/neo4j/system/lib/commons-lang-2.4.jar:/var/lib/neo4j/system/lib/commons-logging-1.1.1.jar:/var/lib/neo4j/system/lib/jackson-core-asl-1.9.7.jar:/var/lib/neo4j/system/lib/jackson-jaxrs-1.9.7.jar:/var/lib/neo4j/system/lib/jackson-mapper-asl-1.9.7.jar:/var/lib/neo4j/system/lib/janino-2.6.1.jar:/var/lib/neo4j/system/lib/javax.servlet-3.0.0.v201112011016.jar:/var/lib/neo4j/system/lib/jcl-over-slf4j-1.6.1.jar:/var/lib/neo4j/system/lib/jersey-core-1.9.jar:/var/lib/neo4j/system/lib/jersey-multipart-1.9.jar:/var/lib/neo4j/system/lib/jersey-server-1.9.jar:/var/lib/neo4j/system/lib/jetty-http-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jetty-io-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jetty-security-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jetty-server-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jetty-servlet-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jetty-util-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jetty-webapp-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jetty-xml-9.0.5.v20130815.jar:/var/lib/neo4j/system/lib/jsr311-api-1.1.2.r612.jar:/var/lib/neo4j/system/lib/logback-access-1.0.9.jar:/var/lib/neo4j/system/lib/logback-classic-1.0.9.jar:/var/lib/neo4j/system/lib/logback-core-1.0.9.jar:/var/lib/neo4j/system/lib/mimepull-1.6.jar:/var/lib/neo4j/system/lib/neo4j-browser-2.0.1.jar:/var/lib/neo4j/system/lib/neo4j-server-2.0.1.jar:/var/lib/neo4j/system/lib/neo4j-server-2.0.1-static-web.jar:/var/lib/neo4j/system/lib/rhino-1.7R3.jar:/var/lib/neo4j/system/lib/rrd4j-2.0.7.jar:/var/lib/neo4j/system/lib/slf4j-api-1.6.2.jar:/var/lib/neo4j/conf/ 

的預期結果注: 對於集合中的每一行,查詢應該創建2個關係:(i1)-[:PMI]->(i2)(i2)-[:PMI]->(i1)[:PMI]關係的權重是Log()函數中的數學運算。該圖是(:Ingredient)(:Recipe)節點。此查詢將創建(i1:Ingredient)(i2:Ingredient)之間的關係,該關係發生在包含(i1)的配方中。這使我瞭解配料配對的可能性。

+0

解釋查詢應該做什麼可能會有幫助。我想你會發現有更簡潔的方式來表達你想要的東西,這對2.0.1來說可以很好地工作。 –

+0

對於集合中的每一行,查詢都應創建一個關係:A-> B和A <-B。這些關係的權重是Log()函數中的數學。 圖表爲:配料和配方節點。此查詢將創建A:配料與每個配方中包含的每個B:配料之間的關係,這使得我可以瞭解配料配對的概率。 我敢肯定,有更簡潔的方法來使這項工作,我很感激在該領域的輸入,但我的問題是爲什麼這個查詢正確創建關係在2.0.0而不是2.0.1。 – lazaruslarue

+0

這在2.0.1中不起作用的原因是它依賴於2.0.0版本中存在的錯誤,並且此後已被修復。在上面的例子中,'c'集合是所有不同類型事物的混合體,因此Cypher的整體類型爲「Collection 」 - 這意味着裏面的每個東西只是一個'Any'(最不實用的類型)。 2.0.0中的錯誤是,任何類型爲「Any」的東西實際上在任何地方都被接受(即Any的類型檢查失敗)。 –

回答

0

您是否可以將您的非收藏物品包裹在[ ]中,並查看是否解決了問題?例如:FOREACH(i1 IN [c[0]] |

好吧,這裏是一筆編譯輕微重寫嘗試 - 將是巨大的,如果你可以張貼在console.neo4j.org一些示例數據,如果它不工作:

MATCH (total:Recipe) 
WITH count(DISTINCT total) AS tots, timestamp() AS time 
MATCH (ia:Ingredient)<-[:HAS_INGREDIENT]-(recab:Recipe)-[recHasB:HAS_INGREDIENT]->(ib:Ingredient) 
WHERE id(ia)=5167 
WITH DISTINCT ib AS idB, count(DISTINCT recab) AS recAB , count(DISTINCT recHasB) AS recB, tots, time 
MATCH (i:Ingredient)<-[:HAS_INGREDIENT]-(r:Recipe) 
WHERE id(i)=5167 
WITH i, count(DISTINCT r.id) as recA, idB as i2, recAB, recB, tots, time 
CREATE (i)-[pm1:PMI {pmiTime:time, weight:log((tots*recAB) /(recA*recB))}]->(i2) 
CREATE (i)<-[pm2:PMI {pmiTime:time, weight:log((tots*recAB) /(recA*recB))}]-(i2) 
SET i.pmiTime = time, pm1.pmiTime = time, i2.pmiTime = time, pm2.pmiTime = time 
+0

另外,你爲什麼迭代行IN C,然後使用C [0]之類的?感覺不對。 –

+0

完全同意,這是一個hacky查詢。很高興有一些關於如何改進工作方式的意見。我花了幾天的時間纔得到它在第一個案件中的工作。如何使這項工作在2.0.1?它仍然適用於2.0.0版本的部署和測試服務器,但它不適用於我試圖用2.0.1構建的新服務器。 – lazaruslarue

+0

另外,是的...我已根據您的建議包裝了所有查詢。我現在收到一個不同的錯誤(指針'totalRec'): SyntaxException:類型不匹配:期望Double或Integer但是Any(第17行,第102列) 「SET startNode(pm1).pmiTime = c [6] ,endNode(pm1).pmiTime = c [6],pm1.weight = log((totalRec * recAB)/(recA * recB)),pm1.pmiTime = c [6]「 I將totalRec替換爲[c [5]]:類型不匹配:預期爲Double或Integer,但爲Any(第17行,第105列) – lazaruslarue