Wagtail not indexing body field

Viewed 243

I'm trying to search. It can find words from the 'title' but not from the 'body' text. I have no further settings only defaults.

Any ideas?

class StandardPage(Page):
    body = RichTextField(features=['h2', 'bold', 'italic', 'link', 'ol', 'ul', 'document-link', 'image', 'embed'])
    sidebar_image = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )


    # Search index configuration

    search_fields = Page.search_fields + [ # Inherit search_fields from Page
        index.SearchField('body'),
    ]

and this is my view.py

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()

    return render(request, 'search/search.html', {
        'search_query': search_query,
        'search_results': search_results,
    })
1 Answers

This is a limitation of the database-based search backend enabled by default. As per the Wagtail search backend docs:

The database backend is very basic and is intended only to be used in development and on small sites. It cannot order results by relevance, severely hampering its usefulness when searching a large collection of pages.

It also doesn’t support: Searching on fields in subclasses of Page (unless the class is being searched directly)

The body field on StandardPage is one such field - it will not be searched by Page.objects.live().search(search_query), only StandardPage.objects.live().search(search_query). For a general search interface that works across all page types and searches the full content of each one, you'll need to switch to one of the other search backends.

Related