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

Limit content search #200

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tests/xapian_tests/tests/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ def test_content_search(self):
# documents with "medium" AND "this" have higher score
self.assertEqual(pks(result)[:4], [1, 4, 7, 10])

def test_content_search2(self):
# content should only search the document, not all fields.
result = self.queryset.filter(content='summary')
self.assertEqual(pks(result), [])


def test_field_search(self):
self.assertEqual(pks(self.queryset.filter(name__contains='8')), [4])
self.assertEqual(pks(self.queryset.filter(type_name='book')),
Expand Down
13 changes: 6 additions & 7 deletions xapian_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,10 +1292,9 @@ def _query_from_term(self, term, field_name, filter_type, is_not):
# It it is an AutoQuery, it has no filters
# or others, thus we short-circuit the procedure.
if isinstance(term, AutoQuery):
if field_name != 'content':
query = '%s:%s' % (field_name, term.prepare(self))
else:
query = term.prepare(self)
if field_name == 'content':
field_name = self.backend.content_field_name
query = '%s:%s' % (field_name, term.prepare(self))
return [self.backend.parse_query(query)]
query_list = []

Expand All @@ -1304,10 +1303,10 @@ def _query_from_term(self, term, field_name, filter_type, is_not):
term = list(term)

if field_name == 'content':
# content is the generic search:
# force no field_name search
# content is the document search:
# adjust field_name to point to the document field
# and the field_type to be 'text'.
field_name = None
field_name = self.backend.content_field_name
field_type = 'text'

# we don't know what is the type(term), so we parse it.
Expand Down