Skip to content

Commit

Permalink
Index user annotations in elasticsearch (#956)
Browse files Browse the repository at this point in the history
  • Loading branch information
blms committed Jan 31, 2024
1 parent c00237c commit f97a4e6
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions apps/readux/documents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Elasticsearch indexing rules for UserAnnotations"""

from html import unescape
from django_elasticsearch_dsl import Document, fields
from django_elasticsearch_dsl.registries import registry
from django.utils.html import strip_tags

from apps.readux.models import UserAnnotation
from apps.iiif.manifests.documents import stemmer

@registry.register_document
class UserAnnotationDocument(Document):
"""Elasticsearch Document class for Readux UserAnnotation"""

# fields to map explicitly in Elasticsearch
canvas_index = fields.IntegerField()
canvas_pid = fields.KeywordField()
content = fields.TextField(analyzer=stemmer)
manifest_pid = fields.KeywordField()
owner_username = fields.KeywordField()
pid = fields.KeywordField()

class Index:
"""Settings for Elasticsearch"""

name = "annotations"

class Django:
"""Settings for automatically pulling data from Django"""

model = UserAnnotation

def prepare_content(self, instance):
"""Strip HTML tags from content"""
return unescape(strip_tags(instance.content))

def prepare_canvas_index(self, instance):
return instance.canvas.position

def prepare_canvas_pid(self, instance):
return instance.canvas.pid

def prepare_manifest_pid(self, instance):
return instance.canvas.manifest.pid

def prepare_owner_username(self, instance):
return instance.owner.username

0 comments on commit f97a4e6

Please sign in to comment.