fix empty queries returning 50 results
authorsadposter <hannah+pleroma@coffee-and-dreams.uk>
Tue, 14 Dec 2021 15:02:11 +0000 (15:02 +0000)
committersadposter <hannah+pleroma@coffee-and-dreams.uk>
Tue, 14 Dec 2021 15:02:11 +0000 (15:02 +0000)
lib/pleroma/elasticsearch/store.ex
lib/pleroma/search/elasticsearch.ex

index f8d9090d7f570a3db28b3bb057f48baeaf6c3d50..5f2acd420d326fae04412bc94de847b6f0dab283 100644 (file)
@@ -142,6 +142,8 @@ defmodule Pleroma.Elasticsearch do
 
   def bulk_post(_, :hashtags), do: {:ok, nil}
 
+  def search(_, _, _, :skip), do: []
+
   def search(:raw, index, type, q) do
     with {:ok, raw_results} <- Elastix.Search.search(url(), index, [type], q) do
       results =
index 145bdec7b6c33824a34869fc10c4159ad11a104e..eabd2a8521a3fd56c27858c0be438c93f2bf33d4 100644 (file)
@@ -8,45 +8,63 @@ defmodule Pleroma.Search.Elasticsearch do
   alias Pleroma.Web.Endpoint
 
   def es_query(:activity, query) do
-    %{
-      size: 50,
-      terminate_after: 50,
-      timeout: "5s",
-      sort: [
-        %{"_timestamp" => "desc"}
-      ],
-      query: %{
-        bool: %{
-          must: Parsers.Activity.parse(query)
+    must = Parsers.Activity.parse(query)
+
+    if must == [] do
+      :skip
+    else
+      %{
+        size: 50,
+        terminate_after: 50,
+        timeout: "5s",
+        sort: [
+          %{"_timestamp" => "desc"}
+        ],
+        query: %{
+          bool: %{
+            must: must
+          }
         }
       }
-    }
+    end
   end
 
   def es_query(:user, query) do
-    %{
-      size: 50,
-      terminate_after: 50,
-      timeout: "5s",
-      query: %{
-        bool: %{
-          must: Parsers.User.parse(query)
+    must = Parsers.User.parse(query)
+
+    if must == [] do
+      :skip
+    else
+      %{
+        size: 50,
+        terminate_after: 50,
+        timeout: "5s",
+        query: %{
+          bool: %{
+            must: must
+          }
         }
       }
-    }
+    end
   end
 
   def es_query(:hashtag, query) do
-    %{
-      size: 50,
-      terminate_after: 50,
-      timeout: "5s",
-      query: %{
-        bool: %{
-          must: Parsers.Hashtag.parse(query)
+    must = Parsers.Hashtag.parse(query)
+
+    if must == [] do
+      :skip
+    else
+      %{
+        size: 50,
+        terminate_after: 50,
+        timeout: "5s",
+        query: %{
+          bool: %{
+            must: Parsers.Hashtag.parse(query)
+          }
         }
       }
-    }
+    end
   end
 
   @impl Pleroma.Search