Created
February 28, 2013 17:55
-
-
Save birkin/5058709 to your computer and use it in GitHub Desktop.
django view function that proxys solr instances
- usage: url/collection/?q=the-solr-query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def search_custom( request, collection ): | |
""" | |
Returns unmodified SELECT solr requests to settings_app.COLLECTION_MAPPER solr urls. | |
# possible TODO: override max_rows | |
""" | |
## check that we're only using GET requests | |
if request.method != 'GET': | |
return HttpResponseNotAllowed(['GET']) | |
## grab request parameter string | |
logger.debug( u'request.GET: %s' % request.GET ) | |
logger.debug( u'request.META: %s' % request.META ) | |
logger.debug( u"request.META[u'QUERY_STRING']: %s" % request.META[u'QUERY_STRING'] ) | |
if not request.GET: | |
output = json.dumps( | |
{ u'docs': settings_app.DOCUMENTATION_URL, | |
u'hint': u'No solr parameters found.', | |
u'status': u'400 / Bad Request' }, indent=2, sort_keys=True ) | |
return HttpResponseBadRequest( output, content_type=u'application/json; charset=utf-8' ) | |
params_string = request.META[u'QUERY_STRING'].decode(u'utf-8', u'replace') | |
assert type(params_string) == unicode, type(params_string) | |
## construct solr url | |
try: | |
base_solr_url = settings_app.COLLECTION_MAPPER[ collection ] | |
logger.debug( u'base_solr_url: ' + base_solr_url ) | |
assert base_solr_url[-8:] == u'/select/', base_solr_url[-8:] | |
except Exception as e: | |
logger.debug( u'Exception: ' + repr(e).decode(u'utf-8', u'replace') ) | |
output = json.dumps( | |
{ u'docs': settings_app.DOCUMENTATION_URL, | |
u'hint': u'Invalid solr instance.', | |
u'status': u'400 / Bad Request' }, indent=2, sort_keys=True ) | |
return HttpResponseBadRequest( output, content_type=u'application/json; charset=utf-8' ) | |
url = u'%s?%s' % ( base_solr_url, params_string ) | |
logger.debug( u'Trying url: ' + url ) | |
## hit solr | |
try: | |
resp = requests.get( url ) | |
except Exception as e: | |
logger.error( u'Error querying solr: ' + str(e) ) | |
logger.error( u'Solr status code was: ' + str(resp.status_code) ) | |
logger.error( u'Solr data was: ' + resp.content ) | |
return HttpResponseServerError( u'Internal Server error.' ) | |
if resp.status_code == 400 and settings_app.PARSE_ERROR in resp.content: | |
logger.error( u'Got parse exception: ' + resp.content ) | |
return HttpResponseBadRequest( u'Error parsing query. Please check the format.' ) | |
## determine preferred response format | |
format_pref = request.GET.get( u'wt', None ) | |
if format_pref: | |
if format_pref == u'json': | |
content_type = u'application/json; charset=utf-8' | |
elif format_pref == u'xml': | |
content_type = u'text/xml; charset=utf-8' | |
else: | |
content_type = u'text/plain; charset=utf-8' | |
else: | |
content_type = u'text/xml; charset=utf-8' # solr default | |
## build response | |
output = resp.content.decode( u'utf-8', u'replace' ) | |
assert type(output) == unicode | |
callback = request.GET.get( u'callback', None ) | |
if callback: | |
assert type(callback) == unicode | |
output = u'%s(%s)' % ( callback, output ) | |
content_type = u'application/json; charset=utf-8' # overrides above determination | |
return HttpResponse( output, content_type=content_type ) | |
# end def search_custom() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment