wagtailwagtail-search

Wagtail - Search Page Owner Full Name


I am setting up a blog style documentation site. I was using a user input field for author when a child page was created. I found out that Wagtail houses owner in the page model. In the interest of not duplicating data, I removed my author field so I can use the default wagtail field. However, I have set up an LDAP module for authentication so the owner is housed as an Employee ID and not a user name. This Employee ID does map to a full name though and I am able to access that on a template via the owner.get_full_name.

So the question is, how do I set up the default search to check the owner full name when performing searches? How to get this into the search index? I am still a bit new to Wagtail so this may be a case of creating an author field with a foreign key mapping back to the user table or should I be modifying the search view to include a run through the user table?

def search(request):
search_query = request.GET.get('query', None)
page = request.GET.get('page', 1)

# Search
if search_query:
    search_results = Page.objects.live().search(search_query)
    query = Query.get(search_query)

    # Record hit
    query.add_hit()
else:
    search_results = Page.objects.none()

# Pagination
paginator = Paginator(search_results, 10)
try:
    search_results = paginator.page(page)
except PageNotAnInteger:
    search_results = paginator.page(1)
except EmptyPage:
    search_results = paginator.page(paginator.num_pages)

return TemplateResponse(request, 'search/search.html', {
    'search_query': search_query,
    'search_results': search_results,
})

Solution

  • If you check the Wagtail search documentation, it describes the process for indexing callables and extra attributes:

    https://docs.wagtail.io/en/stable/topics/search/indexing.html#indexing-callables-and-other-attributes

    So what I would do is:

    1. Create a get_owner_full_name() method in your page class
    2. Add an index.SearchField('get_owner_full_name') to the search_fields

    One note though, this will only work if you are using either the PostgreSQL backend, or the Elasticsearch backend. The default database backend does not support the indexing of extra fields.