pythonpysolr

how to use urlopen for dynamic query string


I am working with python and solr and load the data from solr to python through url as:

connection = urlopen('http://localhost:8983/solr/data/select?indent=on&q=sender_name:*AX*%20AND%20message:*Avail%20Lmt&rows=211&start=0&wt=json')

if i have to pass different parameters in query parameter of different form using function.How can we pass as this query is of solr

sender_name:*SDI* - message:*Take this* (rows = 213 start=0)
sender_name:*TRY* - message:*Look Up*   (rows =300 start=0)

Solution

  • Normally this would be done by holding all of your parameters in a dictionary and then using urllib.parse.urlencode() to convert it into a suitable URL. For example:

    import urllib
    
    sender_name = '*AX*'
    message = '*Avail Lmt'
    
    parms = {'q' : f'sender_name:{sender_name} - message:{message}', 'rows' : 213, 'start' : 0}
    url = 'http://localhost:8983/solr/data/select?indent=on&' + urllib.parse.urlencode(parms, quote_via=urllib.parse.quote, safe='*:')
    print(url)
    

    This would give you:

    http://localhost:8983/solr/data/select?indent=on&q=sender_name:*AX*%20-%20message:*Avail%20Lmt&rows=213&start=0
    

    urllib.parse.quote() is used to give %20 for a space rather than +.