add hashtag indexing
authorFloatingGhost <hannah@coffee-and-dreams.uk>
Tue, 14 Dec 2021 12:37:10 +0000 (12:37 +0000)
committerFloatingGhost <hannah@coffee-and-dreams.uk>
Tue, 14 Dec 2021 12:37:10 +0000 (12:37 +0000)
lib/mix/tasks/pleroma/search.ex
lib/pleroma/elasticsearch/document_mappings/hashtag.ex [new file with mode: 0644]
lib/pleroma/elasticsearch/store.ex
priv/es-mappings/hashtag.json

index 64396b92fe792f51e38b1e3494755520e97994da..751e0ca11d57cffb567ceabf9b96ec99d8059ea8 100644 (file)
@@ -9,6 +9,7 @@ defmodule Mix.Tasks.Pleroma.Search do
   alias Pleroma.Activity
   alias Pleroma.Pagination
   alias Pleroma.User
+  alias Pleroma.Hashtag
 
   @shortdoc "Manages elasticsearch"
 
@@ -29,8 +30,16 @@ defmodule Mix.Tasks.Pleroma.Search do
     |> get_all(:users)
   end
 
+  def run(["import", "hashtags" | _rest]) do
+    start_pleroma()
+
+    from(h in Hashtag)
+    |> Pleroma.Repo.all()
+    |> Pleroma.Elasticsearch.bulk_post(:hashtags)
+  end
+
   defp get_all(query, index, max_id \\ nil) do
-    params = %{limit: 2000}
+    params = %{limit: 1000}
 
     params =
       if max_id == nil do
diff --git a/lib/pleroma/elasticsearch/document_mappings/hashtag.ex b/lib/pleroma/elasticsearch/document_mappings/hashtag.ex
new file mode 100644 (file)
index 0000000..1c47d14
--- /dev/null
@@ -0,0 +1,10 @@
+defmodule Pleroma.Elasticsearch.DocumentMappings.Hashtag do
+  def id(obj), do: obj.id
+
+  def encode(hashtag) do
+    %{
+      hashtag: hashtag.name,
+      timestamp: hashtag.inserted_at
+    }
+  end
+end
index 776bad92133a610675a40b43080562f9d39ecad2..74c9330387e517603e636e89abe95726e78a9e88 100644 (file)
@@ -85,6 +85,25 @@ defmodule Pleroma.Elasticsearch do
     )
   end
 
+  def bulk_post(data, :hashtags) do
+    d =
+      data
+      |> Enum.map(fn d ->
+        [
+          %{index: %{_id: DocumentMappings.Hashtag.id(d)}},
+          DocumentMappings.Hashtag.encode(d)
+        ]
+      end)
+      |> List.flatten()
+
+    Elastix.Bulk.post(
+      url(),
+      d,
+      index: "hashtags",
+      type: "hashtag"
+    )
+  end
+
   def search_activities(q) do
     Elastix.Search.search(
       url(),
index 5330b8a2e0f61d25792b7730d7319a3fdb3f5475..3a0ade8f8048ed870fec5cfedc00bf3ddc507d60 100644 (file)
@@ -1,5 +1,9 @@
 {
   "properties": {
+    "timestamp": {
+      "type": "date",
+      "index": true
+    },
     "hashtag": {
       "type": "text"
     }