1

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 e.g. the REST API

wintersolutions
  • 5,173
  • 5
  • 30
  • 51

1 Answers1

2

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.
wintersolutions
  • 5,173
  • 5
  • 30
  • 51
jjaderberg
  • 9,844
  • 34
  • 34
  • I think the parameters need to be in curly braces too, at least thats what I had to do to make it work. This is one of the best answers I ever received on SO. It offers multiple solutions to the problem and gives me pointers on how to improve other aspects. Thanks! – wintersolutions Apr 19 '14 at 08:14