Reordered methods position in XapianSearchQuery.

Also removed two @staticmethod.
This commit is contained in:
Jorge C. Leitão 2014-05-17 12:42:45 +02:00
parent 93522bbd56
commit 83bd8ee3e5

View File

@ -1045,6 +1045,15 @@ class XapianSearchQuery(BaseSearchQuery):
else: else:
return xapian.Query(xapian.Query.OP_AND, query_list) return xapian.Query(xapian.Query.OP_AND, query_list)
def _all_query(self):
"""
Private method that returns a xapian.Query that returns all documents,
Returns:
A xapian.Query
"""
return xapian.Query('')
def _content_field(self, term, is_not): def _content_field(self, term, is_not):
""" """
Private method that returns a xapian.Query that searches for `value` Private method that returns a xapian.Query that searches for `value`
@ -1090,25 +1099,6 @@ class XapianSearchQuery(BaseSearchQuery):
else: else:
return query return query
def _filter_exact(self, term, field, is_not):
"""
Private method that returns a xapian.Query that searches for an exact
match for `term` in a specified `field`.
Required arguments:
``term`` -- The term to search for
``field`` -- The field to search
``is_not`` -- Invert the search results
Returns:
A xapian.Query
"""
query = self._phrase_query(term.split(), field)
if is_not:
return xapian.Query(xapian.Query.OP_AND_NOT, self._all_query(), query)
else:
return query
def _filter_in(self, term_list, field, is_not): def _filter_in(self, term_list, field, is_not):
""" """
Private method that returns a xapian.Query that searches for any term Private method that returns a xapian.Query that searches for any term
@ -1138,6 +1128,25 @@ class XapianSearchQuery(BaseSearchQuery):
else: else:
return xapian.Query(xapian.Query.OP_OR, query_list) return xapian.Query(xapian.Query.OP_OR, query_list)
def _filter_exact(self, term, field, is_not):
"""
Private method that returns a xapian.Query that searches for an exact
match for `term` in a specified `field`.
Required arguments:
``term`` -- The term to search for
``field`` -- The field to search
``is_not`` -- Invert the search results
Returns:
A xapian.Query
"""
query = self._phrase_query(term.split(), field)
if is_not:
return xapian.Query(xapian.Query.OP_AND_NOT, self._all_query(), query)
else:
return query
def _filter_startswith(self, term, field, is_not): def _filter_startswith(self, term, field, is_not):
""" """
Private method that returns a xapian.Query that searches for any term Private method that returns a xapian.Query that searches for any term
@ -1159,49 +1168,21 @@ class XapianSearchQuery(BaseSearchQuery):
) )
return self.backend.parse_query('%s:%s*' % (field, term)) return self.backend.parse_query('%s:%s*' % (field, term))
def _filter_gt(self, term, field, is_not): def _phrase_query(self, term_list, field=None, is_content=False):
return self._filter_lte(term, field, is_not=not is_not) """
Private method that returns a phrase based xapian.Query that searches
for terms in `term_list.
def _filter_lt(self, term, field, is_not): Required arguments:
return self._filter_gte(term, field, is_not=not is_not) ``term_list`` -- The terms to search for
``field`` -- The field to search (If `None`, all fields)
def _filter_gte(self, term, field, is_not):
"""
Private method that returns a xapian.Query that searches for any term
that is greater than `term` in a specified `field`.
"""
vrp = XHValueRangeProcessor(self.backend)
pos, begin, end = vrp('%s:%s' % (field, _marshal_value(term)), '*')
if is_not:
return xapian.Query(xapian.Query.OP_AND_NOT,
self._all_query(),
xapian.Query(xapian.Query.OP_VALUE_RANGE, pos, begin, end)
)
return xapian.Query(xapian.Query.OP_VALUE_RANGE, pos, begin, end)
def _filter_lte(self, term, field, is_not):
"""
Private method that returns a xapian.Query that searches for any term
that is less than `term` in a specified `field`.
"""
vrp = XHValueRangeProcessor(self.backend)
pos, begin, end = vrp('%s:' % field, '%s' % _marshal_value(term))
if is_not:
return xapian.Query(xapian.Query.OP_AND_NOT,
self._all_query(),
xapian.Query(xapian.Query.OP_VALUE_RANGE, pos, begin, end)
)
return xapian.Query(xapian.Query.OP_VALUE_RANGE, pos, begin, end)
@staticmethod
def _all_query():
"""
Private method that returns a xapian.Query that returns all documents,
Returns: Returns:
A xapian.Query A xapian.Query
""" """
return xapian.Query('') if field and not is_content:
term_list = ['%s%s%s' % (TERM_PREFIXES['field'], field.upper(), term) for term in term_list]
return xapian.Query(xapian.Query.OP_PHRASE, term_list)
def _term_query(self, term, field=None): def _term_query(self, term, field=None):
""" """
@ -1236,22 +1217,39 @@ class XapianSearchQuery(BaseSearchQuery):
xapian.Query(unstemmed) xapian.Query(unstemmed)
) )
@staticmethod def _filter_gt(self, term, field, is_not):
def _phrase_query(term_list, field=None, is_content=False): return self._filter_lte(term, field, is_not=not is_not)
"""
Private method that returns a phrase based xapian.Query that searches
for terms in `term_list.
Required arguments: def _filter_lt(self, term, field, is_not):
``term_list`` -- The terms to search for return self._filter_gte(term, field, is_not=not is_not)
``field`` -- The field to search (If `None`, all fields)
Returns: def _filter_gte(self, term, field, is_not):
A xapian.Query
""" """
if field and not is_content: Private method that returns a xapian.Query that searches for any term
term_list = ['%s%s%s' % (TERM_PREFIXES['field'], field.upper(), term) for term in term_list] that is greater than `term` in a specified `field`.
return xapian.Query(xapian.Query.OP_PHRASE, term_list) """
vrp = XHValueRangeProcessor(self.backend)
pos, begin, end = vrp('%s:%s' % (field, _marshal_value(term)), '*')
if is_not:
return xapian.Query(xapian.Query.OP_AND_NOT,
self._all_query(),
xapian.Query(xapian.Query.OP_VALUE_RANGE, pos, begin, end)
)
return xapian.Query(xapian.Query.OP_VALUE_RANGE, pos, begin, end)
def _filter_lte(self, term, field, is_not):
"""
Private method that returns a xapian.Query that searches for any term
that is less than `term` in a specified `field`.
"""
vrp = XHValueRangeProcessor(self.backend)
pos, begin, end = vrp('%s:' % field, '%s' % _marshal_value(term))
if is_not:
return xapian.Query(xapian.Query.OP_AND_NOT,
self._all_query(),
xapian.Query(xapian.Query.OP_VALUE_RANGE, pos, begin, end)
)
return xapian.Query(xapian.Query.OP_VALUE_RANGE, pos, begin, end)
def _marshal_value(value): def _marshal_value(value):