Paolo Di Pietro
Paolo Di Pietro

Reputation: 529

A complex match

I got the following cypher query:

neo4j-sh$ start n=node(1344) match (n)-[t:_HAS_TRANSLATION]-(p) return t,p;
+-----------------------------------------------------------------------------------+
| t                                   | p                                           |
+-----------------------------------------------------------------------------------+
| :_HAS_TRANSLATION[2224]{of:"value"} | Node[1349]{language:"hi-hi",text:"(>0@"}    |
| :_HAS_TRANSLATION[2223]{of:"value"} | Node[1348]{language:"es-es",text:"hembra"}  |
| :_HAS_TRANSLATION[2222]{of:"value"} | Node[1347]{language:"ru-ru",text:"65=A:89"} |
| :_HAS_TRANSLATION[2221]{of:"value"} | Node[1346]{language:"en-us",text:"female"}  |
| :_HAS_TRANSLATION[2220]{of:"value"} | Node[1345]{language:"it-it",text:"femmina"} |
+-----------------------------------------------------------------------------------+

and the following array: ["it-it", "en-us", "fr-fr", "de-de", "ru-ru", "hi-hi"]

How can I change the query to return just one result, where 'language' is the same as the first occurrence of the array?

If the array should be ["fr-fr","jp-jp","en-us", "it-it", "de-de", "ru-ru", "hi-hi"]

I'd need to return the Node[1346], because it is the first with a match in the language array [en-us], being no entry for [fr-fr] and [jp-jp]

Thank you

Paolo

Upvotes: 0

Views: 53

Answers (1)

FrobberOfBits
FrobberOfBits

Reputation: 18002

Cypher can express arrays, and indexes into them. So on one level, you could do this:

start n=node(1344)
match (n)-[t:_HAS_TRANSLATION]-(p)         
where p.language = ["it-it", "en-us", "fr-fr", "de-de", "ru-ru", "hi-hi"][0] return t,p; 

This is really just the same as asking for those nodes p where p.language="it-it" (the first element in your array).

Now, if what you mean is that the language attribute itself can be an array, then just treat it like one:

$ create (p { language: ['it-it', 'en-us']});
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 1
Properties set: 1
$ match (p) where p.language[0] = 'it-it' return p;
+-------------------------------------+
| p                                   |
+-------------------------------------+
| Node[1]{language:["it-it","en-us"]} |
+-------------------------------------+
1 row
38 ms

Note the array brackets on p.language[0].

Finally, if what you're talking about is splitting your language parts into pieces (i.e. "en-us" = ["en", "us"]) then cypher's string processing functions are a little on the weak side, and I wouldn't try to do this. Instead, I'd pre-process that before inserting it into the graph in the first place, and query them as separate pieces.

Upvotes: 1

Related Questions