Running Solr queries and updates via curl

 
Published on 2012-09-21 by John Collins. Socials: YouTube - X - Spotify - Amazon Music - Apple Podcast

I am a big fan of Solr: it is one of those rare systems that just works out of the box, and I have successfully used it as a black-box search solution on several projects. The main reason why you can treat Solr as a black-box search solution is that it has an excellent API that is widely supported by many client tools and libraries, which successfully isolates you from it's internal workings (as any good API should).

However you do not need anything fancy to use Solr, as you can interact with it's API directly using nothing more than curl on the command line.

Here are some common CRUD examples:

Create/Update an index entry

Here I am assuming the following simple data schema:

<fields>
    <field name="id" type="string" indexed="true" stored="true" required="true" />
    <field name="username" type="text_en" indexed="true" stored="true"/>
    <field name="firstname" type="text_en" indexed="true" stored="true"/>
    <field name="lastname" type="text_en" indexed="true" stored="true"/>
    <field name="classname" type="text_en" indexed="true" stored="true"/>
</fields>
 
<uniqueKey>id</uniqueKey>

Now to create a new index entry (in this example representing a User record), I can do the following:

$ curl http://localhost:8080/solr/update?commit=true -H "Content-Type: text/xml" --data-binary '<add><doc><field name="id">12345</field><field name="username">alphadevx</field><field name="firstname">John</field><field name="lastname">Collins</field><field name="classname">User</field></doc></add>'
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader"><int name="status">0</int><int name="QTime">370</int></lst>
</response>

The XML format I am posting is pretty clear to follow, and is based off the example from the Solr wiki. The -H flag sets the Content-Type header on our request to be XML, and finally the commit=true parameter tells Solr to commit the update we are sending to it on this request.

Note that the status code of zero in the response from Solr indicates success.

To perform an update on this record, simple submit the same request again only with the new data this time (here I am changing my surname from Collins to Doe):

curl http://localhost:8080/solr/update?commit=true -H "Content-Type: text/xml" --data-binary '<add><doc><field name="id">12345</field><field name="username">alphadevx</field><field name="firstname">John</field><field name="lastname">Doe</field><field name="classname">User</field></doc></add>'
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader"><int name="status">0</int><int name="QTime">406</int></lst>
</response>

Reading an index entry

To read back your new index entry, you need to perform a query request on the id field which is unique so will only return one result (or nothing):

curl http://localhost:8080/solr/select?q=id:12345
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader"><int name="status">0</int><int name="QTime">1</int><lst name="params"><str name="q">id:12345</str></lst></lst><result name="response" numFound="1" start="0"><doc><str name="firstname">John</str><str name="id">12345</str><str name="lastname">Doe</str><str name="username">alphadevx</str></doc></result>
</response>

The query syntax in Solr is very extensive and is beyond the scope of this tutorial, please see the Solr wiki for more information.

Deleting an index entry

And now to delete it:

curl http://localhost:8080/solr/update?commit=true -H "Content-Type: text/xml" --data-binary '<delete><id>12345</id></delete>'
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader"><int name="status">0</int><int name="QTime">69</int></lst>
</response>

...and just to confirm it is gone, run the query to try to find it again:

curl http://localhost:8080/solr/select?q=id:12345
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader"><int name="status">0</int><int name="QTime">2</int><lst name="params"><str name="q">id:12345</str></lst></lst><result name="response" numFound="0" start="0"/>
</response>

Updated 2022 : note that the above post was originally published in 2012, so may be out-dated.