{% if query %}
- -

搜索:{{ query }}

+ {% if suggestion %} +

+ 已显示 “{{ suggestion }}” 的搜索结果。   + 仍然搜索:{{ query }}
+

+ {% else %} +

+ 搜索:{{ query }}    +

+ {% endif %}
{% endif %} {% if query and page.object_list %} From 86029562c5a2ebb5cb593a0a66f7b731dc8c1181 Mon Sep 17 00:00:00 2001 From: ch3nnn Date: Wed, 17 Aug 2022 23:57:05 +0800 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20suggest=5Fsear?= =?UTF-8?q?ch=20=E6=8E=A8=E8=8D=90=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- djangoblog/elasticsearch_backend.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/djangoblog/elasticsearch_backend.py b/djangoblog/elasticsearch_backend.py index eb7fea263..4afe4981f 100644 --- a/djangoblog/elasticsearch_backend.py +++ b/djangoblog/elasticsearch_backend.py @@ -54,19 +54,22 @@ def clear(self, models=None, commit=True): self.remove(None) @staticmethod - def get_suggestion(body: str): - """获取建议 keyword """ + def get_suggestion(query: str) -> str: + """获取推荐词, 如果没有找到添加原搜索词""" + search = ArticleDocument.search() \ - .query("match", body=body) \ - .suggest('suggest_search', body, term={'field': 'body'}) \ + .query("match", body=query) \ + .suggest('suggest_search', query, term={'field': 'body'}) \ .execute() keywords = [] for suggest in search.suggest.suggest_search: if suggest["options"]: keywords.append(suggest["options"][0]["text"]) - - return ' '.join(keywords) if keywords else body + else: + keywords.append(suggest["text"]) + + return ' '.join(keywords) @log_query def search(self, query_string, **kwargs): @@ -75,9 +78,8 @@ def search(self, query_string, **kwargs): start_offset = kwargs.get('start_offset') end_offset = kwargs.get('end_offset') - # 搜索建议 - is_suggest = getattr(self, "is_suggest", None) - if is_suggest is not False: + # 推荐词搜索 + if getattr(self, "is_suggest", None): suggestion = self.get_suggestion(query_string) else: suggestion = query_string @@ -171,10 +173,7 @@ class ElasticSearchModelSearchForm(ModelSearchForm): def search(self): # 是否建议搜索 - self.searchqueryset.query.backend.is_suggest = True - if self.data.get("is_suggest") == "no": - self.searchqueryset.query.backend.is_suggest = False - + self.searchqueryset.query.backend.is_suggest = self.data.get("is_suggest") != "no" sqs = super().search() return sqs From 5aa41ce952e384bfe0884638158f23054fa7ceb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=94=E5=90=AC=E9=A3=8E=E5=90=9F?= Date: Thu, 18 Aug 2022 14:27:02 +0800 Subject: [PATCH 3/4] Update requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 7bc2c9554..d4e15c513 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ coverage==6.4 bleach==5.0.0 -Django==4.0.7 +Django==4.1 django-compressor==4.0 django-haystack==3.2.1 django-ipware==4.0.2 From 3ec5ab662b1cebe67419a2462cf5901cf0ad9ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=94=E5=90=AC=E9=A3=8E=E5=90=9F?= Date: Thu, 18 Aug 2022 14:29:58 +0800 Subject: [PATCH 4/4] Update requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index d4e15c513..b9672707b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ coverage==6.4 bleach==5.0.0 Django==4.1 -django-compressor==4.0 +django-compressor==4.1 django-haystack==3.2.1 django-ipware==4.0.2 django-mdeditor==0.1.20