view searchService/searchSolr.py @ 21:90643ccc6545

cross scripting and xml mime-type
author dwinter
date Tue, 29 Jan 2013 17:20:25 +0100
parents cf4503528b5e
children f748e2b684c9
line wrap: on
line source

'''
Created on 16.11.2012

@author: dwinter
'''

import solr
import web
import urllib
import os.path
import json
import urllib2
import logging

SOLR_SERVER="https://md.mpiwg-berlin.mpg.de/solr"
#SOLR_SERVER="http://127.0.0.1:8983/solr"
DRI_SERVER="http://md.mpiwg-berlin.mpg.de/"

class searchSolr:
    
    def __init__(self):
        #logging.basicConfig(filename='/tmp/solr.log',level=logging.DEBUG)
        self.con = solr.SolrConnection(SOLR_SERVER,debug=False)
        
        self.search = solr.SearchHandler(self.con,"/collection1/select")
        
    
    def GET(self):
        paras = web.input()
        
        if not hasattr(paras,'text-url-path'):
            return "usage: ?text-url-path=PATH"
          
        if getattr(paras,'format','')== "short":
            short=True
        else:
            short=False
        
        
        queryString=paras.get("text-url-path")

        return self.doGet(queryString,short=short)
    
    def doGet(self,queryString,short=False):
        
        queryString="""text-url-path:"%s" """%queryString
        response = self.search(queryString)
        
        ret=""
        hitId=0
        rows=[]

        ret="<results>"
        
        for hit in response:
            
            ret+="<result>"
            
            if short:
                key="text-url-path"
                r=hit.get(key)
                ret+="""<%s>%s<%s>"""%(key,r,key) 
            
            else: 
                
                for key in hit.keys():
                    res=hit.get(key)
                    if  not isinstance(res, list):
                        res=[res]
                    
                    
                    
                    for r in res:
    
                        ret+="""<%s>%s<%s>"""%(key,r,key) 
                
                #ret.append(hit.get('archive-path'))
        
            ret+="</result>"
        
        
        web.header('Content-Type', 'text/xml')
        web.header('Access-Control-Allow-Origin', '*')
        web.header('Access-Control-Allow-Credentials', 'true')
       
        return ret+"</results>"
    


        


if __name__ == '__main__':
    sl = searchSolr()
    
    x=sl.doGet("/diverse/de/Einst_Bemer_de_1907.xml",short=True)
    
    print x.encode("utf-8")