diff --git a/common/index.py b/common/index.py
index f9d4bfea..c2389db2 100644
--- a/common/index.py
+++ b/common/index.py
@@ -153,11 +153,15 @@ class Indexer:
# print(r)
import types
results = types.SimpleNamespace()
- results.items = list(map(lambda i: self.item_to_obj(i), r['hits']))
+ results.items = list([x for x in map(lambda i: self.item_to_obj(i), r['hits']) if x is not None])
results.num_pages = (r['nbHits'] + SEARCH_PAGE_SIZE - 1) // SEARCH_PAGE_SIZE
# print(results)
return results
@classmethod
def item_to_obj(self, item):
- return self.class_map[item['_class']].objects.get(id=item['id'])
+ try:
+ return self.class_map[item['_class']].objects.get(id=item['id'])
+ except Exception as e:
+ logger.error(f"unable to load search result item from db:\n{item}")
+ return None
diff --git a/common/templates/common/search_result.html b/common/templates/common/search_result.html
index 2d9e74f2..096adf00 100644
--- a/common/templates/common/search_result.html
+++ b/common/templates/common/search_result.html
@@ -43,9 +43,7 @@
{% for item in items %}
-
{% include "partial/list_item.html" %}
-
{% empty %}
-
{% trans '无站内条目匹配' %}
diff --git a/common/templates/partial/list_item_movie.html b/common/templates/partial/list_item_movie.html
index e947ed30..35b2f667 100644
--- a/common/templates/partial/list_item_movie.html
+++ b/common/templates/partial/list_item_movie.html
@@ -66,16 +66,12 @@
{% if movie.director %}{% trans '导演' %}:
{% for director in movie.director %}
-<<<<<<< Updated upstream
- {{ director }}{% if not forloop.last %} {% endif %}
-=======
{% if request.GET.q %}
{{ director | highlight:request.GET.q }}
{% else %}
{{ director }}
{% endif %}
{% if not forloop.last %},{% endif %}
->>>>>>> Stashed changes
{% endfor %}/
{% endif %}
@@ -89,7 +85,13 @@
{% if movie.actor %}{% trans '主演' %}:
{% for actor in movie.actor %}
- 5 %}style="display: none;" {% endif %}>{{ actor }}
+ 5 %}style="display: none;" {% endif %}>
+ {% if request.GET.q %}
+ {{ actor | highlight:request.GET.q }}
+ {% else %}
+ {{ actor }}
+ {% endif %}
+
{% if forloop.counter <= 5 %}
{% if not forloop.counter == 5 and not forloop.last %} {% endif %}
{% endif %}
diff --git a/common/templatetags/highlight.py b/common/templatetags/highlight.py
index 042c40da..02d79bbb 100644
--- a/common/templatetags/highlight.py
+++ b/common/templatetags/highlight.py
@@ -1,13 +1,19 @@
from django import template
from django.utils.safestring import mark_safe
from django.template.defaultfilters import stringfilter
-from django.utils.html import format_html
+from opencc import OpenCC
-import re
+cc = OpenCC('t2s')
register = template.Library()
+
@register.filter
@stringfilter
def highlight(text, search):
- return mark_safe(text.replace(search, f'{search}')) # TODO better query words match
+ for s in cc.convert(search.strip().lower()).split(' '):
+ if s:
+ p = cc.convert(text.lower()).find(s)
+ if p != -1:
+ text = f'{text[0:p]}{text[p:p+len(s)]}{text[p+len(s):]}'
+ return mark_safe(text)
diff --git a/requirements.txt b/requirements.txt
index f9a5bc4d..3cb02071 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -14,3 +14,4 @@ requests
filetype
setproctitle
tqdm
+opencc