Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django pass Haystack highlighter result to a view

I am using django-haystack and I am trying to implement a way to append the page number to a pdf link in order to open it in the specific page. My goal is to open the pdf in the page where the first hit is found. I know the position of the hit in my document and the position where the page changes. For example i know that the first hit starts at character 2067 and the second page changes at character 3000, so i have to open the pdf at the second page.

My question is: how can i get the result of the function that finds the number of the page where the pdf should open and render it ?

I am thinking that the result should be something like that <a href="{% static 'img/sample.pdf#page={{ pageNumber }}' %}"> but i am open to any other suggestions.

P.S. I am not asking you to solve my problem. I am asking for suggestions or a constructive discussion as i am new to django.

Thank you in advance

EDIT

So after researching for a bit i did the following. I found that the highlighter class has a function that finds the position of the hits. I added a getter to that class in order to get the positions (I will change it later. For now i want to see if it is working the way i think it should). Then in my views.py file i added the following

from django.shortcuts import render
from haystack.utils.highlighting import Highlighter


def getPage(request):
        pos = Highlighter.getPos()
        print (pos)
        return render(request, 'search/_result_object.html', {'pos': pos})

and in my html i added this

<ul>
    {% for element in pos %}
        <li>{{ element }}</li>
    {% endfor %}
</ul>

just to print the position and see that everything works ok. But the list is empty, meaning that i get no results. Maybe something is not working the way i think it should. Any ideas?

Edit #2

I think that it is impossible to get the positions from the highlighter since I don't have the actual Highlighter object in order to get the positions using a getter.

Is there any other way to pass arguments between highlighter and a view? I managed to get the query term in my view but i don't have the text block where the query term was found nor the full text to search again for the position. In addition, i think that this approach would be slow when the program scales up.

like image 980
mnmbs Avatar asked Mar 15 '19 11:03

mnmbs


1 Answers

I found a solution for my problem and i post it here for anyone interested in the future.

So, i ended up writing a new template tag called findpage using the highlight tag as reference. Everything needed to create a custom tag can be found here. That way i can call it like this {% findpage obj.content with query %} and pass the query as an argument so as to find the position of the query hits in the text block. Given the page breaks and the position of the query hit in the document i can now find the exact page that i should open the pdf. The link now looks like this <a href= "{% static "img/sample.pdf#page=" %}{% findpage obj.content with query %}" >.

I hope that this will be useful for someone in the future. I will try to answer any question that may come up.

Thank everyone for their suggestions.

like image 97
mnmbs Avatar answered Nov 07 '22 05:11

mnmbs