-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from brylie/add-vector-search
Add vector search
- Loading branch information
Showing
10 changed files
with
150 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django import template | ||
from blog.models import BlogIndexPage | ||
|
||
register = template.Library() | ||
|
||
|
||
@register.simple_tag | ||
def blogindex_url(): | ||
try: | ||
return BlogIndexPage.objects.live().first().url | ||
except AttributeError: | ||
return None # Return None if BlogIndexPage doesn't exist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{% load wagtailcore_tags %} | ||
{% load blog_tags %} | ||
|
||
<nav class="navbar navbar-expand-lg"> | ||
<div class="container-fluid"> | ||
<a class="navbar-brand" href="/">Wagtail Vector Blog</a> | ||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> | ||
<span class="navbar-toggler-icon"></span> | ||
</button> | ||
<div class="collapse navbar-collapse" id="navbarSupportedContent"> | ||
<ul class="navbar-nav me-auto mb-2 mb-lg-0"> | ||
<li class="nav-item"> | ||
<a class="nav-link active" aria-current="page" href="/">Home</a> | ||
</li> | ||
{% blogindex_url as blog_url %} | ||
{% if blog_url %} | ||
<li class="nav-item"> | ||
<a class="nav-link" href="{{ blog_url }}">Blog</a> | ||
</li> | ||
{% endif %} | ||
<!-- Add more navigation items as needed --> | ||
</ul> | ||
<form class="d-flex" action="{% url 'search' %}" method="get"> | ||
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search" name="query"> | ||
<button class="btn btn-outline-success" type="submit">Search</button> | ||
</form> | ||
</div> | ||
</div> | ||
</nav> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{% if results.has_previous or results.has_next %} | ||
<nav aria-label="Search result pages"> | ||
<ul class="pagination"> | ||
{% if results.has_previous %} | ||
<li class="page-item"> | ||
<a class="page-link" href="{% url 'search' %}?query={{ search_query|urlencode }}&page={{ results.previous_page_number }}">Previous</a> | ||
</li> | ||
{% endif %} | ||
|
||
<li class="page-item active"> | ||
<span class="page-link"> | ||
Page {{ results.number }} of {{ results.paginator.num_pages }} | ||
</span> | ||
</li> | ||
|
||
{% if results.has_next %} | ||
<li class="page-item"> | ||
<a class="page-link" href="{% url 'search' %}?query={{ search_query|urlencode }}&page={{ results.next_page_number }}">Next</a> | ||
</li> | ||
{% endif %} | ||
</ul> | ||
</nav> | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,51 @@ | ||
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator | ||
from django.template.response import TemplateResponse | ||
|
||
from wagtail.models import Page | ||
|
||
# To enable logging of search queries for use with the "Promoted search results" module | ||
# <https://docs.wagtail.org/en/stable/reference/contrib/searchpromotions.html> | ||
# uncomment the following line and the lines indicated in the search function | ||
# (after adding wagtail.contrib.search_promotions to INSTALLED_APPS): | ||
|
||
# from wagtail.contrib.search_promotions.models import Query | ||
from blog.models import BlogPage | ||
|
||
|
||
def search(request): | ||
search_query = request.GET.get("query", None) | ||
page = request.GET.get("page", 1) | ||
|
||
# Search | ||
# Default Search | ||
if search_query: | ||
search_results = Page.objects.live().search(search_query) | ||
|
||
# To log this query for use with the "Promoted search results" module: | ||
|
||
# query = Query.get(search_query) | ||
# query.add_hit() | ||
default_results = Page.objects.live().search(search_query) | ||
else: | ||
default_results = Page.objects.none() | ||
|
||
# Vector Search | ||
if search_query: | ||
vector_results = BlogPage.vector_index.search( | ||
search_query, | ||
) | ||
else: | ||
search_results = Page.objects.none() | ||
vector_results = BlogPage.objects.none() | ||
|
||
# Pagination for default results | ||
default_paginator = Paginator(default_results, 5) | ||
try: | ||
default_results = default_paginator.page(page) | ||
except PageNotAnInteger: | ||
default_results = default_paginator.page(1) | ||
except EmptyPage: | ||
default_results = default_paginator.page(default_paginator.num_pages) | ||
|
||
# Pagination | ||
paginator = Paginator(search_results, 10) | ||
# Pagination for vector results | ||
vector_paginator = Paginator(vector_results, 5) | ||
try: | ||
search_results = paginator.page(page) | ||
vector_results = vector_paginator.page(page) | ||
except PageNotAnInteger: | ||
search_results = paginator.page(1) | ||
vector_results = vector_paginator.page(1) | ||
except EmptyPage: | ||
search_results = paginator.page(paginator.num_pages) | ||
vector_results = vector_paginator.page(vector_paginator.num_pages) | ||
|
||
return TemplateResponse( | ||
request, | ||
"search/search.html", | ||
{ | ||
"search_query": search_query, | ||
"search_results": search_results, | ||
"default_results": default_results, | ||
"vector_results": vector_results, | ||
}, | ||
) |