Updated date_facet to deal with changes in SHA d1a195 in Haystack.

This commit is contained in:
David Sauve 2009-08-16 14:03:19 -04:00
parent 9754ba0338
commit 9cf9fe9e27
2 changed files with 9 additions and 12 deletions

View file

@ -191,8 +191,8 @@ class XapianSearchBackendTestCase(TestCase):
self.sb.update(self.msi, self.sample_objs)
self.assertEqual(len(self.xapian_search('')), 3)
self.assertEqual(self.sb.search('', date_facets={'pub_date': {'start_date': datetime.datetime(2008, 10, 26), 'end_date': datetime.datetime(2009, 3, 26), 'gap': 'month=1'}}), {'hits': 0, 'results': []})
results = self.sb.search('index', date_facets={'pub_date': {'start_date': datetime.datetime(2008, 10, 26), 'end_date': datetime.datetime(2009, 3, 26), 'gap': 'month=1'}})
self.assertEqual(self.sb.search('', date_facets={'pub_date': {'start_date': datetime.datetime(2008, 10, 26), 'end_date': datetime.datetime(2009, 3, 26), 'gap_by': 'month'}}), {'hits': 0, 'results': []})
results = self.sb.search('index', date_facets={'pub_date': {'start_date': datetime.datetime(2008, 10, 26), 'end_date': datetime.datetime(2009, 3, 26), 'gap_by': 'month'}})
self.assertEqual(results['hits'], 3)
self.assertEqual(results['facets']['dates']['pub_date'], [
('2009-02-26T00:00:00', 0),
@ -202,7 +202,7 @@ class XapianSearchBackendTestCase(TestCase):
('2008-10-26T00:00:00', 0),
])
results = self.sb.search('index', date_facets={'pub_date': {'start_date': datetime.datetime(2009, 02, 01), 'end_date': datetime.datetime(2009, 3, 15), 'gap': 'days=15'}})
results = self.sb.search('index', date_facets={'pub_date': {'start_date': datetime.datetime(2009, 02, 01), 'end_date': datetime.datetime(2009, 3, 15), 'gap_by': 'day', 'gap_amount': 15}})
self.assertEqual(results['hits'], 3)
self.assertEqual(results['facets']['dates']['pub_date'], [
('2009-03-03T00:00:00', 0),

View file

@ -42,8 +42,6 @@ DOCUMENT_ID_TERM_PREFIX = 'Q'
DOCUMENT_CUSTOM_TERM_PREFIX = 'X'
DOCUMENT_CT_TERM_PREFIX = DOCUMENT_CUSTOM_TERM_PREFIX + 'CONTENTTYPE'
gap_re = re.compile(r'(?P<type>year|month|day|hour|minute|second+)s?=?(?P<value>\d*)', re.IGNORECASE)
class XHValueRangeProcessor(xapian.ValueRangeProcessor):
def __init__(self, sb):
@ -525,12 +523,12 @@ class SearchBackend(BaseSearchBackend):
Required arguments:
`results` -- A list SearchResults to facet
`date_facets` -- A dictionary containing facet parameters:
{'field': {'start_date': ..., 'end_date': ...: 'gap': '...'}}
nb., gap must satisfy the regex:
(?P<type>year|month|day|hour|minute|second+)s?=?(?P<value>\d*)
{'field': {'start_date': ..., 'end_date': ...: 'gap_by': '...', 'gap_amount': n}}
nb., gap must be one of the following:
year|month|day|hour|minute|second
For each date facet field in `date_facets`, generates a list
of date ranges (from `start_date` to `end_date` by `gap`) then
of date ranges (from `start_date` to `end_date` by `gap_by`) then
iterates through `results` and tallies the count for each date_facet.
Returns a dictionary of date facets (fields) containing a list with
@ -549,9 +547,8 @@ class SearchBackend(BaseSearchBackend):
facet_dict = {}
for date_facet, facet_params in date_facets.iteritems():
match = gap_re.search(facet_params['gap']).groupdict()
gap_type = match['type']
gap_value = match.get('value', 1)
gap_type = facet_params.get('gap_by')
gap_value = facet_params.get('gap_amount', 1)
date_range = facet_params['start_date']
facet_list = []
while date_range < facet_params['end_date']: