Ejemplo n.º 1
0
def titlelookup(pid):
    s = SolrConnection("ora.ouls.ox.ac.uk:8080")
    results = {}
    query = ""
    if pid:
        pid = "\:".join(pid.split(":"))
        query = "id:%s" % pid
    elif uuid:
        query = "id:uuid\:%s" % uuid
    else:
        return results
    # Running actual query (3 tries, failover)
    tries = 0
    while (tries != 3):
        try:
            r = s.search(q=query, wt="json", fl="title")
            logger.debug("Solr response: %s" % r)
            tries = 3
        except BadStatusLine:
            sleep(0.5)
            tries = tries + 1
    try:
        results = simplejson.loads(r)
        assert results['response']['numFound'] == 1
        doc = results['response']['docs'][0]
        return doc['title']
    except ValueError:
        logger.warn("Couldn't parse json response from Solr endpoint: %s" % r)
        return {}
    except AssertionError:
        logger.warn(
            "Couldn't assert that only a single result was fetched: %s" %
            results)
        return {}
Ejemplo n.º 2
0
def oralookup(
    pid=None,
    uuid=None,
    fields_to_return="f_name, f_subject, f_keyphrase, faculty, f_institution, thesis_type, content_type, collection",
):
    s = SolrConnection("ora.ouls.ox.ac.uk:8080")
    results = {}
    query = ""
    if pid:
        pid = "\:".join(pid.split(":"))
        query = "id:%s" % pid
    elif uuid:
        query = "id:uuid\:%s" % uuid
    else:
        return results
    # Running actual query (3 tries, failover)
    tries = 0
    while tries != 3:
        try:
            r = s.search(q=query, wt="json", fl=fields_to_return)
            logger.debug("Solr response: %s" % r)
            tries = 3
        except BadStatusLine:
            sleep(0.5)
            tries = tries + 1
    try:
        results = simplejson.loads(r)
        assert results["response"]["numFound"] == 1
        return results["response"]["docs"][0]
    except ValueError:
        logger.warn("Couldn't parse json response from Solr endpoint: %s" % r)
        return {}
    except AssertionError:
        logger.warn("Couldn't assert that only a single result was fetched: %s" % results)
        return {}
Ejemplo n.º 3
0
def titlelookup(pid):
    s = SolrConnection("ora.ouls.ox.ac.uk:8080")
    results = {}
    query = ""
    if pid:
        pid = "\:".join(pid.split(":"))
        query = "id:%s" % pid
    elif uuid:
        query = "id:uuid\:%s" % uuid
    else:
        return results
    # Running actual query (3 tries, failover)
    tries = 0
    while tries != 3:
        try:
            r = s.search(q=query, wt="json", fl="title")
            logger.debug("Solr response: %s" % r)
            tries = 3
        except BadStatusLine:
            sleep(0.5)
            tries = tries + 1
    try:
        results = simplejson.loads(r)
        assert results["response"]["numFound"] == 1
        doc = results["response"]["docs"][0]
        return doc["title"]
    except ValueError:
        logger.warn("Couldn't parse json response from Solr endpoint: %s" % r)
        return {}
    except AssertionError:
        logger.warn("Couldn't assert that only a single result was fetched: %s" % results)
        return {}
Ejemplo n.º 4
0
def main():
    opts, args = parse_opts()
    logging.basicConfig(filename=opts.log_file, level=logging.DEBUG,
                        format='[%(asctime)s|%(levelname)s|%(name)s|%(threadName)s|%(message)s]')

    solr = SolrConnection(opts.solr_uri)
    protocol = LineProtocol()
    for request in protocol.input():
        try:
            query = build_query(request)
            if query is None:
                protocol.output(query_failed(), True)
                continue
            log.debug("Running query: " + str(query))
            results = solr.search(**query)
            if results is None:
                protocol.output({'code' : 400})
                continue
            resp = json.loads(results)
            ret = {
                'code' : 200,
                'json' : resp['response']
            }
            protocol.output(ret, True)
        except Exception:
            log.exception("Uncaught exception")
    return 0
