Permalink
Browse files

Merge branch 'mattdeboard-sq-run-refactor'

  • Loading branch information...
jezdez committed Jun 20, 2012
2 parents 92435a6 + 864c882 commit 5b982ae6b8905066dc1d00c548953b0d7e98926f
@@ -758,65 +758,67 @@ def build_alt_parser_query(self, parser_name, query_string='', **kwargs):
return u"{!%s %s}" % (parser_name, ' '.join(kwarg_bits))
- def run(self, spelling_query=None, **kwargs):
- """Builds and executes the query. Returns a list of search results."""
- final_query = self.build_query()
+ def build_params(self, spelling_query=None, **kwargs):
search_kwargs = {
'start_offset': self.start_offset,
- 'result_class': self.result_class,
+ 'result_class': self.result_class
}
order_by_list = None
if self.order_by:
if order_by_list is None:
order_by_list = []
+ for order_by in self.order_by:
+ if order_by.startswith('-'):
+ order_by_list.append('%s desc' % order_by[1:])
+ else:
+ order_by_list.append('%s asc' % order_by)
+
+ search_kwargs['sort_by'] = ", ".join(order_by_list)
- for field in self.order_by:
- direction = 'asc'
- if field.startswith('-'):
- direction = 'desc'
- field = field[1:]
- order_by_list.append((field, direction))
+ if self.date_facets:
+ search_kwargs['date_facets'] = self.date_facets
- search_kwargs['sort_by'] = order_by_list
+ if self.distance_point:
+ search_kwargs['distance_point'] = self.distance_point
+
+ if self.dwithin:
+ search_kwargs['dwithin'] = self.dwithin
if self.end_offset is not None:
search_kwargs['end_offset'] = self.end_offset
- if self.highlight:
- search_kwargs['highlight'] = self.highlight
-
if self.facets:
search_kwargs['facets'] = list(self.facets)
- if self.date_facets:
- search_kwargs['date_facets'] = self.date_facets
-
- if self.query_facets:
- search_kwargs['query_facets'] = self.query_facets
-
- if self.narrow_queries:
- search_kwargs['narrow_queries'] = self.narrow_queries
-
if self.fields:
search_kwargs['fields'] = self.fields
+ if self.highlight:
+ search_kwargs['highlight'] = self.highlight
+
if self.models:
search_kwargs['models'] = self.models
- if spelling_query:
- search_kwargs['spelling_query'] = spelling_query
+ if self.narrow_queries:
+ search_kwargs['narrow_queries'] = self.narrow_queries
+
+ if self.query_facets:
+ search_kwargs['query_facets'] = self.query_facets
if self.within:
search_kwargs['within'] = self.within
- if self.dwithin:
- search_kwargs['dwithin'] = self.dwithin
-
- if self.distance_point:
- search_kwargs['distance_point'] = self.distance_point
+ if spelling_query:
+ search_kwargs['spelling_query'] = spelling_query
+ return search_kwargs
+
+ def run(self, spelling_query=None, **kwargs):
+ """Builds and executes the query. Returns a list of search results."""
+ final_query = self.build_query()
+ search_kwargs = self.build_params(spelling_query, **kwargs)
results = self.backend.search(final_query, **search_kwargs)
self._results = results.get('results', [])
self._hit_count = results.get('hits', 0)
@@ -610,13 +610,11 @@ def build_alt_parser_query(self, parser_name, query_string='', **kwargs):
return u"{!%s %s}" % (parser_name, ' '.join(kwarg_bits))
- def run(self, spelling_query=None, **kwargs):
- """Builds and executes the query. Returns a list of search results."""
- final_query = self.build_query()
+ def build_params(self, spelling_query=None, **kwargs):
search_kwargs = {
'start_offset': self.start_offset,
- 'result_class': self.result_class,
- }
+ 'result_class': self.result_class
+ }
order_by_list = None
if self.order_by:
@@ -631,42 +629,48 @@ def run(self, spelling_query=None, **kwargs):
search_kwargs['sort_by'] = ", ".join(order_by_list)
+ if self.date_facets:
+ search_kwargs['date_facets'] = self.date_facets
+
+ if self.distance_point:
+ search_kwargs['distance_point'] = self.distance_point
+
+ if self.dwithin:
+ search_kwargs['dwithin'] = self.dwithin
+
if self.end_offset is not None:
search_kwargs['end_offset'] = self.end_offset
- if self.highlight:
- search_kwargs['highlight'] = self.highlight
-
if self.facets:
search_kwargs['facets'] = list(self.facets)
- if self.date_facets:
- search_kwargs['date_facets'] = self.date_facets
-
- if self.query_facets:
- search_kwargs['query_facets'] = self.query_facets
-
- if self.narrow_queries:
- search_kwargs['narrow_queries'] = self.narrow_queries
-
if self.fields:
search_kwargs['fields'] = self.fields
+ if self.highlight:
+ search_kwargs['highlight'] = self.highlight
+
if self.models:
search_kwargs['models'] = self.models
- if spelling_query:
- search_kwargs['spelling_query'] = spelling_query
+ if self.narrow_queries:
+ search_kwargs['narrow_queries'] = self.narrow_queries
+
+ if self.query_facets:
+ search_kwargs['query_facets'] = self.query_facets
if self.within:
search_kwargs['within'] = self.within
- if self.dwithin:
- search_kwargs['dwithin'] = self.dwithin
-
- if self.distance_point:
- search_kwargs['distance_point'] = self.distance_point
+ if spelling_query:
+ search_kwargs['spelling_query'] = spelling_query
+ return search_kwargs
+
+ def run(self, spelling_query=None, **kwargs):
+ """Builds and executes the query. Returns a list of search results."""
+ final_query = self.build_query()
+ search_kwargs = self.build_params(spelling_query, **kwargs)
results = self.backend.search(final_query, **search_kwargs)
self._results = results.get('results', [])
self._hit_count = results.get('hits', 0)
@@ -319,7 +319,8 @@ def test_search(self):
self.assertEqual(self.sb.search('', highlight=True), {'hits': 0, 'results': []})
self.assertEqual(self.sb.search('Index', highlight=True)['hits'], 3)
- self.assertEqual([result.highlighted for result in self.sb.search('Index', highlight=True)['results']], [[u'<em>Indexed</em>!\n2 '], [u'<em>Indexed</em>!\n1 '], [u'<em>Indexed</em>!\n3 ']])
+ self.assertEqual([result.highlighted for result in self.sb.search('Index', highlight=True)['results']],
+ [[u'<em>Indexed</em>!\n2'], [u'<em>Indexed</em>!\n1'], [u'<em>Indexed</em>!\n3']])
self.assertEqual(self.sb.search('Indx')['hits'], 0)
self.assertEqual(self.sb.search('indax')['spelling_suggestion'], None)

0 comments on commit 5b982ae

Please sign in to comment.