Skip to content

Commit

Permalink
Merge pull request #8 from DirectEmployers/pysolr-upgrade
Browse files Browse the repository at this point in the history
Made saved-search semi-compatible with the newest version of pysolr [ST-307]
  • Loading branch information
JLMcLaugh committed Aug 25, 2014
2 parents f064f4e + 17e43d8 commit 7090c01
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
*.pyc
locpop.py
python_usdol.py
.idea
62 changes: 20 additions & 42 deletions saved_search/groupsearch.py
Expand Up @@ -55,7 +55,7 @@ def search(self, query_string, sort_by=None, start_offset=0, end_offset=None,
limit_to_registered_models=None, result_class=None, group=True,
group_ngroups=True, group_query=[], group_format="simple",
**kwargs):

if not group_query:
raise GroupQueryError("You must specify at least one group query.")

Expand All @@ -79,10 +79,11 @@ def search(self, query_string, sort_by=None, start_offset=0, end_offset=None,
if sort_by is not None:
kwargs['sort'] = sort_by

if start_offset is not None:
if start_offset is None and end_offset is None:
kwargs['rows'] = 1
elif start_offset is not None:
kwargs['start'] = start_offset

if end_offset is not None:
elif end_offset is not None:
kwargs['rows'] = end_offset - start_offset

if highlight is True:
Expand All @@ -99,7 +100,12 @@ def search(self, query_string, sort_by=None, start_offset=0, end_offset=None,

if facets is not None:
kwargs['facet'] = 'on'
kwargs['facet.field'] = facets
kwargs['facet.field'] = facets.keys()

for facet_field, options in facets.items():
for key, value in options.items():
kwargs['f.%s.facet.%s' % (facet_field, key)] = self.conn._from_python(value)


kwargs['group'] = self.conn._from_python(kwargs['group'])
kwargs['group.ngroups'] = self.conn._from_python(kwargs['group.ngroups'])
Expand Down Expand Up @@ -159,19 +165,22 @@ def search(self, query_string, sort_by=None, start_offset=0, end_offset=None,

return [self._process_results(res, highlight=highlight,
result_class=result_class)
for res in raw_results]
for res in raw_results.grouped.iteritems()]

def _process_results(self, raw_results, highlight=False, result_class=None):
from haystack import connections

results = []
hits = raw_results.hits

try:
group = raw_results.group
except:
hits = raw_results[1]['doclist']['numFound']
except (KeyError, IndexError):
hits = 0

try:
group = raw_results[0]
except IndexError:
group = ""

facets = {}
spelling_suggestion = None

Expand Down Expand Up @@ -199,37 +208,6 @@ def _process_results(self, raw_results, highlight=False, result_class=None):
# collated result from the end.
spelling_suggestion = raw_results.spellcheck.get('suggestions')[-1]

unified_index = connections[self.connection_alias].get_unified_index()
indexed_models = unified_index.get_indexed_models()

for raw_result in raw_results.docs:
app_label, model_name = raw_result[DJANGO_CT].split('.')
additional_fields = {}
model = get_model(app_label, model_name)

if model and model in indexed_models:
for key, value in raw_result.items():
index = unified_index.get_index(model)
string_key = str(key)

if string_key in index.fields and hasattr(index.fields[string_key], 'convert'):
additional_fields[string_key] = index.fields[string_key].convert(value)
else:
additional_fields[string_key] = self.conn._to_python(value)

del(additional_fields[DJANGO_CT])
del(additional_fields[DJANGO_ID])
del(additional_fields['score'])

if raw_result[ID] in getattr(raw_results, 'highlighting', {}):
additional_fields['highlighted'] = raw_results.highlighting[raw_result[ID]]

result = result_class(app_label, model_name, raw_result[DJANGO_ID],
raw_result['score'], **additional_fields)
results.append(result)
else:
hits -= 1

return {
'group': group,
'results': results,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -3,7 +3,7 @@

setup(
name = "saved_search",
version = "0.1",
version = "0.2",
description = "User-defined, persistent searches for Django-Haystack.",
author = "Matt DeBoard",
author_email = "matt@directemployers.com",
Expand Down

0 comments on commit 7090c01

Please sign in to comment.