From 5eb4fb0b8a9eb60d2c1ce78c0ede20da74663db4 Mon Sep 17 00:00:00 2001
From: MareStare <mare.stare.official@gmail.com>
Date: Thu, 27 Mar 2025 01:24:24 +0000
Subject: [PATCH] Add missing sorting to server-side autocomplete

---
 lib/philomena_web/controllers/autocomplete/tag_controller.ex | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/lib/philomena_web/controllers/autocomplete/tag_controller.ex b/lib/philomena_web/controllers/autocomplete/tag_controller.ex
index 5082ff59..c9f52bf4 100644
--- a/lib/philomena_web/controllers/autocomplete/tag_controller.ex
+++ b/lib/philomena_web/controllers/autocomplete/tag_controller.ex
@@ -81,6 +81,10 @@ defmodule PhilomenaWeb.Autocomplete.TagController do
       }
     )
     |> Enum.filter(&(&1.images > 0))
+    # Sometimes we have data desynchronization between OpenSearch and Postgres due
+    # to bugs. This client-side sort serves as a patch to make sure we have correct
+    # ranking of the autocomplete results even in the case of desynchronization.
+    |> Enum.sort_by(& &1.images, :desc)
     |> Enum.take(limit)
   end