pythonpython-3.xpysolr

Using pysolr getting 400 error when trying to add data to solr


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>

Solution

  • 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