Skip to content

Commit

Permalink
Merge b4b43f8 into 1a5b4cf
Browse files Browse the repository at this point in the history
  • Loading branch information
asedeno committed Sep 26, 2021
2 parents 1a5b4cf + b4b43f8 commit 34af3c5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 81 deletions.
10 changes: 2 additions & 8 deletions install_xapian.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# first argument of the script is Xapian version (e.g. 1.2.19)
# first argument of the script is Xapian version (e.g. 1.4.18)

VERSION=$1

Expand Down Expand Up @@ -31,12 +31,6 @@ cd $VIRTUAL_ENV/packages/${CORE}

PYTHON_FLAG=--with-python3

if [ $VERSION = "1.3.3" ]; then
XAPIAN_CONFIG=$VIRTUAL_ENV/bin/xapian-config-1.3
else
XAPIAN_CONFIG=
fi

# The bindings for Python require python-sphinx
echo "Installing Python-Sphinx..."
SPHINX2_FIXED_VERSION=1.4.12
Expand All @@ -48,7 +42,7 @@ fi

echo "Installing Xapian-bindings..."
cd $VIRTUAL_ENV/packages/${BINDINGS}
./configure --prefix=$VIRTUAL_ENV $PYTHON_FLAG XAPIAN_CONFIG=$XAPIAN_CONFIG && make && make install
./configure --prefix=$VIRTUAL_ENV $PYTHON_FLAG && make && make install

# clean
cd $VIRTUAL_ENV
Expand Down
31 changes: 8 additions & 23 deletions tests/xapian_tests/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,14 @@ def tearDown(self):
self.backend.clear()
connections['default']._index = self.old_ui

def assertExpectedQuery(self, query, string_or_list, xapian12string=''):
def assertExpectedQuery(self, query, string_or_list):
if isinstance(string_or_list, list):
strings = string_or_list
else:
strings = [string_or_list]

expected = ['Query(%s)' % string for string in strings]

if XAPIAN_VERSION[1] <= 2:
if xapian12string:
expected = ['Xapian::Query(%s)' % xapian12string]
else:
expected = ['Xapian::Query(%s)' % string for string in strings]

self.assertIn(str(query), expected)


Expand Down Expand Up @@ -607,11 +601,9 @@ def test_build_schema(self):
])

def test_parse_query(self):
self.assertExpectedQuery(self.backend.parse_query('indexed'), 'Zindex@1',
xapian12string='Zindex:(pos=1)')
self.assertExpectedQuery(self.backend.parse_query('indexed'), 'Zindex@1')

self.assertExpectedQuery(self.backend.parse_query('name:david'),
'ZXNAMEdavid@1', xapian12string='ZXNAMEdavid:(pos=1)')
self.assertExpectedQuery(self.backend.parse_query('name:david'), 'ZXNAMEdavid@1')

if xapian.minor_version() >= 2:
# todo: why `SYNONYM WILDCARD OR XNAMEda`?
Expand All @@ -620,10 +612,7 @@ def test_parse_query(self):
[
'(SYNONYM WILDCARD OR XNAMEda)',
'WILDCARD SYNONYM XNAMEda',
],
xapian12string='(XNAMEdavid1:(pos=1) SYNONYM '
'XNAMEdavid2:(pos=1) SYNONYM '
'XNAMEdavid3:(pos=1))')
])
else:
self.assertEqual(str(self.backend.parse_query('name:da*')),
'Xapian::Query(('
Expand All @@ -636,26 +625,22 @@ def test_parse_query_range(self):
[
'0 * VALUE_RANGE 9 david1 david2',
'VALUE_RANGE 9 david1 david2',
],
xapian12string='VALUE_RANGE 9 david1 david2')
])
self.assertExpectedQuery(self.backend.parse_query('number:0..10'),
[
'0 * VALUE_RANGE 11 000000000000 000000000010',
'VALUE_RANGE 11 000000000000 000000000010',
],
xapian12string='VALUE_RANGE 11 000000000000 000000000010')
])
self.assertExpectedQuery(self.backend.parse_query('number:..10'),
[
'0 * VALUE_RANGE 11 %012d 000000000010' % (-sys.maxsize - 1),
'VALUE_RANGE 11 %012d 000000000010' % (-sys.maxsize - 1),
],
xapian12string='VALUE_RANGE 11 %012d 000000000010' % (-sys.maxsize - 1))
])
self.assertExpectedQuery(self.backend.parse_query('number:10..*'),
[
'0 * VALUE_RANGE 11 000000000010 %012d' % sys.maxsize,
'VALUE_RANGE 11 000000000010 %012d' % sys.maxsize,
],
xapian12string='VALUE_RANGE 11 000000000010 %012d' % sys.maxsize)
])

