1

Using pysolr trying to add document to solr with Python3.9 and getting below error 400 even with only 1 or 2 fields. The fields that I'm using here are dynamic fields. No issue with connecting to solr.

#/usr/bin/python
import pysolr
solr = pysolr.Solr('http://localhost:8080/solr/', always_commit=True)
if solr.ping():
    print('connection successful')

docs = [{'id':'123c', 's_chan_name': 'TV-201'}]
solr.add(docs)

res = solr.search('123c')
print(res)

Getting below error:

connection successful 
Traceback (most recent call last):   
File "/tmp/test-solr.py", line 8, in <module>
        solr.add(docs)   
File "/usr/local/lib/python3.9/site-packages/pysolr.py", line 1042, in add
        return self._update(   File "/usr/local/lib/python3.9/site-packages/pysolr.py", line 568, in
    _update
       return self._send_request(   File "/usr/local/lib/python3.9/site-packages/pysolr.py", line 463, in
    _send_request
        raise SolrError(error_message % (resp.status_code, solr_message)) 
        pysolr.SolrError: Solr responded with an error (HTTP 400): [Reason: None] 
        <html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
        <title>Error 400 Unexpected character &apos;[&apos; (code 91) in prolog;
        expected &apos;<&apos; at [row,col {unknown-source}]: [1,1]</title>
        </head><body><h2>HTTP ERROR 400</h2><p>Problem accessing /solr/update/. 
        Reason:<pre>    Unexpected character &apos;[&apos; (code 91) in prolog; 
        expected &apos;<&apos; at [row,col {unknown-source}]: [1,1]</pre>
        </p><hr><a href="http://eclipse.org/jetty">
        Powered by Jetty://9.4.15.v20190215</a><hr/></body></html>
rajendrau
  • 38
  • 5

2 Answers2

1

I’d recommend looking at the debug logs (which might require you to use logging.basicConfig() to enable them) and testing the URLs it’s using yourself. Depending on your Solr configuration, it might be the case that your Solr URL needs to have the core at the end (e.g. http://localhost:8983/solr/mycore).

In general, most reports like this which we get turn out to be oddities in how someone configured Solr. In addition to the debug logging, I highly recommend looking through the test suite since that does everything needed to run Solr and can be a handy point for comparison:

https://github.com/django-haystack/pysolr

Chris Adams
  • 4,966
  • 1
  • 30
  • 28
  • Thank you very much Chris for your help. After debugging, found issue to be with the version of Solr I am using (pretty old 3.6). Installed and tried latest version of Solr. It's working fine with Solr 8.8. – rajendrau Apr 20 '21 at 09:47
  • Oh, yes, 3.6 is quite old — if memory serves, Solr didn't support the JSON request API until Solr 5 or so. – Chris Adams Apr 26 '21 at 21:15
0
solr.add method supports only one attribute 
docs = {'id':'123c', 's_chan_name': 'TV-201'}
solr.add(docs)

it is not suitable for iterable.

docs = [{'id':'123c', 's_chan_name': 'TV-201'}]
solr.add_many(docs)

it will works

  • This module pysolr takes care of adding many OR adding 1 doc to solr. If a dict is passed it internally converts to list with 1 doc. If list of dictonaries is passed, it will loop through all the docs and add them. Either 1 doc or multiple docs currently getting the same error. – rajendrau Apr 14 '21 at 10:01