Skip to content

Commit

Permalink
Add settings for custom metadata facets and index them (#960)
Browse files Browse the repository at this point in the history
  • Loading branch information
blms committed Dec 19, 2023
1 parent 5a7dc6a commit 32ca1a8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
33 changes: 33 additions & 0 deletions apps/iiif/manifests/documents.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Elasticsearch indexing rules for IIIF manifests"""

from html import unescape
from django.conf import settings
from django_elasticsearch_dsl import Document, fields
from django_elasticsearch_dsl.registries import registry
from elasticsearch_dsl import MetaField, Keyword, analyzer
Expand Down Expand Up @@ -106,6 +107,38 @@ def prepare_languages(self, instance):
return [lang.name for lang in instance.languages.all()]
return ["[no language]"]

def prepare_metadata(self, instance):
"""use custom metadata settings to prepare metadata field"""
custom_metadata = {}

if (
settings
and hasattr(settings, "CUSTOM_METADATA")
and isinstance(settings.CUSTOM_METADATA, dict)
):
# should be a dict like {meta_key: {"multi": bool, "separator": str}}
for key, opts in settings.CUSTOM_METADATA.items():
val = None
# each key in CUSTOM_METADATA dict should be a metadata key.
# however, instance.metadata will generally be a list rather than a dict: it's a
# jsonfield that maps to the IIIF manifest metadata field, which is a list
# consisting of dicts like { label: str, value: str }
if isinstance(instance.metadata, list):
# find matching value by "label" == key
for obj in instance.metadata:
if "label" in obj and obj["label"] == key and "value" in obj:
val = obj["value"]
break
elif isinstance(instance.metadata, dict):
# in some cases it may be just a dict, so in that case, use get()
val = instance.metadata.get(key, None)
# should have "multi" bool and if multi is True, "separator" string
if val and opts.get("multi", False) == True:
val = val.split(opts.get("separator", ";"))
custom_metadata[key] = val

return custom_metadata

def prepare_summary(self, instance):
"""Strip HTML tags from summary"""
return unescape(strip_tags(instance.summary))
Expand Down
21 changes: 21 additions & 0 deletions config/settings/local.dst
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,24 @@ LOGGING = {

# Must be configured to the host and port running Elasticsearch
ELASTICSEARCH_DSL['default']['hosts'] = 'user:pass@localhost:9200'

# Custom faceted metadata fields configuration. Must be a dict with key names
# matching exactly the ingest metadata spradsheet column names to be indexed.
# For each column, configure options for whether it should accept multiple
# values in a single record ("multi": True), and if so, what separator will be
# used ("separator": ";", semicolon by default).
# Note that these key names will also correspond to "label" names in the Manifest
# metadata list.
#
# Example:
# CUSTOM_METADATA = {
# "Column Name": {
# "multi": True,
# "separator": ";",
# },
# "Other Column": {
# "multi": False,
# },
# }

CUSTOM_METADATA = {}

0 comments on commit 32ca1a8

Please sign in to comment.