Ejemplo n.º 5
0
class MLGBsolr:
    def __init__(self):
        self.s_result = ()
        self.conn = None
        self.connstatus = False
        self.req = None

    def solrconn(self, core='books'):
        try:
            solr_base_and_core = "%s/%s" % (solr_base, core)

            self.conn = SolrConnection( host=solr_host, solrBase=solr_base_and_core, \
                                        username=solr_uname, password=solr_pswd )
            self.connstatus = True
            return True
        except:
            #print "solr connection error!"
            return False

    def solrquery(self, para, core='books'):
        if self.solrconn(core):
            try:
                self.search = self.conn.search(para)
                self.connstatus = True
                return True
            except:
                #print "solr query failed!"
                return False

    def solrresults(self, para, Facet=False, core='books'):
        if self.solrquery(para, core):
            rsp = simplejson.loads(self.search)

            s_numFound = rsp['response'].get('numFound', None)
            s_docs = rsp['response'].get('docs', None)
            s_params = rsp['responseHeader'].get('params', None)
            s_rows = s_params.get('rows', None)
            s_start = s_params.get('start', None)
            s_q = s_params.get('q', None)
            if Facet:
                s_facet_fields = rsp['facet_counts'].get('facet_fields', None)

            self.s_result = {
                'numFound': s_numFound,
                'search_params': s_params,
                'query': s_q,
                'start': s_start,
                'rows': s_rows,
                'docs': s_docs
            }
            if Facet:
                self.s_result['facet'] = s_facet_fields

            def __unicode__(self):
                return self.s_result
Ejemplo n.º 6
0
class MLGBsolr:
    def __init__(self):
        self.s_result   = ()
        self.conn       = None
        self.connstatus = False
        self.req        = None

    def solrconn( self, core = 'books' ):
        try:
            solr_base_and_core = "%s/%s" % (solr_base, core)

            self.conn = SolrConnection( host=solr_host, solrBase=solr_base_and_core, \
                                        username=solr_uname, password=solr_pswd )
            self.connstatus=True
            return True
        except:
            #print "solr connection error!"
            return False
        
    def solrquery( self, para, core = 'books' ):
        if self.solrconn( core ):
            try:
                self.search = self.conn.search( para )
                self.connstatus=True
                return True
            except:
                #print "solr query failed!"
                return False
            
    def solrresults( self, para, Facet = False, core = 'books' ):
        if self.solrquery( para, core ):
            rsp = simplejson.loads( self.search )

            s_numFound = rsp[ 'response' ].get( 'numFound', None )
            s_docs     = rsp[ 'response' ].get( 'docs', None )
            s_params   = rsp[ 'responseHeader' ].get( 'params', None )
            s_rows  = s_params.get( 'rows',  None )
            s_start = s_params.get( 'start', None )
            s_q     = s_params.get( 'q',     None )
            if Facet :
                s_facet_fields=rsp['facet_counts'].get( 'facet_fields', None )
         
            self.s_result = {'numFound'     : s_numFound, 
                             'search_params': s_params, 
                             'query'        : s_q,
                             'start'        : s_start,
                             'rows'         : s_rows,
                             'docs'         : s_docs
                            }
            if Facet :
                self.s_result['facet'] = s_facet_fields

            def __unicode__(self):
                return self.s_result
Ejemplo n.º 7
0
def oralookup(
    pid=None,
    uuid=None,
    fields_to_return="f_name, f_subject, f_keyphrase, faculty, f_institution, thesis_type, content_type, collection"
):
    s = SolrConnection("ora.ouls.ox.ac.uk:8080")
    results = {}
    query = ""
    if pid:
        pid = "\:".join(pid.split(":"))
        query = "id:%s" % pid
    elif uuid:
        query = "id:uuid\:%s" % uuid
    else:
        return results
    # Running actual query (3 tries, failover)
    tries = 0
    while (tries != 3):
        try:
            r = s.search(q=query, wt="json", fl=fields_to_return)
            logger.debug("Solr response: %s" % r)
            tries = 3
        except BadStatusLine:
            sleep(0.5)
            tries = tries + 1
    try:
        results = simplejson.loads(r)
        assert results['response']['numFound'] == 1
        return results['response']['docs'][0]
    except ValueError:
        logger.warn("Couldn't parse json response from Solr endpoint: %s" % r)
        return {}
    except AssertionError:
        logger.warn(
            "Couldn't assert that only a single result was fetched: %s" %
            results)
        return {}