def extra_request_info(request): """ Add some extra useful stuff into the RequestContext. """ fulltext_range = _fulltext_range() return { 'site_title': 'Chronicling America', 'omniture_url': settings.OMNITURE_SCRIPT if "OMNITURE_SCRIPT" in dir(settings) else None, 'sharetool_url': settings.SHARETOOL_URL if "SHARETOOL_URL" in dir(settings) else None, 'fulltext_startdate': fulltext_range[0], 'fulltext_enddate': fulltext_range[1], }
def page_search(d): """ Pass in form data for a given page search, and get back a corresponding solr query. """ q = ['+type:page'] if d.get('lccn', None): q.append(query_join(d.getlist('lccn'), 'lccn')) if d.get('state', None): q.append(query_join(d.getlist('state'), 'state')) date_filter_type = d.get('dateFilterType', None) date_boundaries = _fulltext_range() date1 = d.get('date1', None) date2 = d.get('date2', None) if not date1: date1 = date_boundaries[0] if not date2: date2 = date_boundaries[1] if date_filter_type == 'year': date1 = int(date1) date2 = int(date2) q.append('+year:[%(year)s TO %(year)s]' % d) elif date_filter_type in ('range', 'yearRange'): d1 = _solrize_date(str(date1)) d2 = _solrize_date(str(date2), is_start=False) if d1 and d2: date1, date2 = map(lambda d: int(str(d)[:4]), (d1, d2)) q.append('+date:[%i TO %i]' % (d1, d2)) # choose a facet range gap such that the number of date ranges returned # is <= 10. These would be used to populate a select dropdown on search # results page. gap = max(1, int(math.ceil((date2 - date1)/10))) ocrs = ['ocr_%s' % l for l in settings.SOLR_LANGUAGES] lang = d.get('language', None) lang_full = models.Language.objects.get(code=str(lang)) if lang else None if lang_full: q.append('+language:%s' % lang_full) ocr_lang = 'ocr_' + lang if lang else 'ocr' if d.get('ortext', None): q.append('+((' + query_join(solr_escape(d['ortext']).split(' '), "ocr")) if lang: q.append(' AND ' + query_join(solr_escape(d['ortext']).split(' '), ocr_lang)) q.append(') OR ' + query_join(solr_escape(d['ortext']).split(' '), ocr_lang)) else: q.append(')') for ocr in ocrs: q.append('OR ' + query_join(solr_escape(d['ortext']).split(' '), ocr)) q.append(')') if d.get('andtext', None): q.append('+((' + query_join(solr_escape(d['andtext']).split(' '), "ocr", and_clause=True)) if lang: q.append('AND ' + query_join(solr_escape(d['andtext']).split(' '), ocr_lang, and_clause=True)) q.append(') OR ' + query_join(solr_escape(d['andtext']).split(' '), ocr_lang, and_clause=True)) else: q.append(')') for ocr in ocrs: q.append('OR ' + query_join(solr_escape(d['andtext']).split(' '), ocr, and_clause=True)) q.append(')') if d.get('phrasetext', None): phrase = solr_escape(d['phrasetext']) q.append('+((' + 'ocr' + ':"%s"^10000' % (phrase)) if lang: q.append('AND ocr_' + lang + ':"%s"' % (phrase)) q.append(') OR ocr_' + lang + ':"%s"' % (phrase)) else: q.append(')') for ocr in ocrs: q.append('OR ' + ocr + ':"%s"' % (phrase)) q.append(')') if d.get('proxtext', None): distance = d.get('proxdistance', PROX_DISTANCE_DEFAULT) prox = solr_escape(d['proxtext']) q.append('+((' + 'ocr' + ':("%s"~%s)^10000' % (prox, distance)) if lang: q.append('AND ocr_' + lang + ':"%s"~%s' % (prox, distance)) q.append(') OR ocr_' + lang + ':"%s"~%s' % (prox, distance)) else: q.append(')') for ocr in ocrs: q.append('OR ' + ocr + ':"%s"~%s' % (prox, distance)) q.append(')') if d.get('sequence', None): q.append('+sequence:"%s"' % d['sequence']) if d.get('issue_date', None): q.append('+month:%d +day:%d' % (int(d['date_month']), int(d['date_day']))) facet_params = {'facet': 'true','facet_field': ['state', 'county'], 'facet_range':'year', 'f_year_facet_range_start': date1, 'f_year_facet_range_end': date2, 'f_year_facet_range_gap': gap, 'facet_mincount': 1} return ' '.join(q), facet_params