Skip to content

Commit

Permalink
Merge pull request #7 from brylie/add-vector-search
Browse files Browse the repository at this point in the history
Add vector search
  • Loading branch information
brylie authored Jul 29, 2024
2 parents 121dbac + 6eaf18f commit db4556e
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 107 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,5 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

.DS_Store
2 changes: 2 additions & 0 deletions app/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class BlogIndexPage(Page):

content_panels = Page.content_panels + [FieldPanel("intro")]

max_count = 1

def get_context(self, request):
context = super().get_context(request)
blogpages = self.get_children().live().order_by("-first_published_at")
Expand Down
12 changes: 12 additions & 0 deletions app/blog/templatetags/blog_tags.py
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
3 changes: 3 additions & 0 deletions app/core/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@

<body class="{% block body_class %}{% endblock %}">
{% wagtailuserbar %}

{% include "heading.html" %}

<div class="container">
{% block content %}{% endblock %}
</div>
Expand Down
29 changes: 29 additions & 0 deletions app/core/templates/heading.html
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>
5 changes: 1 addition & 4 deletions app/home/templates/home/home_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

{% block extra_css %}

{% comment %}
Delete the line below if you're just getting started and want to remove the welcome screen!
{% endcomment %}
<link rel="stylesheet" href="{% static 'css/welcome_page.css' %}">

{% endblock extra_css %}

{% block content %}
Expand Down
52 changes: 0 additions & 52 deletions app/home/templates/home/welcome_page.html

This file was deleted.

23 changes: 23 additions & 0 deletions app/search/templates/search/pagination.html
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 %}
80 changes: 51 additions & 29 deletions app/search/templates/search/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,55 @@
{% block title %}Search{% endblock %}

{% block content %}
<h1>Search</h1>
<div class="container mt-4">
<h1>Search</h1>

<form action="{% url 'search' %}" method="get" class="mb-4">
<div class="input-group">
<input type="text" class="form-control" name="query" value="{{ search_query }}" placeholder="Search...">
<button class="btn btn-primary" type="submit">Search</button>
</div>
</form>

<form action="{% url 'search' %}" method="get">
<input type="text" name="query"{% if search_query %} value="{{ search_query }}"{% endif %}>
<input type="submit" value="Search" class="button">
</form>

{% if search_results %}
<ul>
{% for result in search_results %}
<li>
<h4><a href="{% pageurl result %}">{{ result }}</a></h4>
{% if result.search_description %}
{{ result.search_description }}
{% endif %}
</li>
{% endfor %}
</ul>

{% if search_results.has_previous %}
<a href="{% url 'search' %}?query={{ search_query|urlencode }}&amp;page={{ search_results.previous_page_number }}">Previous</a>
{% endif %}

{% if search_results.has_next %}
<a href="{% url 'search' %}?query={{ search_query|urlencode }}&amp;page={{ search_results.next_page_number }}">Next</a>
{% endif %}
{% elif search_query %}
No results found
{% endif %}
{% endblock %}
{% if search_query %}
<div class="row">
<div class="col-md-6">
<h2>Default Search Results</h2>
{% if default_results %}
<ul class="list-unstyled">
{% for result in default_results %}
<li class="mb-3">
<h3 class="h4"><a href="{% pageurl result %}">{{ result }}</a></h3 class="h4">
{% if result.search_description %}
<p>{{ result.search_description }}</p>
{% endif %}
</li>
{% endfor %}
</ul>
{% include "search/pagination.html" with results=default_results %}
{% else %}
<p>No results found</p>
{% endif %}
</div>
<div class="col-md-6">
<h2>Vector Search Results</h2>
{% if vector_results %}
<ul class="list-unstyled">
{% for result in vector_results %}
<li class="mb-3">
<h3 class="h4"><a href="{% pageurl result %}">{{ result }}</a></h3 class="h4">
{% if result.search_description %}
<p>{{ result.search_description }}</p>
{% endif %}
</li>
{% endfor %}
</ul>
{% include "search/pagination.html" with results=vector_results %}
{% else %}
<p>No results found</p>
{% endif %}
</div>
</div>
{% endif %}
</div>
{% endblock %}
49 changes: 27 additions & 22 deletions app/search/views.py
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,
},
)

0 comments on commit db4556e

Please sign in to comment.