Skip to content

Commit

Permalink
Allow more internal data exact searches
Browse files Browse the repository at this point in the history
Bump version

Fix syntax error

Adjust CHANGELOG.rst

Add test for __exact on ID
  • Loading branch information
karolyi committed Mar 18, 2023
1 parent 251e924 commit 0dcc958
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Expand Up @@ -5,6 +5,7 @@ xapian-haystack Changelog
Unreleased
----------

- Add DJANGO_CT, DJANGO_ID, ID to be used with '__exact' internally.
- Dropped support for Python 3.6.
- Fixed DatabaseLocked errors when running management commands with
multiple workers.
Expand Down
7 changes: 7 additions & 0 deletions tests/xapian_tests/tests/test_query.py
Expand Up @@ -236,6 +236,13 @@ def test_content_type(self):
self.sq.add_filter(SQ(django_ct='time'))
self.assertExpectedQuery(self.sq.build_query(), 'CONTENTTYPEtime')

def test_unphrased_id(self):
'An internal ID should NOT be phrased so one can exclude IDs.'
self.sq.add_filter(SQ(id__in=['testing123', 'testing456']))
expected = '(Qtesting123 OR Qtesting456)'
self.assertExpectedQuery(
query=self.sq.build_query(), string_or_list=expected)


class SearchQueryTestCase(HaystackBackendTestCase, TestCase):
"""
Expand Down
4 changes: 3 additions & 1 deletion xapian_backend.py
Expand Up @@ -42,6 +42,8 @@
'field': 'X'
}

_EXACT_SEARCHFIELDS = frozenset((DJANGO_CT, DJANGO_ID, ID))

MEMORY_DB_NAME = ':memory:'

DEFAULT_XAPIAN_FLAGS = (
Expand Down Expand Up @@ -1437,7 +1439,7 @@ def _filter_exact(self, term, field_name, field_type, is_not):
Assumes term is not a list.
"""
if field_type == 'text' and field_name not in (DJANGO_CT,):
if field_type == 'text' and field_name not in _EXACT_SEARCHFIELDS:
term = '^ %s $' % term
query = self._phrase_query(term.split(), field_name, field_type)
else:
Expand Down

0 comments on commit 0dcc958

Please sign in to comment.