Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Fix length query for audio (#661)
Browse files Browse the repository at this point in the history
* Add `length` as the field for audio duration categories

* Update api/catalog/api/serializers/audio_serializers.py

* Add `shortest` length, fix other length ranges

* Make `duration` query param work for `length` ES field

Signed-off-by: Olga Bulat <[email protected]>

* Final renaming of 'duration' query to 'length'
  • Loading branch information
obulat authored May 17, 2022
1 parent 6cc0551 commit d9f83e5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
4 changes: 3 additions & 1 deletion api/catalog/api/constants/field_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"large",
}

DURATION = {
LENGTHS = {
"shortest",
"short",
"medium",
"long",
}
1 change: 1 addition & 0 deletions api/catalog/api/controllers/search_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def search(
("extension", None),
("category", None),
("categories", "category"),
("length", None),
("aspect_ratio", None),
("size", None),
("source", None),
Expand Down
14 changes: 7 additions & 7 deletions api/catalog/api/serializers/audio_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from elasticsearch_dsl.response import Hit

from catalog.api.constants.field_values import AUDIO_CATEGORIES, DURATION
from catalog.api.constants.field_values import AUDIO_CATEGORIES, LENGTHS
from catalog.api.docs.media_docs import fields_to_md
from catalog.api.models import AudioReport
from catalog.api.models.audio import Audio
Expand Down Expand Up @@ -57,7 +57,7 @@ class AudioSearchRequestSerializer(
*MediaSearchRequestSerializer.fields_names,
*AudioSearchRequestSourceSerializer.field_names,
"category",
"duration",
"length",
]
"""
Keep the fields names in sync with the actual fields below as this list is
Expand All @@ -69,9 +69,9 @@ class AudioSearchRequestSerializer(
help_text=make_comma_separated_help_text(AUDIO_CATEGORIES, "categories"),
required=False,
)
duration = serializers.CharField(
label="duration",
help_text=make_comma_separated_help_text(DURATION, "audio lengths"),
length = serializers.CharField(
label="length",
help_text=make_comma_separated_help_text(LENGTHS, "audio lengths"),
required=False,
)

Expand All @@ -81,8 +81,8 @@ def validate_category(value):
return value.lower()

@staticmethod
def validate_duration(value):
_validate_enum("duration", DURATION, value)
def validate_length(value):
_validate_enum("length", LENGTHS, value)
return value.lower()


Expand Down
12 changes: 8 additions & 4 deletions ingestion_server/ingestion_server/elasticsearch_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,10 @@ class Durations(Enum):
api/catalog/api/serializers/audio_serializers.py.
"""

SHORT = 4 * 60 * 1e3 # under 4 minutes
MEDIUM = 20 * 60 * 1e3 # 4 - 20 minutes
LONG = float("inf") # longer than 20 minutes
SHORTEST = 30 * 1e3 # under 30 seconds
SHORT = 2 * 60 * 1e3 # 30 seconds - 2 minutes
MEDIUM = 10 * 60 * 1e3 # 2 - 10 minutes
LONG = float("inf") # longer than 10 minutes

class Index:
name = "audio"
Expand All @@ -320,20 +321,23 @@ def database_row_to_elasticsearch_doc(row, schema):
attrs = Audio.get_instance_attrs(row, schema)
popularity = attrs["standardized_popularity"]

length = Audio.get_length(row[schema["duration"]])

return Audio(
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,
authority_boost=authority_boost,
max_boost=max(popularity or 1, authority_boost or 1),
min_boost=min(popularity or 1, authority_boost or 1),
**attrs,
)

@staticmethod
def get_duration(duration):
def get_length(duration):
if not duration:
return None
for length in Audio.Durations:
Expand Down
1 change: 1 addition & 0 deletions ingestion_server/ingestion_server/es_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def index_settings(table_name):
"sample_rate": {"type": "integer"},
"genres": {"fields": {"keyword": {"type": "keyword"}}, "type": "text"},
"duration": {"type": "integer"},
"length": {"type": "keyword"},
},
}
media_mappings = common_mappings.copy()
Expand Down

0 comments on commit d9f83e5

Please sign in to comment.