neo4j - Cypher query with ORDER BY on translation with fallback -
i have following data model:
- node entity
businessobject
- node entity
translation
containinglanguage
,text
property - relationship
translates
translation business object, potentially multiple
the translations allow me present texts appropriate users language. example, user able read german, business object translated english , german, ui displays german. in general, there's fallback user's language default language (english).
now, query businessobject
s , order them translation should include fallback. possible cypher?
edit: example
businessobject a translations ("en", "xyz english translation a") , ("de", "deutsche Übersetzung für a").
businessobject b translations ("en", "some english translation b") , ("fr", "traduction français pour b").
a user u1 has language german ("de") , fallback should english ("en"). so, querying business objects should result in list [a, b] because translation "deutsch... für a" before "some en...for b" lexicographically when ordering ascending. on other hand, when second user u2 has language french, result should [b, a] because "traduc... pour b" before "xyz eng... a". third english user u3 should result [b, a] because translation "some... b" before "xyz... a".
so need ordering done database if possible.
edit: solution
with great ideas answers, came following query:
match (r:businessobject) r optional match (r)<-[translates]-(t:translation) t.language = 'de' // user language r, t.text t1 optional match (r)<-[translates]-(t:translation) t.language = 'en' // fallback r, coalesce(t1, t.text) sort return distinct r order sort;
surely, hasn't best performance that's ok me. contributers!
i don't know way set order by
that. short of finding answer if exists, option might return fallback separately.
with "english" fallback match (b: businessobject)-[:translates]-(fbtr: translation) translation.language = fallback fallback, fbtr, b match (b)-[:translates]-(tr: translation) translation.language <> fallback return collect(tr) translations, fbtr fallback
edit
here's simpler idea sorts language, putting fallback last:
with 'english' fallback match (b:businessobject)-[:translates]-(t) t, case t.language when fallback -1 else 0 end sort return t order sort desc, t.language
maybe closer you're looking for.
Comments
Post a Comment