I am trying to output the highlighted search-term from a Solr search. I'm using Django 1.8.4 with Scorched.
I have already activated highlighting (resp = ('highlighting', self.solr_response.highlighting))
and to the search-view, and my json output from a search is:
"highlighting": {
"f0109b89-4882-44cc-90b2-6a51561d14ee": { }, <!-- nothing here, though the result comes up)
"73bc1fe4-2c4a-4036-9373-242811e3e7d9": { },<!-- nothing here, though the result comes up)
"b7e7a44a-57c4-4378-94fc-273229b0ac7f":
{
"some_field":
[
"Bla bla bla, <em>highlighted search-term</em> bla bla bla..."
]
},
)
The problem is that I cannot find the way how to tell the Django template system to access that some_field
, since it's under content.highlighting
(and its id is under result.id
). Of course content.highlighting.result.id.some_field
doesn't work - is there a way of concatenating something like {{ content.highlighting}} + {{ result.id }}
, so that I can output the highlighted string in the template?
this is how I solved my problem.
Basically, I added this code in the view:
for d in response:
d['highlighted_string'] = response.highlighting[d['id']]
results_list = response
so that each result-item coming from solr contains the highlighted_string corresponding to its own ID. Then, in the template, result.highlighted_string outputs the correct highlighted_string corresponding with that result.
I hope it can be of help to someone else.