I am new to Wagtail/Django. Getting start was quite easy and I got my application up and running quite easily. However I ran into a problem when I was searcing. The application is quite small and I would like to use the backend db (or MSSQL). I have search all around but suprisingly not managed to find solution. Hope to get one her :-)
I have a page with inline elements. Simplified below
class BlockPage(Page):
content = RichTextField()
content_panels = Page.content_panels + [
FieldPanel('content'),
InlinePanel('rel_page_blocks', label="Markup blocks"),
]
search_fields = [
index.SearchField('content', partial_match=True),
index.RelatedFields('rel_page_blocks', [
index.SearchField('block_content', partial_match=True),
]),
index.FilterField('live'),
]
The inline panel is defined like this
class Blocks(index.Indexed, Orderable):
page = ParentalKey(BlockPage, related_name='rel_page_blocks')
block_content = RichTextField()
panels = [
FieldPanel('block_content'),
]
So far so good. Now I would like to search the page with its inline Blocks. I have successfully searched the page but the Blocks are not searched. I know that this issue is solved using Elastic search but it is not an option for me.
Her is the views.py
def search(request):
search_query = request.GET.get('query', None)
page = request.GET.get('page', 1)
# Search
if search_query:
blocks_results = BlockPage.objects.live().search(search_query)
blocks_result_ids = [p.page_ptr.id for p in blocks_results]
# I get a result using this but I would like it to be attached to the
# parent so when viewing the result I can refere/show the parent BlockPage
# The question is how to attach "r" to the parent result?
r = get_search_backend().search(search_query, Blocks)
page_ids = blocks_result_ids + **other page ids**
search_results = Page.objects.filter(id__in=page_ids)
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,
})
Any hint would be greatly appreciated. I am a only a couple of days old here so please bear that in mind if you have a solution.