new data model: fix reindex
This commit is contained in:
parent
8aa78cf297
commit
274fe591a1
7 changed files with 62 additions and 14 deletions
|
@ -2,6 +2,13 @@ from django.core.management.base import BaseCommand
|
|||
from django.conf import settings
|
||||
from catalog.models import *
|
||||
import pprint
|
||||
from django.core.paginator import Paginator
|
||||
from tqdm import tqdm
|
||||
from time import sleep
|
||||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
|
||||
BATCH_SIZE = 1000
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
|
@ -13,26 +20,59 @@ class Command(BaseCommand):
|
|||
help="initialize index",
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--update",
|
||||
help="update index schema",
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--stat",
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--reindex",
|
||||
action="store_true",
|
||||
)
|
||||
|
||||
def init_index(self):
|
||||
self.stdout.write(f"Connecting to search server")
|
||||
Indexer.init()
|
||||
self.stdout.write(self.style.SUCCESS("Index created."))
|
||||
|
||||
def update_index(self):
|
||||
Indexer.update_settings()
|
||||
self.stdout.write(self.style.SUCCESS("Index updated."))
|
||||
|
||||
def stat(self, *args, **options):
|
||||
self.stdout.write(f"Connecting to search server")
|
||||
stats = Indexer.get_stats()
|
||||
pprint.pp(stats)
|
||||
|
||||
def reindex(self):
|
||||
if Indexer.busy():
|
||||
self.stdout.write("Please wait for previous updates")
|
||||
# Indexer.update_settings()
|
||||
# self.stdout.write(self.style.SUCCESS('Index settings updated.'))
|
||||
qs = (
|
||||
Item.objects.all()
|
||||
) # if h == 0 else c.objects.filter(edited_time__gt=timezone.now() - timedelta(hours=h))
|
||||
pg = Paginator(qs.order_by("id"), BATCH_SIZE)
|
||||
for p in tqdm(pg.page_range):
|
||||
items = list(
|
||||
map(lambda o: Indexer.obj_to_dict(o), pg.get_page(p).object_list)
|
||||
)
|
||||
if items:
|
||||
Indexer.replace_batch(items)
|
||||
while Indexer.busy():
|
||||
sleep(0.5)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
if options["init"]:
|
||||
self.init_index()
|
||||
elif options["update"]:
|
||||
self.update_index()
|
||||
elif options["stat"]:
|
||||
self.stat()
|
||||
elif options["reindex"]:
|
||||
self.reindex()
|
||||
# else:
|
||||
|
||||
# try:
|
||||
|
|
|
@ -75,8 +75,7 @@ class Indexer:
|
|||
return cls._instance
|
||||
|
||||
@classmethod
|
||||
def init(cls):
|
||||
# cls.instance().collections[INDEX_NAME].delete()
|
||||
def config(cls):
|
||||
# fields = [
|
||||
# {"name": "_class", "type": "string", "facet": True},
|
||||
# {"name": "source_site", "type": "string", "facet": True},
|
||||
|
@ -122,14 +121,20 @@ class Indexer:
|
|||
{"name": "tags", "optional": True, "locale": "zh", "type": "string[]"},
|
||||
{"name": ".*", "optional": True, "locale": "zh", "type": "auto"},
|
||||
]
|
||||
return {
|
||||
"name": INDEX_NAME,
|
||||
"fields": fields,
|
||||
# "default_sorting_field": "rating_count",
|
||||
}
|
||||
|
||||
cls.instance().collections.create({"name": INDEX_NAME, "fields": fields})
|
||||
@classmethod
|
||||
def init(cls):
|
||||
# cls.instance().collections[INDEX_NAME].delete()
|
||||
cls.instance().collections.create(cls.config())
|
||||
|
||||
@classmethod
|
||||
def update_settings(cls):
|
||||
# https://github.com/typesense/typesense/issues/96
|
||||
# FIXME
|
||||
pass
|
||||
cls.instance().collections[INDEX_NAME].update(cls.config())
|
||||
|
||||
@classmethod
|
||||
def get_stats(cls):
|
||||
|
@ -227,6 +232,7 @@ class Indexer:
|
|||
"per_page": SEARCH_PAGE_SIZE,
|
||||
"query_by": ",".join(SEARCHABLE_ATTRIBUTES),
|
||||
"filter_by": filters,
|
||||
"sort_by": "_text_match:desc,rating_count:desc"
|
||||
# 'facetsDistribution': ['_class'],
|
||||
# 'sort_by': None,
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
<div class="entity-detail__rating">
|
||||
{% if item.rating and item.rating_count >= 5 %}
|
||||
<span class="entity-detail__rating-star rating-star" data-rating-score="{{ item.rating | floatformat:"0" }}"></span>
|
||||
<span class="entity-detail__rating-score"> {{ item.rating }} </span>
|
||||
<span class="entity-detail__rating-score"> {{ item.rating | floatformat:1 }} </span>
|
||||
<small>({{ item.rating_count }}人评分)</small>
|
||||
{% else %}
|
||||
<span> {% trans '评分:评分人数不足' %}</span>
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
<div class="entity-detail__rating">
|
||||
{% if item.rating and item.rating_count >= 5 %}
|
||||
<span class="entity-detail__rating-star rating-star" data-rating-score="{{ item.rating | floatformat:"0" }}"></span>
|
||||
<span class="entity-detail__rating-score"> {{ item.rating }} </span>
|
||||
<span class="entity-detail__rating-score"> {{ item.rating | floatformat:1 }} </span>
|
||||
<small>({{ item.rating_count }}人评分)</small>
|
||||
{% else %}
|
||||
<span> {% trans '评分:评分人数不足' %}</span>
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
|
||||
<div>{% if item.pub_house %}{% trans '出版社:' %}{{ item.pub_house }}{% endif %}</div>
|
||||
{% if item.rating %}
|
||||
{% trans '评分: ' %}<span class="entity-card__rating-star rating-star" data-rating-score="{{ item.rating }}"></span>
|
||||
{% trans '评分: ' %}<span class="entity-card__rating-star rating-star" data-rating-score="{{ item.rating | floatformat:0 }}"></span>
|
||||
<span class="entity-card__rating-score rating-score">{{ item.rating | floatformat:1 }}</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
|
|
@ -267,5 +267,6 @@ def search(request):
|
|||
PAGE_LINK_NUMBER, page_number, result.num_pages
|
||||
),
|
||||
"categories": ["book", "movie", "music", "game"],
|
||||
"hide_category": category is not None,
|
||||
},
|
||||
)
|
||||
|
|
|
@ -36,15 +36,16 @@
|
|||
{% else %}
|
||||
{{ item.title }}
|
||||
{% endif %}
|
||||
{% if item.year %}<small style="font-weight: lighter">({{ item.year }})</small>{% endif %}
|
||||
</a>
|
||||
{% if not request.GET.c and not hide_category %}
|
||||
<span class="entity-list__entity-category">[{{item.category.label}}]</span>
|
||||
{% endif %}
|
||||
{% for res in item.external_resources.all %}
|
||||
<a href="{{ res.url }}">
|
||||
<span class="source-label source-label__{{ res.site_name }}">{{ res.site_name.label }}</span>
|
||||
</a>
|
||||
{% endfor %}
|
||||
{% if not hide_category %}
|
||||
<span class="entity-list__entity-category">[{{item.category.label}}]</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
{% if item.rating %}
|
||||
|
|
Loading…
Add table
Reference in a new issue