cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Get the first or last node from a list

mithun_das
Graph Buddy

First of all I must admit I could not find a suitable title for the problem, thus could not find results in search. Trying to explain it.
I have a situation where i store few translations of a node like following.

(n1)-[:HAS_TRANSLATION { code: "en" }]->(:Translation { text: "some string" })
(n1)-[:HAS_TRANSLATION { code: "esp" }]->(:Translation { text: "another string" })
(n2)-[:HAS_TRANSLATION { code: "en" }]->(:Translation { text: "another string" })
(n3)-[:HAS_TRANSLATION { code: "tr" }]->(:Translation { text: "another string" })
(n3)-[:HAS_TRANSLATION { code: "zh" }]->(:Translation { text: "another string" })

Now i have a situation where i need to search the nodes by translation code such that if i search by code: "en" I will get n1 and n2 (from above example) i do like

match (n)-[:HAS_TRANSLATION { code: "en" }]->(tr:Translation)
return tr.text as name

but I also intend to show other nodes as well such that following list is formed

  • n1 - en - textstring
  • n2 - en - textstring
  • n3 - tr - textstring

I understand I can use apoc.case for this but what I am struggling is
If I use match (n)-[:HAS_TRANSLATION]->(tr:Translation) for n3 i will get 2 non "en" nodes, but i want to pick one of them only.... can i write a query for that so that it gives one node for n3 -> either the tr or the esp one... remember i will not know what translation i have for a node befor hand.
Thanks in advance...

1 ACCEPTED SOLUTION

Ah, okay, so this can be solved with a sorting operation so the desired type of translation comes first, then taking the first one per text.

Something like this:

MATCH (n)-[r:HAS_TRANSLATION]->(tr:Translation)
WITH n, tr
ORDER BY r.code = 'en' DESC
WITH n, collect(tr)[0] as tr
RETURN tr.text as name

View solution in original post

6 REPLIES 6

intouch_vivek
Graph Steward

Hi Mithun,

I saw your insert I have few questions

  1. Why there is no label for n
  2. Please write on your second requirement

Regards
vivek

mithun_das
Graph Buddy

Those were examples.... and there is no second requirement... just wanted a help to understand how to achieve the scenario of getting all nodes irrespecitive of language choice... So to clarify more
I have

(n1)-[tr:HAS_TRANSLATION { code: "en" }]->(:Translation { text: "some string" })
(n1)-[tr:HAS_TRANSLATION { code: "esp" }]->(:Translation { text: "another string" })
(n2)-[tr:HAS_TRANSLATION { code: "en" }]->(:Translation { text: "another string" })
(n3)-[tr:HAS_TRANSLATION { code: "tr" }]->(:Translation { text: "another string" })
(n3)-[tr:HAS_TRANSLATION { code: "zh" }]->(:Translation { text: "another string" })

here n1, n2, n3 are example nodes

now if i search with tr.code==="en" I want to get
n1, n2 in en
and n3 in either tr or esp

I know its really tough to explain... but thanks for trying to help.

It sounds like you only have specific exceptions that you also want to consider.

I think you want to use a WHERE clause here, using a list of codes you're interested in finding:

match ()-[r:HAS_TRANSLATION]->(tr:Translation)
where r.code in ["en", "tr"]
return tr.text as name

If you have many :HAS_TRANSLATION relationships, you might want to also consider fulltext indexes, which can be used over relationships.

Thank you once again, but seems like my question is not clear.....
first of all i do not know what are the languages that can come... each node has several translations.... i will pass one translation code in my query... -> here it is "en"... and i want back all nodes with "en" translation PLUS also those nodes who do not have "en" but atleast have one translation in any code...

So if i have a data like

n1 has english, turkish translation,
n2 has english translation
n3 has spanish, german translation

Then i want output like

n1 -> with english translation
n2 -> with english translation
n3 -> with either one of spanish or german translation

Hopefully this makes my question more clear.

Ah, okay, so this can be solved with a sorting operation so the desired type of translation comes first, then taking the first one per text.

Something like this:

MATCH (n)-[r:HAS_TRANSLATION]->(tr:Translation)
WITH n, tr
ORDER BY r.code = 'en' DESC
WITH n, collect(tr)[0] as tr
RETURN tr.text as name

mithun_das
Graph Buddy

Thank u.. this is what I was looking for.. and once again sorry to make it so complicated in my question
Wish I could have written a better title for this topic so that in future people can readily find this solution.