From c50ad91d3167e540599713ba1b8167e8d37c81bb Mon Sep 17 00:00:00 2001 From: sadposter Date: Tue, 14 Dec 2021 15:02:11 +0000 Subject: [PATCH] fix empty queries returning 50 results --- lib/pleroma/elasticsearch/store.ex | 2 + lib/pleroma/search/elasticsearch.ex | 72 ++++++++++++++++++----------- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/lib/pleroma/elasticsearch/store.ex b/lib/pleroma/elasticsearch/store.ex index 93501a2fb..3cf80ebd4 100644 --- a/lib/pleroma/elasticsearch/store.ex +++ b/lib/pleroma/elasticsearch/store.ex @@ -141,6 +141,8 @@ def bulk_post([] = data, :hashtags) 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 = diff --git a/lib/pleroma/search/elasticsearch.ex b/lib/pleroma/search/elasticsearch.ex index 145bdec7b..eabd2a852 100644 --- a/lib/pleroma/search/elasticsearch.ex +++ b/lib/pleroma/search/elasticsearch.ex @@ -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