Conditional sorting by different text fields in Elastic Search Query

I have a problem sorting some data in an Elastic Search index.

My data in ES are documents with a default name and possibly translation of the name can be found in a translations nested structure:

[
{
    "defaultName": "item1 default name",
    "translations": [
        {
            "languageCode": "en",
            "name": "Item1 in english"
        },
        {
            "languageCode": "da",
            "name": "Item1 in danish"
        }
    ]
},
{
    "defaultName": "item2 default name",
    "translations": [
        {
            "languageCode": "en",
            "name": "Item2 in english"
        }
    ]
},
{
    "defaultName": "item3 default name",
    "translations": [] 
}
]

When I perform a blank search I need to sort the search result by default name or translated name depending on whether or not the name has been translated or not.

When I search in english (en) the sorting should be done between:

  • Item1 in english
  • Item2 in english
  • Item3 default name

When I search in danish (da) the sorting should be done between:

  • Item1 in danish
  • Item2 default name
  • Item3 default name

I have tried much to get there, but I would like hear if this is possible at all? and maybe a way to do it. I believe I cannot calculate custom scores in this scenario as the sorting is based on text comparison.

If everything fails my fallback solution is to populate non-existing translations with the default translation, but I don't like this solution as it seems much more clean to avoid duplicate data.

Can anyone help me out? :slight_smile:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.