Skip to content

Commit

Permalink
Fix queryset highlighting so it actually works
Browse files Browse the repository at this point in the history
  • Loading branch information
rlskoeser committed Feb 20, 2020
1 parent ebaf47d commit 030c2f1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions parasolr/query/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _set_highlighting_opts(self, query_opts: Dict) -> None:
if self.highlight_field:
query_opts.update({
'hl': True,
'hl.field': self.highlight_field
'hl.fl': self.highlight_field
})
for key, val in self.highlight_opts.items():
query_opts['hl.%s' % key] = val
Expand Down Expand Up @@ -519,7 +519,7 @@ def get_highlighting(self):
"""Return the highlighting portion of the Solr response."""
if not self._result_cache:
self.get_results()
return self._result_cache.get('highlighting', {})
return self._result_cache.highlighting

def all(self) -> 'SolrQuerySet':
"""Return a new queryset that is a copy of the current one."""
Expand Down
13 changes: 9 additions & 4 deletions parasolr/query/tests/test_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_query_opts(self):
assert query_opts['q'] == '*:*'
# don't include unset options
for opt in ['fq', 'rows', 'sort', 'fl', 'hl',
'hl.field', 'facet', 'stats', 'stats.field']:
'hl.fl', 'facet', 'stats', 'stats.field']:
assert opt not in query_opts

# customized query opts
Expand All @@ -53,7 +53,7 @@ def test_query_opts(self):
assert query_opts['fl'] == ','.join(sqs.field_list)
# highlighting should be turned on
assert query_opts['hl']
assert query_opts['hl.field'] == 'content'
assert query_opts['hl.fl'] == 'content'
# highlighting options added with hl.prefix
assert query_opts['hl.snippets'] == 3
assert query_opts['hl.method'] == 'unified'
Expand Down Expand Up @@ -496,12 +496,17 @@ def test_get_highlighting(self):
mocksolr = Mock(spec=SolrClient)
sqs = SolrQuerySet(mocksolr)
# simulate result cache already populated, no highlighting
sqs._result_cache = {'response': {'docs': []}}
sqs._result_cache = QueryResponse({
'responseHeader': {'params': ''},
'response': {
'docs': [], 'numFound': 0, 'start': 1,
}
})
assert sqs.get_highlighting() == {}

# simulate response with highlighting
mock_highlights = {'id1': {'text': ['sample match content']}}
sqs._result_cache = {'highlighting': mock_highlights}
sqs._result_cache = Mock(highlighting=mock_highlights)
assert sqs.get_highlighting() == mock_highlights

# should populate cache if empty
Expand Down
2 changes: 2 additions & 0 deletions parasolr/solr/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def __init__(self, response: Dict) -> None:
if 'facet_counts' in response:
self.facet_counts = \
self._process_facet_counts(response.facet_counts)
self.highlighting = response.get('highlighting', {})

# NOTE: To access facet_counts.facet_fields or facet_counts.facet_ranges
# as OrderedDicts, you must use dict notation (or AttrDict *will*
# convert).
Expand Down
5 changes: 3 additions & 2 deletions parasolr/solr/tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from collections import OrderedDict
import time
from collections import OrderedDict

from attrdict import AttrDict
import requests

from parasolr import __version__ as parasolr_ver
from parasolr.solr.admin import CoreAdmin
from parasolr.solr.client import QueryResponse, SolrClient, ParasolrDict
from parasolr.solr.client import ParasolrDict, QueryResponse, SolrClient
from parasolr.solr.schema import Schema
from parasolr.solr.update import Update

Expand Down Expand Up @@ -93,6 +93,7 @@ def test_init(self):
OrderedDict)
assert qr.facet_counts['facet_fields']['A']['5'] == 1
assert qr.facet_counts['facet_ranges']['A']['counts']['2'] == 2
assert qr.highlighting == {}


class TestSolrClient:
Expand Down

0 comments on commit 030c2f1

Please sign in to comment.