Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore compatibility with latest django-haystack #160

Merged
merged 5 commits into from
Oct 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ language: python

matrix:
include:
- python: 3.3
env: DJANGO_VERSION=1.6 XAPIAN_VERSION=1.3.3
- python: 3.3
env: DJANGO_VERSION=1.7 XAPIAN_VERSION=1.3.3
- python: 3.3
- python: 3.4
env: DJANGO_VERSION=1.8 XAPIAN_VERSION=1.3.3
- python: 2.7
env: DJANGO_VERSION=1.6 XAPIAN_VERSION=1.3.3
- python: 2.7
env: DJANGO_VERSION=1.7 XAPIAN_VERSION=1.3.3
- python: 3.4
env: DJANGO_VERSION=1.9 XAPIAN_VERSION=1.3.3
- python: 3.4
env: DJANGO_VERSION=1.10 XAPIAN_VERSION=1.3.3
- python: 2.7
env: DJANGO_VERSION=1.8 XAPIAN_VERSION=1.3.3
- python: 2.7
env: DJANGO_VERSION=1.6 XAPIAN_VERSION=1.2.19
env: DJANGO_VERSION=1.9 XAPIAN_VERSION=1.3.3
- python: 2.7
env: DJANGO_VERSION=1.7 XAPIAN_VERSION=1.2.19
env: DJANGO_VERSION=1.10 XAPIAN_VERSION=1.3.3
- python: 2.7
env: DJANGO_VERSION=1.8 XAPIAN_VERSION=1.2.19
- python: 2.7
env: DJANGO_VERSION=1.9 XAPIAN_VERSION=1.2.19
- python: 2.7
env: DJANGO_VERSION=1.10 XAPIAN_VERSION=1.2.19

addons:
apt:
Expand Down
9 changes: 7 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,24 @@ Xapian-Haystack provides all the standard features of Haystack:
- Spelling suggestions
- EdgeNGram and Ngram (for autocomplete)

Limitations
-----------

The `endswith` search operation is not supported by Xapian-Haystack.


Requirements
------------

- Python 2.7 or 3.3
- Django 1.6+
- Django 1.8+
- Django-Haystack 2
- Xapian 1.2.19+

In particular, we build this backend on `Travis`_ using:

- Python 2.7 and 3.3
- Django 1.6, 1.7 and 1.8
- Django 1.8, 1.9 and 1.10
- Django-Haystack (master)
- Xapian 1.2.19 (in Python 2) and 1.3.3 (in both)

Expand Down
4 changes: 2 additions & 2 deletions install_xapian.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ BINDINGS=xapian-bindings-$VERSION

# download
echo "Downloading source..."
curl -O http://oligarchy.co.uk/xapian/$VERSION/${CORE}.tar.xz
curl -O http://oligarchy.co.uk/xapian/$VERSION/${BINDINGS}.tar.xz
curl -O https://oligarchy.co.uk/xapian/$VERSION/${CORE}.tar.xz
curl -O https://oligarchy.co.uk/xapian/$VERSION/${BINDINGS}.tar.xz

# extract
echo "Extracting source..."
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Django>=1.6
Django>=1.8
Django-Haystack>=2
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def read(fname):
license='GPL2',
py_modules=['xapian_backend'],
install_requires=[
'django>=1.6',
'django>=1.8',
'django-haystack>=2',
]
)
4 changes: 2 additions & 2 deletions tests/xapian_tests/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import subprocess
import os

from django.apps import apps
from django.test import TestCase
from django.db.models.loading import get_model
from django.utils.encoding import force_text

from haystack import connections
Expand All @@ -27,7 +27,7 @@
class XapianSearchResult(SearchResult):
def __init__(self, app_label, model_name, pk, score, **kwargs):
super(XapianSearchResult, self).__init__(app_label, model_name, pk, score, **kwargs)
self._model = get_model('xapian_tests', model_name)
self._model = apps.get_model('xapian_tests', model_name)


def get_terms(backend, *args):
Expand Down
12 changes: 6 additions & 6 deletions tests/xapian_tests/tests/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ def test_content_search(self):
self.assertEqual(pks(result)[:4], [1, 4, 7, 10])

def test_field_search(self):
self.assertEqual(pks(self.queryset.filter(name='8')), [4])
self.assertEqual(pks(self.queryset.filter(name__contains='8')), [4])
self.assertEqual(pks(self.queryset.filter(type_name='book')),
pks(Document.objects.filter(type_name='book')))