def test_order_by_django_id(self):
"""
Expand Down
22 changes: 6 additions & 16 deletions tests/xapian_tests/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,12 @@ def test_datetime(self):
self.sq.add_filter(SQ(content=datetime.datetime(2009, 5, 8, 11, 28)))
self.assertExpectedQuery(self.sq.build_query(),
'((Z2009-05-08 OR 2009-05-08) OR'
' (Z11:28:00 OR 11:28:00))',
xapian12string='(Z2009-05-08 OR 2009-05-08 OR'
' Z11:28:00 OR 11:28:00)')
' (Z11:28:00 OR 11:28:00))')

def test_datetime_not(self):
self.sq.add_filter(~SQ(content=datetime.datetime(2009, 5, 8, 11, 28)))
self.assertExpectedQuery(self.sq.build_query(),
'(<alldocuments> AND_NOT ((Z2009-05-08 OR 2009-05-08) OR (Z11:28:00 OR 11:28:00)))',
xapian12string='(<alldocuments> AND_NOT '
'(Z2009-05-08 OR 2009-05-08 OR'
' Z11:28:00 OR 11:28:00))')
'(<alldocuments> AND_NOT ((Z2009-05-08 OR 2009-05-08) OR (Z11:28:00 OR 11:28:00)))')

def test_float(self):
self.sq.add_filter(SQ(content=25.52))
Expand All @@ -103,8 +98,7 @@ def test_multiple_words_or(self):
self.sq.add_filter(SQ(content='hello') | SQ(content='world'))
self.assertExpectedQuery(
self.sq.build_query(),
'((Zhello OR hello) OR (Zworld OR world))',
xapian12string='(Zhello OR hello OR Zworld OR world)')
'((Zhello OR hello) OR (Zworld OR world))')

def test_multiple_words_or_not(self):
self.sq.add_filter(~SQ(content='hello') | ~SQ(content='world'))
Expand All @@ -118,9 +112,7 @@ def test_multiple_words_mixed(self):
self.assertExpectedQuery(
self.sq.build_query(),
'(((Zwhi OR why) OR (Zhello OR hello)) AND '
'(<alldocuments> AND_NOT (Zworld OR world)))',
xapian12string='((Zwhi OR why OR Zhello OR hello) AND'
' (<alldocuments> AND_NOT (Zworld OR world)))',)
'(<alldocuments> AND_NOT (Zworld OR world)))')

def test_multiple_word_field_exact(self):
self.sq.add_filter(SQ(foo='hello'))
Expand All @@ -139,15 +131,13 @@ def test_multiple_word_field_exact_not(self):
def test_or(self):
self.sq.add_filter(SQ(content='hello world'))
self.assertExpectedQuery(
self.sq.build_query(), '((Zhello OR hello) OR (Zworld OR world))',
xapian12string='(Zhello OR hello OR Zworld OR world)')
self.sq.build_query(), '((Zhello OR hello) OR (Zworld OR world))')

def test_not_or(self):
self.sq.add_filter(~SQ(content='hello world'))
self.assertExpectedQuery(
self.sq.build_query(),
'(<alldocuments> AND_NOT ((Zhello OR hello) OR (Zworld OR world)))',
xapian12string='(<alldocuments> AND_NOT (Zhello OR hello OR Zworld OR world))')
'(<alldocuments> AND_NOT ((Zhello OR hello) OR (Zworld OR world)))')

def test_boost(self):
self.sq.add_filter(SQ(content='hello'))
Expand Down
37 changes: 3 additions & 34 deletions xapian_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@
"Please refer to the documentation.")


class NotSupportedError(Exception):
"""
When the installed version of Xapian doesn't support something and we have
the old implementation.
"""
pass

# this maps the different reserved fields to prefixes used to
# create the database:
# id str: unique document id.
Expand Down Expand Up @@ -285,11 +278,7 @@ def update(self, index, iterable, commit=True):
term_generator = xapian.TermGenerator()
term_generator.set_database(database)
term_generator.set_stemmer(xapian.Stem(self.language))
try:
term_generator.set_stemming_strategy(self.stemming_strategy)
except AttributeError:
# Versions before Xapian 1.2.11 do not support stemming strategies for TermGenerator
pass
term_generator.set_stemming_strategy(self.stemming_strategy)
if self.include_spelling is True:
term_generator.set_flags(xapian.TermGenerator.FLAG_SPELLING)

Expand Down Expand Up @@ -648,10 +637,7 @@ def search(self, query, sort_by=None, start_offset=0, end_offset=None,
enquire.set_query(query)

if sort_by:
try:
_xapian_sort(enquire, sort_by, self.column)
except NotSupportedError:
_old_xapian_sort(enquire, sort_by, self.column)
_xapian_sort(enquire, sort_by, self.column)

results = []
facets_dict = {
Expand Down Expand Up @@ -1651,25 +1637,8 @@ def _from_xapian_value(value, field_type):
return value


def _old_xapian_sort(enquire, sort_by, column):
sorter = xapian.MultiValueSorter()

for sort_field in sort_by:
if sort_field.startswith('-'):
reverse = True
sort_field = sort_field[1:] # Strip the '-'
else:
reverse = False # Reverse is inverted in Xapian -- http://trac.xapian.org/ticket/311
sorter.add(column[sort_field], reverse)

enquire.set_sort_by_key_then_relevance(sorter, True)


def _xapian_sort(enquire, sort_by, column):
try:
sorter = xapian.MultiValueKeyMaker()
except AttributeError:
raise NotSupportedError
sorter = xapian.MultiValueKeyMaker()

for sort_field in sort_by:
if sort_field.startswith('-'):
Expand Down

0 comments on commit 34af3c5

Please sign in to comment.