简体   繁体   English

Neo4J IN运营商

[英]Neo4J IN operator

I am having trouble using the IN operator in a Cypher query, using the embedded Java API. 我在使用嵌入式Java API的Cypher查询中使用IN运算符时遇到问题。 It would appear that you are unable to utilize the IN operator with an identifier other than the entity(ies) retrieved in the start clause. 您似乎无法使用IN运算符,其标识符不是在start子句中检索的实体。

For example, here is my query: 例如,这是我的查询:

start n=node(someLongNodeId) 
match n--m 
where m.gnid? IN [\"someStringId\",\"otherStringId\"] 
return m

I can utilize the IN operator when filtering on a property of n , but it fails with the following exception when I execute the above query and attempt to call hasNext() on the Iterator returned from columnAs("m") : 我可以在对n的属性进行过滤时使用IN运算符,但是当我执行上述查询并尝试在从columnAs("m")返回的Iterator上调用hasNext()时,它会失败并出现以下异常:

Exception in thread "main" java.lang.RuntimeException
    at org.neo4j.cypher.internal.pipes.matching.MiniMap.newWith(ExpanderStep.scala:172)
    at org.neo4j.cypher.internal.pipes.matching.MiniMap.newWith(ExpanderStep.scala:155)
    at org.neo4j.cypher.internal.commands.InCollection$$anonfun$isMatch$1.apply(InCollection.scala:39)
    at org.neo4j.cypher.internal.commands.InCollection$$anonfun$isMatch$1.apply(InCollection.scala:38)
    at scala.collection.LinearSeqOptimized$class.exists(LinearSeqOptimized.scala:79)
    at scala.collection.immutable.List.exists(List.scala:45)
    at org.neo4j.cypher.internal.commands.AnyInCollection$$anonfun$seqMethod$2.apply(InCollection.scala:71)
    at org.neo4j.cypher.internal.commands.AnyInCollection$$anonfun$seqMethod$2.apply(InCollection.scala:71)
    at org.neo4j.cypher.internal.commands.InCollection.isMatch(InCollection.scala:38)
    at org.neo4j.cypher.internal.commands.And.isMatch(Predicate.scala:83)
    at org.neo4j.cypher.internal.pipes.matching.FilteringIterable$FilteringIterator.spoolToNextInLine(FilteringIterable.scala:55)
    at org.neo4j.cypher.internal.pipes.matching.FilteringIterable$FilteringIterator.<init>(FilteringIterable.scala:34)
    at org.neo4j.cypher.internal.pipes.matching.FilteringIterable.iterator(FilteringIterable.scala:72)
    at org.neo4j.cypher.internal.pipes.matching.FilteringIterable.iterator(FilteringIterable.scala:27)
    at scala.collection.JavaConversions$IterableWrapperTrait$class.iterator(JavaConversions.scala:557)
    at scala.collection.JavaConversions$IterableWrapper.iterator(JavaConversions.scala:583)
    at scala.collection.JavaConversions$IterableWrapper.iterator(JavaConversions.scala:583)
    at org.neo4j.kernel.impl.traversal.TraversalBranchWithState.expandRelationshipsWithoutChecks(TraversalBranchWithState.java:70)
    at org.neo4j.kernel.impl.traversal.TraversalBranchImpl.expandRelationships(TraversalBranchImpl.java:104)
    at org.neo4j.kernel.impl.traversal.StartNodeTraversalBranch.next(StartNodeTraversalBranch.java:47)
    at org.neo4j.kernel.impl.traversal.AsOneStartBranch.next(AsOneStartBranch.java:100)
    at org.neo4j.kernel.PreorderDepthFirstSelector.next(PreorderDepthFirstSelector.java:52)
    at org.neo4j.kernel.impl.traversal.TraverserIterator.fetchNextOrNull(TraverserIterator.java:65)
    at org.neo4j.kernel.impl.traversal.TraverserIterator.fetchNextOrNull(TraverserIterator.java:34)
    at org.neo4j.helpers.collection.PrefetchingIterator.hasNext(PrefetchingIterator.java:55)
    at scala.collection.JavaConversions$JIteratorWrapper.hasNext(JavaConversions.scala:574)
    at scala.collection.Iterator$$anon$21.hasNext(Iterator.scala:371)
    at scala.collection.Iterator$$anon$21.hasNext(Iterator.scala:371)
    at scala.collection.Iterator$$anon$22.hasNext(Iterator.scala:388)
    at scala.collection.Iterator$$anon$19.hasNext(Iterator.scala:334)
    at scala.collection.Iterator$$anon$19.hasNext(Iterator.scala:334)
    at org.neo4j.cypher.PipeExecutionResult.hasNext(PipeExecutionResult.scala:138)
    at scala.collection.Iterator$$anon$19.hasNext(Iterator.scala:334)
    at scala.collection.Iterator$$anon$19.hasNext(Iterator.scala:334)
    at scala.collection.JavaConversions$IteratorWrapper.hasNext(JavaConversions.scala:562)
    at com.awesomecompany.data.neo.NeoTestDriver.performBidirectionalQuery(NeoTestDriver.java:122)
    at com.awesomecompany.data.neo.NeoTestDriver.main(NeoTestDriver.java:44)

Am I correct in my assumption that you cannot use the IN operator on something other than n in my example? 我是否正确地假设您不能在我的示例中使用除n其他内容的IN运算符? Is this a bug, or is this by design? 这是一个错误,还是这个设计?

I wanted to answer my own question, in case other people are having similar issues. 我想回答我自己的问题,以防其他人遇到类似的问题。 There are a couple things that I want to adress in reference to the above example: 在参考上面的例子时,我想谈谈几件事:

Per this discussion in the neo4j Google Group forum 在neo4j Google Group论坛中进行此讨论

  • I added the HAS() function to the WHERE clause 我将HAS()函数添加到WHERE子句中
  • I also changed the array values to use a single quote ( ' ) instead of the previous double quotes 我还更改了数组值以使用单引号( ' )而不是之前的双引号

So my new example query looks like this: 所以我的新示例查询如下所示:

start n=node(someLongNodeId) 
match n--m 
where HAS(m.gnid) AND m.gnid IN ['someStringId','otherStringId'] 
return m

This worked as expected. 这按预期工作。

After making the above edits, I converted my query to use parameters (which if JDBC has taught me anything, is the correct way to perform queries like this). 在进行了上述编辑之后,我将查询转换为使用参数(如果JDBC教会了我什么,那么执行这样的查询的正确方法)。 That query looked like this: 该查询看起来像这样:

start n=node({nodeId}) 
match n--m 
where HAS(m.gnid) AND m.gnid IN {otherIds}
return m

This also worked correctly. 这也正常。

The IN operator is not terribly well documented at the moment, which is understandable since it is fairly new to the neo4j project, but I wanted to make sure this was answered in case anybody else is having similar troubles. IN操作员目前还没有很好的文档记录,这是可以理解的,因为它对neo4j项目来说还是比较新的,但是我想确保在其他人遇到类似问题的情况下回答这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM