简体   繁体   中英

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. I want to create the relationsship & the other Word ( o ) if they don't exist yet.

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.

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

To use individual values in a parameter array you can use FOREACH , something like

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

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. 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"})
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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