2

I want to be able to search in one of my custom page models. The search should exclude all predefined page.id which are in a list, and also exclude where the page.url_path contains a certain value.

For example:

from django.db.models import Q

# The view
def search(request, search_query):
    ...
    CustomPageModel.objects.live().exclude(
        Q(id__in=list_page_ids) | 
        Q(url_path__icontains='notsearchable')
    ).search(search_query))

This works when I use:

WAGTAILSEARCH_BACKENDS = {
    'default': {
        'BACKEND': 'wagtail.contrib.postgres_search.backend',
    }
}

But does not work when I use:

WAGTAILSEARCH_BACKENDS = {
    'default': {
        'BACKEND': 'wagtail.search.backends.elasticsearch6',
        ...
    }
}

The error I get from Elasticsearch as backend:

FilterError at /search/query/
Could not apply filter on search results: "url_path__icontains = notsearchable". Lookup "icontains"" not recognised.

Elasticsearch does not support icontains.

So how can I exclude and search on the same model?

ps. The CustomModel is used in different url_paths, so there should not be any changes to the model itself.

Igoranze
  • 1,506
  • 13
  • 35

0 Answers0