Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Use the database category value for Elasticsearch model (#829)

Olga Bulat1f32bb8b30bf
diff --git a/api/catalog/api/models/image.py b/api/catalog/api/models/image.py
index a848a9470..1db7833b8 100644
--- a/api/catalog/api/models/image.py
+++ b/api/catalog/api/models/image.py
@@ -42,7 +42,7 @@ class Image(ImageFileMixin, AbstractMedia):
"""
Inherited fields
================
- category: eg. photograph, digitised artwork & illustration
+ category: eg. photograph, digitized_artwork & illustration
"""
class Meta(AbstractMedia.Meta):
diff --git a/api/catalog/api/serializers/image_serializers.py b/api/catalog/api/serializers/image_serializers.py
index fcae44eac..a7281d31c 100644
--- a/api/catalog/api/serializers/image_serializers.py
+++ b/api/catalog/api/serializers/image_serializers.py
@@ -47,7 +47,7 @@ class ImageSearchRequestSerializer(
used to generate Swagger documentation.
"""
- # Ref: ingestion_server/ingestion_server/categorize.py#Category
+ # Ref: api/catalog/api/constants/field_values.py
category = EnumCharField(
plural="categories",
enum_class=IMAGE_CATEGORIES,
diff --git a/ingestion_server/ingestion_server/categorize.py b/ingestion_server/ingestion_server/categorize.py
deleted file mode 100644
index 60bc2d7cd..000000000
--- a/ingestion_server/ingestion_server/categorize.py
+++ /dev/null
@@ -1,53 +0,0 @@
-"""
-https://github.com/creativecommons/cccatalog-api/issues/340
-
-Attempt to figure out the image type (illustration, vector, photograph, or
-digitized artwork) based on its source and file extension.
-"""
-
-from enum import Enum, auto
-
-
-class Category(Enum):
- PHOTOGRAPH = auto()
- DIGITIZED_ARTWORK = auto()
- ILLUSTRATION = auto()
-
-
-# One-to-one mapping of providers to categories
-source_category = {
- "__default": None,
- "thorvaldsenmuseum": Category.DIGITIZED_ARTWORK,
- "svgsilh": Category.ILLUSTRATION,
- "phylopic": Category.ILLUSTRATION,
- "floraon": Category.PHOTOGRAPH,
- "animaldiversity": Category.PHOTOGRAPH,
- "WoRMS": Category.PHOTOGRAPH,
- "clevelandmuseum": Category.DIGITIZED_ARTWORK,
- "CAPL": Category.PHOTOGRAPH,
- "sciencemuseum": Category.PHOTOGRAPH,
- "rijksmuseum": Category.DIGITIZED_ARTWORK,
- "museumsvictoria": Category.DIGITIZED_ARTWORK,
- "met": Category.DIGITIZED_ARTWORK,
- "mccordmuseum": Category.DIGITIZED_ARTWORK,
- "digitaltmuseum": Category.DIGITIZED_ARTWORK,
- "deviantart": Category.DIGITIZED_ARTWORK,
- "brooklynmuseum": Category.DIGITIZED_ARTWORK,
-}
-
-
-def get_category(extension, source):
- """
- Return the category of an image based on its extension and source.
- :param extension: the file extension of the image
- :param source: the source providing this image
- :return: the category of this image
- """
-
- if extension and extension.lower() == "svg":
- category = Category.ILLUSTRATION
- elif source in source_category:
- category = source_category[source]
- else:
- category = None
- return category.name.lower() if category is not None else None
diff --git a/ingestion_server/ingestion_server/elasticsearch_models.py b/ingestion_server/ingestion_server/elasticsearch_models.py
index 11490972c..8b46bc781 100644
--- a/ingestion_server/ingestion_server/elasticsearch_models.py
+++ b/ingestion_server/ingestion_server/elasticsearch_models.py
@@ -10,7 +10,6 @@ from enum import Enum, auto
from elasticsearch_dsl import Document, Field, Integer
from ingestion_server.authority import get_authority_boost
-from ingestion_server.categorize import get_category
class RankFeature(Field):
@@ -65,7 +64,7 @@ class Media(SyncableDocType):
name = "media"
@staticmethod
- def database_row_to_elasticsearch_doc(row, schema):
+ def database_row_to_elasticsearch_doc(row: tuple, schema: dict[str, int]):
"""
Map each row in the downstream database to a Python dictionary that
represents a document in the ElasticSearch index.
@@ -96,6 +95,9 @@ class Media(SyncableDocType):
popularity = Media.get_popularity(row[schema["standardized_popularity"]])
else:
popularity = None
+ # Extracted for compatibility with the old image schema to pass the
+ # cleanup tests in CI: test/unit_tests/test_cleanup.py
+ category = row[schema["category"]] if "category" in schema else None
return {
"_id": row[schema["id"]],
@@ -112,6 +114,7 @@ class Media(SyncableDocType):
"license_url": Media.get_license_url(meta),
"provider": row[schema["provider"]],
"source": row[schema["source"]],
+ "category": category,
"created_on": row[schema["created_on"]],
"tags": Media.parse_detailed_tags(row[schema["tags"]]),
"mature": Media.get_maturity(meta, row[schema["mature"]]),
@@ -227,11 +230,7 @@ class Image(Media):
@staticmethod
def database_row_to_elasticsearch_doc(row, schema):
- source = row[schema["source"]]
extension = Image.get_extension(row[schema["url"]])
- category = get_category(extension, source)
- if "category" in schema:
- category = row[schema["category"]]
height = row[schema["height"]]
width = row[schema["width"]]
@@ -243,11 +242,11 @@ class Image(Media):
authority_boost = Image.get_authority_boost(meta, provider)
attrs = Image.get_instance_attrs(row, schema)
+ attrs["category"] = attrs["category"]
popularity = attrs["standardized_popularity"]
return Image(
thumbnail=row[schema["thumbnail"]],
- category=category,
aspect_ratio=aspect_ratio,
extension=extension,
size=size,
@@ -333,7 +332,6 @@ class Audio(Media):
bit_rate=row[schema["bit_rate"]],
sample_rate=row[schema["sample_rate"]],
genres=row[schema["genres"]],
- category=row[schema["category"]],
duration=row[schema["duration"]],
length=length,
filetype=filetype,