Django pass Haystack highlighter result to a view
Asked Answered
E

1

6

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.

Epicene answered 15/3, 2019 at 11:1 Comment(8)
What you have tried please show usAmplify
The first part of your question (how to get the page number) sounds trivial since you already say you know which page comes next. The second part, how to link to a specific page, is indeed what you suggest, according to adobe. So just go ahead, write the code and ask a more specific question if you hit an issue.Secern
@Secern the first part wasn't really a question. I was just giving as much context as i could for you to better understand what i want to do. Following your suggestion i edited my post with what i tried.Epicene
print(pos) in your view prints nothing?Secern
I put that for debug reasons but i dont get anything printerEpicene
I don't understand the line pos = Highlighter.getPos(). Is that the exact code you are using? I would expect that to crash since Highlighter doesn't have an attribute getPos. So show us the code you use to get pos.Secern
I added a getter function that returns the position of the first hit.Epicene
It's nice that you realized that you should use an instance of the Highlighter class. Then please show us your code of getting that instance/object. Maybe you should first go through some examples.Gibbsite
E
2

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.

Epicene answered 19/3, 2019 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.