Skip to content

Commit 6b8f0e5

Browse files
committed
Fix query differences
* search.clients ignores Sphinx errors, but oedipus didn't. This changes oedipus to log the error, but return []. * _sanitize_query wasn't sanitizing /. This changes oedipus to sanitize / as \/. * Adds tests for the fixes.
1 parent 9b22c5e commit 6b8f0e5

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

oedipus/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ def _filter_value_to_int(self, name, value):
402402
def _sanitize_query(query):
403403
"""Strip control characters that cause problems."""
404404
query = re.sub(r'(?<=\S)\-', '\-', query)
405+
query = query.replace('/', '\\/')
405406
return query.replace('^', '').replace('$', '')
406407

407408
def _sphinx(self):
@@ -543,8 +544,9 @@ def _raw(self):
543544
if not results:
544545
raise SearchError('Sphinx returned no results.')
545546
if results[0]['status'] == sphinxapi.SEARCHD_ERROR:
546-
raise SearchError('Sphinx had an error while performing a '
547-
'query.')
547+
log.error('Sphinx errored while performing a query: %r',
548+
results[0]['error'])
549+
return {'matches': []}
548550

549551
# We do only one query at a time; return the first one:
550552
return self._results_cache[0]

oedipus/tests/test_misc.py

+18
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ def test_connection_failure(sphinx_client):
4242
assert_raises(SearchError, S(Biscuit)._raw)
4343

4444

45+
@fudge.patch('sphinxapi.SphinxClient')
46+
def test_query_error(sphinx_client):
47+
"""[] should get returned when the query has an error."""
48+
(sphinx_client.expects_call().returns_fake()
49+
.is_a_stub()
50+
.expects('RunQueries').returns([{
51+
'status': 1,
52+
'warning': '',
53+
'error': 'index questions: syntax error'}]))
54+
eq_(S(Biscuit)._raw(), {'matches': []})
55+
56+
4557
@fudge.patch('sphinxapi.SphinxClient')
4658
@fudge.patch('oedipus.settings')
4759
def test_sphinx_max_results_clips(sphinx_client, settings):
@@ -57,3 +69,9 @@ def test_sphinx_max_results_clips(sphinx_client, settings):
5769
s = S(Biscuit)[0:]
5870
# Do this to trigger the results.
5971
s.count()
72+
73+
74+
def test_sanitize_query():
75+
"""Tests _sanitize_query."""
76+
sq = S._sanitize_query
77+
eq_(sq('google.com/iq'), 'google.com\\/iq')

0 commit comments

Comments
 (0)