Skip to content

Commit

Permalink
添加全文搜索功能
Browse files Browse the repository at this point in the history
  • Loading branch information
cjb0721 committed Apr 26, 2019
1 parent 7592145 commit edd93d9
Show file tree
Hide file tree
Showing 13 changed files with 980 additions and 9 deletions.
21 changes: 21 additions & 0 deletions Blog/Blog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
'django.contrib.staticfiles',
'MyBlog',
'comment',
# 1、添加应用
'haystack',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -123,3 +125,22 @@

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

# 2、添加搜索引擎
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'MyBlog.whoosh_cn_backend.WhooshEngine',
'PATH': os.path.join(BASE_DIR, 'whoosh_index'),
}
}

# 3、分页设置
HAYSTACK_SEARCH_RESULTS_PER_PAGE = 10

# 4、索引生成设置
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'





2 changes: 2 additions & 0 deletions Blog/Blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from haystack.views import SearchView

urlpatterns = [
path('admin/', admin.site.urls),
url('MyBlog/', include('MyBlog.urls', namespace='MyBlog')),
url('MyBlog/', include('comment.urls', namespace='comment')),
url('search/', SearchView(), name='search'),
]
16 changes: 16 additions & 0 deletions Blog/MyBlog/search_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
6、创建索引类
"""

from haystack import indexes
from .models import Article


class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)

def get_model(self):
return Article

def index_queryset(self, using=None):
return self.get_model().objects.all()
Loading

0 comments on commit edd93d9

Please sign in to comment.