Skip to content

Commit d594c15

Browse files
author
zmrenwu
committed
Step10: 基于 drf-haystack 实现文章搜索接口
1 parent 0a98047 commit d594c15

File tree

7 files changed

+114
-49
lines changed

7 files changed

+114
-49
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ elasticsearch = ">=2,<3"
1717
django-haystack = "*"
1818
djangorestframework = "*"
1919
django-filter = "*"
20+
drf-haystack = "*"
2021

2122
[requires]
2223
python_version = "3"

Pipfile.lock

Lines changed: 65 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ $ git clone https://github.com/HelloGitHub-Team/HelloDjango-REST-framework-tutor
197197
8. [文章详情 API](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/97/)
198198
9. [在接口返回Markdown解析后的内容](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/98/)
199199
10. [实现分类、标签、归档日期接口](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/99/)
200-
11. [评论接口](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/100/)
200+
11. [评论接口](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/100/))
201+
12. [基于 drf-haystack 实现文章搜索接口](https://www.zmrenwu.com/courses/django-rest-framework-tutorial/materials/101/))
201202

202203
## 公众号
203204
<p align="center">

blog/serializers.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
from django.contrib.auth.models import User
22
from rest_framework import serializers
3+
from rest_framework.fields import CharField
4+
5+
from drf_haystack.serializers import (
6+
HaystackSerializer,
7+
HaystackSerializerMixin,
8+
HighlighterMixin,
9+
)
310

411
from .models import Category, Post, Tag
12+
from .search_indexes import PostIndex
13+
from .utils import Highlighter
514

615

716
class CategorySerializer(serializers.ModelSerializer):
@@ -71,3 +80,30 @@ class Meta:
7180
"toc",
7281
"body_html",
7382
]
83+
84+
85+
class HighlightedCharField(CharField):
86+
def to_representation(self, value):
87+
value = super().to_representation(value)
88+
request = self.context["request"]
89+
query = request.query_params["text"]
90+
highlighter = Highlighter(query)
91+
return highlighter.highlight(value)
92+
93+
94+
class PostHaystackSerializer(HaystackSerializerMixin, PostListSerializer):
95+
title = HighlightedCharField()
96+
summary = HighlightedCharField(source="body")
97+
98+
class Meta(PostListSerializer.Meta):
99+
search_fields = ["text"]
100+
fields = [
101+
"id",
102+
"title",
103+
"summary",
104+
"created_time",
105+
"excerpt",
106+
"category",
107+
"author",
108+
"views",
109+
]

blog/views.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
from rest_framework.views import APIView
1212

1313
from comments.serializers import CommentSerializer
14+
from drf_haystack.viewsets import HaystackViewSet
1415
from pure_pagination.mixins import PaginationMixin
1516

1617
from .filters import PostFilter
1718
from .models import Category, Post, Tag
19+
from .search_indexes import PostIndex
1820
from .serializers import (
1921
CategorySerializer,
22+
PostHaystackSerializer,
2023
PostListSerializer,
2124
PostRetrieveSerializer,
2225
TagSerializer,
@@ -149,3 +152,8 @@ class TagViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
149152

150153
def get_queryset(self):
151154
return Tag.objects.all().order_by("name")
155+
156+
157+
class PostSearchView(HaystackViewSet):
158+
index_models = [Post]
159+
serializer_class = PostHaystackSerializer

blogproject/settings/local.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
ALLOWED_HOSTS = ['*']
88

99
# 搜索设置
10-
HAYSTACK_CONNECTIONS['default']['URL'] = 'http://elasticsearch_local:9200/'
10+
HAYSTACK_CONNECTIONS['default']['URL'] = 'http://elasticsearch.local:9200/'

blogproject/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
router.register(r"categories", blog.views.CategoryViewSet, basename="category")
2727
router.register(r"tags", blog.views.TagViewSet, basename="tag")
2828
router.register(r"comments", comments.views.CommentViewSet, basename="comment")
29+
router.register(r"search", blog.views.PostSearchView, basename="search")
2930

3031
urlpatterns = [
3132
path("admin/", admin.site.urls),

0 commit comments

Comments
 (0)