From f9a6240014ff76339f5ee8c4dfaa8458cac9d97e Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 21 Apr 2024 21:20:43 -0400 Subject: [PATCH] Add tag category counts to index --- index/images.mk | 15 ++++++++++++- lib/philomena/images/elasticsearch_index.ex | 24 +++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/index/images.mk b/index/images.mk index cc93f9c8..8c843ee2 100644 --- a/index/images.mk +++ b/index/images.mk @@ -72,7 +72,20 @@ galleries: image_search_json tags: image_search_json psql $(DATABASE) -v ON_ERROR_STOP=1 <<-SQL - insert into temp_images.image_search_json (image_id, object) select it.image_id, jsonb_build_object('tag_ids', jsonb_agg(it.tag_id), 'tag_count', count(*)) from image_taggings it group by image_id; + insert into temp_images.image_search_json (image_id, object) select it.image_id, jsonb_build_object( + 'tag_ids', jsonb_agg(it.tag_id), + 'tag_count', count(*), + 'error_tag_count', count(case when t.category = 'error' then t.category else null end), + 'rating_tag_count', count(case when t.category = 'rating' then t.category else null end), + 'origin_tag_count', count(case when t.category = 'origin' then t.category else null end), + 'character_tag_count', count(case when t.category = 'character' then t.category else null end), + 'oc_tag_count', count(case when t.category = 'oc' then t.category else null end), + 'species_tag_count', count(case when t.category = 'species' then t.category else null end), + 'body_type_tag_count', count(case when t.category = 'body-type' then t.category else null end), + 'content_fanmade_tag_count', count(case when t.category = 'content-fanmade' then t.category else null end), + 'content_official_tag_count', count(case when t.category = 'content-official' then t.category else null end), + 'spoiler_tag_count', count(case when t.category = 'spoiler' then t.category else null end), + ) from image_taggings it inner join tags t on t.id = it.tag_id group by image_id; SQL sources: image_search_json diff --git a/lib/philomena/images/elasticsearch_index.ex b/lib/philomena/images/elasticsearch_index.ex index b76912bd..64872d1a 100644 --- a/lib/philomena/images/elasticsearch_index.ex +++ b/lib/philomena/images/elasticsearch_index.ex @@ -87,7 +87,17 @@ defmodule Philomena.Images.ElasticsearchIndex do namespace: %{type: "keyword"} } }, - approved: %{type: "boolean"} + approved: %{type: "boolean"}, + error_tag_count: %{type: "integer"}, + rating_tag_count: %{type: "integer"}, + origin_tag_count: %{type: "integer"}, + character_tag_count: %{type: "integer"}, + oc_tag_count: %{type: "integer"}, + species_tag_count: %{type: "integer"}, + body_type_tag_count: %{type: "integer"}, + content_fanmade_tag_count: %{type: "integer"}, + content_official_tag_count: %{type: "integer"}, + spoiler_tag_count: %{type: "integer"} } } } @@ -151,7 +161,17 @@ defmodule Philomena.Images.ElasticsearchIndex do upvoters: image.upvoters |> Enum.map(&String.downcase(&1.name)), downvoters: image.downvoters |> Enum.map(&String.downcase(&1.name)), deleted_by_user: if(!!image.deleter, do: image.deleter.name), - approved: image.approved + approved: image.approved, + error_tag_count: Enum.count(image.tags, &(&1.category == "error")), + rating_tag_count: Enum.count(image.tags, &(&1.category == "rating")), + origin_tag_count: Enum.count(image.tags, &(&1.category == "origin")), + character_tag_count: Enum.count(image.tags, &(&1.category == "character")), + oc_tag_count: Enum.count(image.tags, &(&1.category == "oc")), + species_tag_count: Enum.count(image.tags, &(&1.category == "species")), + body_type_tag_count: Enum.count(image.tags, &(&1.category == "body-type")), + content_fanmade_tag_count: Enum.count(image.tags, &(&1.category == "content-fanmade")), + content_official_tag_count: Enum.count(image.tags, &(&1.category == "content-official")), + spoiler_tag_count: Enum.count(image.tags, &(&1.category == "spoiler")) } end