Skip to content

Commit

Permalink
Use Keyword as default for strings instead of Text (#960)
Browse files Browse the repository at this point in the history
and move all the existing string fields to explicitly defined TextFields
  • Loading branch information
blms committed Dec 19, 2023
1 parent 239c173 commit 5a7dc6a
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions apps/iiif/manifests/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from html import unescape
from django_elasticsearch_dsl import Document, fields
from django_elasticsearch_dsl.registries import registry
from elasticsearch_dsl import analyzer
from elasticsearch_dsl import MetaField, Keyword, analyzer
from django.db.models.query import Prefetch
from django.utils.html import strip_tags
from unidecode import unidecode
Expand All @@ -26,6 +26,7 @@ class ManifestDocument(Document):
"""Elasticsearch Document class for IIIF Manifest"""

# fields to map explicitly in Elasticsearch
attribution = fields.TextField()
authors = fields.KeywordField(multi=True) # only used for faceting/filtering
author = fields.TextField() # only used for searching
canvas_set = fields.NestedField(
Expand All @@ -42,6 +43,10 @@ class ManifestDocument(Document):
label = fields.TextField(analyzer=stemmer)
label_alphabetical = fields.KeywordField()
languages = fields.KeywordField(multi=True)
license = fields.TextField()
metadata = fields.NestedField()
published_city = fields.TextField()
publisher = fields.TextField()
summary = fields.TextField(analyzer=stemmer)

class Index:
Expand All @@ -56,19 +61,28 @@ class Django:

# fields to map dynamically in Elasticsearch
fields = [
"attribution",
"created_at",
"date_sort_ascending",
"date_sort_descending",
"license",
"pid",
"published_city",
"published_date",
"publisher",
"viewingdirection",
]
related_models = [Collection, Canvas]

class Meta:
# make Keyword type default for strings, for custom dynamically-mapped facet fields
dynamic_templates = MetaField(
[
{
"strings": {
"match_mapping_type": "string",
"mapping": Keyword().to_dict(),
}
}
]
)

def prepare_authors(self, instance):
"""convert authors string into list"""
if instance.author:
Expand Down

0 comments on commit 5a7dc6a

Please sign in to comment.