简体   繁体   English

MATCH查询中的Neo4J数组

[英]Neo4J Arrays in MATCH query

The intention of my Query is to mark similar words. 我的查询的目的是标记相似的单词。

CREATE CONSTRAINT ON (n:Word) ASSERT n.title IS UNIQUE

MATCH (n) WHERE ID(n)={id} 
MERGE (o:Word{title:{title}}) 
WITH n,o MERGE n-[r:SIMILAR{location:'{location}'}]->o 
RETURN ID(o)

n is a existing Word. n是一个现有的单词。 I want to create the relationsship & the other Word ( o ) if they don't exist yet. 我想创建关系和其他单词( o )(如果尚不存在)。

The Problem with this query is, that it works for one title, but if I use a Array with titles the title of the Word o is the whole Array. 此查询的问题是,它适用于一个标题,但是如果我使用带有标题的数组,则单词o的标题就是整个数组。

Can you suggest me another Query that does the same and/or a way to pass multiple values to title. 您能否建议我另一个执行相同的查询和/或将多个值传递给标题的方法。

I'm using the Neography Gem on Rails eg the REST API 我正在使用Rails上的Neography宝石,例如REST API

To use individual values in a parameter array you can use FOREACH , something like 要在参数数组中使用单个值,可以使用FOREACH ,例如

MATCH (n)
WHERE ID (n) = {id}
FOREACH (t IN {title} |
    MERGE (o:Word {title:t})
    MERGE n-[:SIMILAR]->o
)

If you want to pass location also as a parameter (it is actually a string literal in your current query), such that merge operations for n should happen for each title, location pair in a parameter array, you can try 如果您还希望将location作为参数传递(它实际上是当前查询中的字符串文字),以便对参数数组中的每个title, location对进行n合并操作,则可以尝试

FOREACH (map IN {maps} |
    MERGE (o:Word {title:map.title})
    MERGE n-[:SIMILAR {location:map.location}]->o
)

with a parameter that looks something like 参数看起来像

{
    "maps": [
        {
            "title":"neography",
            "location":"1.."
        },{
            "title":"coreography",
            "location":"3.."
        }
    ]
}

Other suggestions: 其他建议:

  1. It's usually not great to look up nodes by internal id from parameter. 通过参数中的内部ID查找节点通常不是很好。 In some cases when chaining queries it may be fine, but in most cases label index lookup would be better: MATCH (n:Word {title:"geography"}) 在某些情况下,将查询链接起来可能会很好,但是在大多数情况下,标签索引查找会更好: MATCH (n:Word {title:"geography"})
  2. If you are not using the transactional cypher endpoint, give it a shot. 如果您不使用交易密码端点,请尝试一下。 You can then make one or more calls with one or more queries in each call within one transaction. 然后,您可以在一个事务中的每个调用中使用一个或多个查询进行一个或多个调用。 Performance improves and you may find you don't need to send the more complex parameter object, but can send many simple queries. 性能提高了,您可能发现不需要发送更复杂的参数对象,但是可以发送许多简单的查询。

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

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