0

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)
Swadhikar
  • 2,152
  • 1
  • 19
  • 32
Saurabh
  • 21
  • 7
  • You can use string formatting here the same as with any other string, like `urlopen(f'http://localhost:8983/solr/data/select?indent=on&q=sender_name:{sender_name}%20AND%20message:{message}%20Lmt&rows={rows}&start={tart}&wt=json')`. – abarnert Jun 14 '18 at 07:55
  • Alternatively, you can use the functions in `urllib` (although it's easier with `requests`) to build the query string out of a dict. (Although you'll still need some string formatting to deal with the `sender_name` and `message` if those are in separate local variables, because they get crammed into the same query variable.) – abarnert Jun 14 '18 at 07:57
  • @abarnert how we pass these parameter – Saurabh Jun 14 '18 at 07:59
  • 1
    If you’re using Python 3.6 or later, you don’t have to explicitly pass anything. That’s the point of f-strings: they can just pick up your local variables. If you’re using 2.7 or 3.5, you need to call the format method, like `'blah blah {} stuff {}'.format(firstvar, secondvar)`. Read the tutorial on strings; it will explain things much better than I can do in a comment. – abarnert Jun 14 '18 at 08:01
  • @abarnert if we have to use 3 parameter or 4 parameter then it will not work.Because we have different number of parameter like 2,3,4 or more – Saurabh Jun 14 '18 at 08:27
  • Well, yes, you need to know what variables you're using to use those variables. I don't know what else you're expecting. – abarnert Jun 14 '18 at 16:22

1 Answers1

0

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 +.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
  • You could try adding `quote_via=urllib.parse.quote` as a parameter to `urlencode()` to force the use of `%20` for spaces. – Martin Evans Jun 14 '18 at 09:46
  • if there is request such that sender_name:*SDI* -message:*Take this* .Actually it is giving error in this method – Saurabh Jun 14 '18 at 10:18
  • Could you edit your question to include the expected URL for such a request? – Martin Evans Jun 14 '18 at 10:21
  • It is difficult to see what variables you need. One takes `AND` and another takes `-`? The `sender_name` and `message` variables could be included separately. Also your valid URL appears to be ok with taking `*` and `:` so I have allowed these. – Martin Evans Jun 14 '18 at 11:01