self.assertEqual(pks(self.queryset.filter(text='text huge')),
self.assertEqual(pks(self.queryset.filter(text__contains='text huge')),
pks(Document.objects.filter(text__contains='text huge')))

def test_field_contains(self):
self.assertEqual(pks(self.queryset.filter(summary='huge')),
self.assertEqual(pks(self.queryset.filter(summary__contains='huge')),
pks(Document.objects.filter(summary__contains='huge')))

result = self.queryset.filter(summary='huge summary')
result = self.queryset.filter(summary__contains='huge summary')
self.assertEqual(sorted(pks(result)),
pks(Document.objects.all()))

Expand All @@ -103,7 +103,7 @@ def test_content_and(self):
self.assertEqual(pks(self.queryset.filter(content='huge').filter(summary='medium')), [])

self.assertEqual(len(self.queryset.filter(content='huge this')), 12)
self.assertEqual(len(self.queryset.filter(content='huge this').filter(summary='huge')), 4)
self.assertEqual(len(self.queryset.filter(content='huge this').filter(summary__contains='huge')), 4)

def test_content_or(self):
self.assertEqual(len(self.queryset.filter(content='huge medium')), 8)
Expand All @@ -113,7 +113,7 @@ def test_field_and(self):
self.assertEqual(pks(self.queryset.filter(name='8').filter(name='4')), [])

def test_field_or(self):
self.assertEqual(pks(self.queryset.filter(name='8 4')), [2, 4])
self.assertEqual(pks(self.queryset.filter(name__contains='8 4')), [2, 4])

def test_field_in(self):
self.assertEqual(set(pks(self.queryset.filter(name__in=['magazine 2', 'article 4']))),
Expand Down
40 changes: 27 additions & 13 deletions tests/xapian_tests/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class XapianSearchQueryTestCase(HaystackBackendTestCase, TestCase):
Tests the XapianSearchQuery, the class that converts SearchQuerySet queries
using the `__` notation to XapianQueries.
"""
fixtures = ['base_data.json']

def get_index(self):
return MockQueryIndex()

Expand All @@ -41,15 +43,15 @@ def test_single_word_not(self):
'(<alldocuments> AND_NOT (Zhello OR hello))')

def test_single_word_field_exact(self):
self.sq.add_filter(SQ(foo='hello'))
self.sq.add_filter(SQ(foo__exact='hello'))
self.assertExpectedQuery(self.sq.build_query(),
'(ZXFOOhello OR XFOOhello)')
'(XFOO^ PHRASE 3 XFOOhello PHRASE 3 XFOO$)')

def test_single_word_field_exact_not(self):
self.sq.add_filter(~SQ(foo='hello'))
self.assertExpectedQuery(self.sq.build_query(),
'(<alldocuments> AND_NOT '
'(ZXFOOhello OR XFOOhello))')
'(XFOO^ PHRASE 3 XFOOhello PHRASE 3 XFOO$))')

def test_boolean(self):
self.sq.add_filter(SQ(content=True))
Expand Down Expand Up @@ -126,15 +128,15 @@ def test_multiple_word_field_exact(self):
self.sq.add_filter(SQ(foo='hello'))
self.sq.add_filter(SQ(title='world'))
self.assertExpectedQuery(self.sq.build_query(),
'((ZXFOOhello OR XFOOhello) AND'
' (ZXTITLEworld OR XTITLEworld))')
'((XFOO^ PHRASE 3 XFOOhello PHRASE 3 XFOO$) AND'
' (XTITLE^ PHRASE 3 XTITLEworld PHRASE 3 XTITLE$))')

def test_multiple_word_field_exact_not(self):
self.sq.add_filter(~SQ(foo='hello'))
self.sq.add_filter(~SQ(title='world'))
self.assertExpectedQuery(self.sq.build_query(),
'((<alldocuments> AND_NOT (ZXFOOhello OR XFOOhello)) AND'
' (<alldocuments> AND_NOT (ZXTITLEworld OR XTITLEworld)))')
'((<alldocuments> AND_NOT (XFOO^ PHRASE 3 XFOOhello PHRASE 3 XFOO$)) AND'
' (<alldocuments> AND_NOT (XTITLE^ PHRASE 3 XTITLEworld PHRASE 3 XTITLE$)))')

def test_or(self):
self.sq.add_filter(SQ(content='hello world'))
Expand Down Expand Up @@ -248,7 +250,7 @@ class SearchQueryTestCase(HaystackBackendTestCase, TestCase):
Tests expected behavior of
SearchQuery.
"""
fixtures = ['initial_data.json']
fixtures = ['base_data.json']

def get_index(self):
return MockSearchIndex()
Expand All @@ -265,10 +267,22 @@ def test_get_spelling(self):
self.assertEqual(self.sq.get_spelling_suggestion(), 'indexed')
self.assertEqual(self.sq.get_spelling_suggestion('indxd'), 'indexed')

def test_contains(self):
self.sq.add_filter(SQ(content='circular'))
self.sq.add_filter(SQ(title__contains='haystack'))
self.assertExpectedQuery(self.sq.build_query(),
'((Zcircular OR circular) AND '
'(ZXTITLEhaystack OR XTITLEhaystack))')

def test_startswith(self):
self.sq.add_filter(SQ(name__startswith='da'))
self.assertEqual([result.pk for result in self.sq.get_results()], [1, 2, 3])

def test_endswith(self):
with self.assertRaises(NotImplementedError):
self.sq.add_filter(SQ(name__endswith='el2'))
self.sq.get_results()

def test_gt(self):
self.sq.add_filter(SQ(name__gt='m'))
self.assertExpectedQuery(self.sq.build_query(),
Expand Down Expand Up @@ -326,7 +340,7 @@ def test_log_query(self):
len(self.sq.get_results())
self.assertEqual(len(connections['default'].queries), 1)
self.assertExpectedQuery(connections['default'].queries[0]['query_string'],
'(ZXNAMEbar OR XNAMEbar)')
'(XNAME^ PHRASE 3 XNAMEbar PHRASE 3 XNAME$)')

# And again, for good measure.
self.sq = connections['default'].get_query()
Expand All @@ -335,10 +349,10 @@ def test_log_query(self):
len(self.sq.get_results())
self.assertEqual(len(connections['default'].queries), 2)
self.assertExpectedQuery(connections['default'].queries[0]['query_string'],
'(ZXNAMEbar OR XNAMEbar)')
'(XNAME^ PHRASE 3 XNAMEbar PHRASE 3 XNAME$)')
self.assertExpectedQuery(connections['default'].queries[1]['query_string'],
'((ZXNAMEbar OR XNAMEbar) AND'
' (ZXTEXTmoof OR XTEXTmoof))')
'((XNAME^ PHRASE 3 XNAMEbar PHRASE 3 XNAME$) AND'
' (XTEXT^ PHRASE 3 XTEXTmoof PHRASE 3 XTEXT$))')

# Restore.
settings.DEBUG = old_debug
Expand All @@ -348,7 +362,7 @@ class LiveSearchQuerySetTestCase(HaystackBackendTestCase, TestCase):
"""
SearchQuerySet specific tests
"""
fixtures = ['initial_data.json']
fixtures = ['base_data.json']

def get_index(self):
return MockSearchIndex()
Expand Down
8 changes: 5 additions & 3 deletions xapian_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1331,9 +1331,9 @@ def _query_from_term(self, term, field_name, filter_type, is_not):

query_list.append(self._filter_contains(term, field_name, field_type, is_not))
# when filter has no filter_type, haystack uses
# filter_type = 'contains'. Here we remove it
# filter_type = 'content'. Here we remove it
# since the above query is already doing this
if filter_type == 'contains':
if filter_type == 'content':
filter_type = None
else:
# get the field_type from the backend
Expand All @@ -1357,12 +1357,14 @@ def _query_from_term(self, term, field_name, filter_type, is_not):
# todo: we should check that the filter is valid for this field_type or raise InvalidIndexError
if filter_type == 'contains':
query_list.append(self._filter_contains(term, field_name, field_type, is_not))
elif filter_type == 'exact':
elif filter_type in ('content', 'exact'):
query_list.append(self._filter_exact(term, field_name, field_type, is_not))
elif filter_type == 'in':
query_list.append(self._filter_in(term, field_name, field_type, is_not))
elif filter_type == 'startswith':
query_list.append(self._filter_startswith(term, field_name, field_type, is_not))
elif filter_type == 'endswith':
raise NotImplementedError("The Xapian search backend doesn't support endswith queries.")
elif filter_type == 'gt':
query_list.append(self._filter_gt(term, field_name, field_type, is_not))
elif filter_type == 'gte':
Expand Down