diff --git a/DjangoBlog/elasticsearch_backend.py b/DjangoBlog/elasticsearch_backend.py index c5ca846..e896653 100644 --- a/DjangoBlog/elasticsearch_backend.py +++ b/DjangoBlog/elasticsearch_backend.py @@ -31,10 +31,10 @@ class ElasticSearchBackend(BaseSearchBackend): def __init__(self, connection_alias, **connection_options): super(ElasticSearchBackend, self).__init__(connection_alias, **connection_options) self.manager = ArticleDocumentManager() - try: - self._rebuild(None) - except: - pass + # try: + # self._rebuild(None) + # except: + # pass def _get_models(self, iterable): models = iterable if iterable else Article.objects.all() @@ -76,7 +76,7 @@ class ElasticSearchBackend(BaseSearchBackend): end_offset = kwargs.get('end_offset') q = Q('bool', - must=[Q('match', body=query_string)], + should=[Q('match', body=query_string), Q('match', title=query_string)], minimum_should_match="70%" ) diff --git a/bin/docker_start.sh b/bin/docker_start.sh index 8139676..c6c3249 100644 --- a/bin/docker_start.sh +++ b/bin/docker_start.sh @@ -20,6 +20,7 @@ python manage.py makemigrations python manage.py migrate python manage.py collectstatic --noinput python manage.py compress --force +python manage.py build_index # Start your Django Unicorn # Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon) exec gunicorn ${DJANGO_WSGI_MODULE}:application \ diff --git a/blog/documents.py b/blog/documents.py index d282059..a8c4180 100644 --- a/blog/documents.py +++ b/blog/documents.py @@ -46,9 +46,9 @@ class ElaspedTimeDocumentManager(): @staticmethod def create(url, time_taken, log_datetime, type, useragent): - if not hasattr(ElaspedTimeDocumentManager, 'mapping_created'): - ElapsedTimeDocument.init() - setattr(ElaspedTimeDocumentManager, 'mapping_created', True) + # if not hasattr(ElaspedTimeDocumentManager, 'mapping_created'): + # ElapsedTimeDocument.init() + # setattr(ElaspedTimeDocumentManager, 'mapping_created', True) doc = ElapsedTimeDocument(meta={'id': int(round(time.time() * 1000))}, url=url, time_taken=time_taken, log_datetime=log_datetime, type=type, useragent=useragent) doc.save() @@ -91,8 +91,8 @@ class ArticleDocument(Document): class ArticleDocumentManager(): def __init__(self): - - ArticleDocument.init() + pass + # ArticleDocument.init() def create_index(self): ArticleDocument.init() @@ -122,6 +122,7 @@ class ArticleDocumentManager(): ) for article in articles] def rebuild(self, articles=None): + ArticleDocument.init() articles = articles if articles else Article.objects.all() docs = self.convert_to_doc(articles) for doc in docs: diff --git a/blog/management/commands/build_index.py b/blog/management/commands/build_index.py index 25dcd9c..f97a1e0 100644 --- a/blog/management/commands/build_index.py +++ b/blog/management/commands/build_index.py @@ -11,7 +11,7 @@ @time: 2019-04-20 20:39 """ -from blog.documents import ArticleDocument, ArticleDocumentManager +from blog.documents import ElapsedTimeDocument, ArticleDocumentManager from django.core.management.base import BaseCommand from blog.models import Article @@ -25,3 +25,6 @@ class Command(BaseCommand): manager = ArticleDocumentManager() manager.delete_index() manager.rebuild() + + manager = ElapsedTimeDocument() + manager.init() diff --git a/blog/management/commands/build_search_words.py b/blog/management/commands/build_search_words.py new file mode 100644 index 0000000..976b2a9 --- /dev/null +++ b/blog/management/commands/build_search_words.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +# encoding: utf-8 +""" +@version: ?? +@author: liangliangyy +@license: MIT Licence +@contact: liangliangyy@gmail.com +@site: https://www.lylinux.net/ +@software: PyCharm +@file: build_search_words.py +@time: 2019/9/23 6:58 下午 +""" + +from django.core.management.base import BaseCommand +from blog.models import Article, Tag, Category + + +# TODO 参数化 +class Command(BaseCommand): + help = 'build search words' + + def handle(self, *args, **options): + datas = set([t.name for t in Tag.objects.all()] + [t.name for t in Category.objects.all()]) + print('\n'.join(datas))