Skip to content

Instantly share code, notes, and snippets.

/search_utils.py Secret

Created November 5, 2013 23:36
Show Gist options
  • Save anonymous/7997d090e8db4eb7b294 to your computer and use it in GitHub Desktop.
Save anonymous/7997d090e8db4eb7b294 to your computer and use it in GitHub Desktop.
def build_main_query(cd, highlight=True):
main_params = {}
# Build up all the queries needed
main_params['q'] = cd['q'] or '*:*'
# Sorting for the main query
main_params['sort'] = cd.get('sort', '')
if str(main_params['sort']).startswith('score'):
main_params['boost'] = 'pagerank'
if highlight:
# Requested fields for the main query. We only need the fields here that
# are not requested as part of highlighting. Facet params are not set
# here because they do not retrieve results, only counts (they are set
# to 0 rows).
main_params['fl'] = 'id,absolute_url,court_id,local_path,source,download_url,status,dateFiled,citeCount'
# Highlighting for the main query.
main_params['hl'] = 'true'
main_params['hl.fl'] = 'text,caseName,judge,suitNature,citation,neutralCite,docketNumber,lexisCite,court_citation_string'
main_params['f.caseName.hl.fragListBuilder'] = 'single'
main_params['f.judge.hl.fragListBuilder'] = 'single'
main_params['f.suitNature.hl.fragListBuilder'] = 'single'
main_params['f.citation.hl.fragListBuilder'] = 'single'
main_params['f.neutralCite.hl.fragListBuilder'] = 'single'
main_params['f.docketNumber.hl.fragListBuilder'] = 'single'
main_params['f.lexisCite.hl.fragListBuilder'] = 'single'
main_params['f.court_citation_string.hl.fragListBuilder'] = 'single'
main_params['f.text.hl.snippets'] = '5'
# If there aren't any hits in the text return the field instead
main_params['f.text.hl.alternateField'] = 'text'
main_params['f.text.hl.maxAlternateFieldLength'] = '500'
main_params['f.caseName.hl.alternateField'] = 'caseName'
main_params['f.judge.hl.alternateField'] = 'judge'
main_params['f.suitNature.hl.alternateField'] = 'suitNature'
main_params['f.citation.hl.alternateField'] = 'citation'
main_params['f.neutralCite.hl.alternateField'] = 'neutralCite'
main_params['f.docketNumber.hl.alternateField'] = 'docketNumber'
main_params['f.lexisCite.hl.alternateField'] = 'lexisCite'
main_params['f.court_citation_string.hl.alternateField'] = 'court_citation_string'
else:
# highlighting is off, therefore we get the default fl parameter,
# which gives us all fields. We could set it manually, but there's
# no need.
pass
main_fq = []
# Case Name and judges
if cd['case_name']:
main_fq.append('caseName:(%s)' % " AND ".join(cd['case_name'].split()))
if cd['judge']:
main_fq.append('judge:(%s)' % ' AND '.join(cd['judge'].split()))
# Citations
if cd['citation']:
main_fq.append('citation:(%s)' % ' AND '.join(cd['citation'].split()))
if cd['docket_number']:
main_fq.append('docketNumber:(%s)' % ' AND '.join(cd['docket_number'].split()))
if cd['neutral_cite']:
main_fq.append('neutralCite:(%s)' % ' AND '.join(cd['neutral_cite'].split()))
# Dates
date_query = make_date_query(cd)
main_fq.append(date_query)
# Citation count
cite_count_query = make_cite_count_query(cd)
main_fq.append(cite_count_query)
# Facet filters
selected_courts_string = get_selected_field_string(cd, 'court_')
selected_stats_string = get_selected_field_string(cd, 'stat_')
if len(selected_courts_string) + len(selected_stats_string) > 0:
main_fq.extend(['{!tag=dt}status_exact:(%s)' % selected_stats_string,
'{!tag=dt}court_exact:(%s)' % selected_courts_string])
# If a param has been added to the fq variables, then we add them to the
# main_params var. Otherwise, we don't, as doing so throws an error.
if len(main_fq) > 0:
main_params['fq'] = main_fq
#print "Params sent to search are: %s" % '&'.join(['%s=%s' % (k, v) for k, v in main_params.items()])
#print results_si.execute()
return main_params
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment