add extra filters
authorFloatingGhost <hannah@coffee-and-dreams.uk>
Sat, 11 Dec 2021 19:56:15 +0000 (19:56 +0000)
committerFloatingGhost <hannah@coffee-and-dreams.uk>
Sun, 12 Dec 2021 17:35:35 +0000 (17:35 +0000)
lib/pleroma/elasticsearch/store.ex
lib/pleroma/web/mastodon_api/controllers/search_controller.ex

index d9e9ed1a73057ee21db2e19a36929cf3cc47b422..55c459801fe7bd1bba97a6c112bc16d0d886964a 100644 (file)
@@ -2,6 +2,10 @@ defmodule Pleroma.Elasticsearch do
   alias Pleroma.Activity
   alias Pleroma.Elasticsearch.DocumentMappings
 
+  @searchable [
+    "hashtag", "instance", "user"
+  ]
+
   defp url do
     Pleroma.Config.get([:elasticsearch, :url])
   end
@@ -34,12 +38,82 @@ defmodule Pleroma.Elasticsearch do
     )
   end
 
+  defp parse_term(t) do
+    if String.contains?(t, ":") and !String.starts_with?(t, "\"") do
+      [field, query] = String.split(t, ":")
+      if Enum.member?(@searchable, field) do
+        {field, query}
+      else
+        {"content", query}
+      end
+    else
+        {"content", t}
+    end
+  end
+
+  defp search_user(params, q) do
+    if q["user"] != nil do
+      params ++ [%{match: %{user: %{
+        query: Enum.join(q["user"], " "),
+        operator: "OR"
+      }}}]
+    else
+      params
+    end
+  end
+
+  defp search_instance(params, q) do
+    if q["instance"] != nil do 
+      params ++ [%{match: %{instance: %{
+        query: Enum.join(q["instance"], " "),
+        operator: "OR"
+      }}}]
+    else
+      params
+    end
+  end
+
+  defp search_content(params, q) do
+    if q["content"] != nil do
+      params ++ [%{match: %{content: %{
+        query: Enum.join(q["content"], " "),
+        operator: "AND"
+      }}}]
+    else
+      params
+    end
+ end
+  defp to_es(q) do
+    []
+    |> search_content(q)
+    |> search_instance(q)
+    |> search_user(q)
+  end
+
+  defp parse(query) do
+    String.split(query, " ")
+    |> Enum.map(&parse_term/1)
+    |> Enum.reduce(%{}, fn {field, query}, acc ->
+        Map.put(acc, field,
+            Map.get(acc, field, []) ++ [query]
+        )
+    end)
+    |> to_es()
+  end
+    
   def search(query) do
+    q = %{query: %{
+      bool: %{
+        must: parse(query)
+      }
+    }}
+    IO.inspect(q)
     Elastix.Search.search(
         url(),
         "activities",
         ["activity"],
-        %{query: %{term: %{content: query}}}
+        q
     )
   end
 end
index 484a959afcbc4ef7b8d7876631d0a7b9c5e25e9e..920ff5980993e5e03ff9985d0ab8c75389d51cb7 100644 (file)
@@ -56,9 +56,9 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do
   defp elasticsearch_search(%{assigns: %{user: user}} = conn, query, options) do
     with {:ok, raw_results} <- Pleroma.Elasticsearch.search(query) do
       results = raw_results
-      |> Map.get(:body)
-      |> Map.get("hits")
-      |> Map.get("hits")
+      |> Map.get(:body, %{})
+      |> Map.get("hits", %{})
+      |> Map.get("hits", [])
       |> Enum.map(fn result -> result["_id"] end)
       |> Pleroma.Activity.all_by_ids_with_object()