Merge pull request #326 from liangliangyy/dev

修复重复重建es索引bug
sh_branch
且听风吟 6 years ago committed by GitHub
commit 7db717c5ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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%"
)

@ -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 \

@ -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:

@ -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()

@ -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))
Loading…
Cancel
Save